-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #312 from xmidt-org/trackLatency
Track latency
- Loading branch information
Showing
11 changed files
with
359 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/go-kit/kit/metrics" | ||
) | ||
|
||
var ( | ||
errNilHistogram = errors.New("histogram cannot be nil") | ||
) | ||
|
||
type httpClient interface { | ||
Do(*http.Request) (*http.Response, error) | ||
} | ||
|
||
func nopHTTPClient(next httpClient) httpClient { | ||
return next | ||
} | ||
|
||
// DoerFunc implements HTTPClient | ||
type doerFunc func(*http.Request) (*http.Response, error) | ||
|
||
func (d doerFunc) Do(req *http.Request) (*http.Response, error) { | ||
return d(req) | ||
} | ||
|
||
type metricWrapper struct { | ||
now func() time.Time | ||
queryLatency metrics.Histogram | ||
} | ||
|
||
func newMetricWrapper(now func() time.Time, queryLatency metrics.Histogram) (*metricWrapper, error) { | ||
if now == nil { | ||
now = time.Now | ||
} | ||
if queryLatency == nil { | ||
return nil, errNilHistogram | ||
} | ||
return &metricWrapper{ | ||
now: now, | ||
queryLatency: queryLatency, | ||
}, nil | ||
} | ||
|
||
func (m *metricWrapper) roundTripper(next httpClient) httpClient { | ||
return doerFunc(func(req *http.Request) (*http.Response, error) { | ||
startTime := m.now() | ||
resp, err := next.Do(req) | ||
endTime := m.now() | ||
code := networkError | ||
|
||
if err == nil { | ||
code = strconv.Itoa(resp.StatusCode) | ||
} | ||
|
||
// find time difference, add to metric | ||
var latency = endTime.Sub(startTime) | ||
m.queryLatency.With("code", code).Observe(latency.Seconds()) | ||
|
||
return resp, err | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/** | ||
* Copyright 2022 Comcast Cable Communications Management, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-kit/kit/metrics" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRoundTripper(t *testing.T) { | ||
errTest := errors.New("test error") | ||
date1 := time.Date(2021, time.Month(2), 21, 1, 10, 30, 0, time.UTC) | ||
date2 := time.Date(2021, time.Month(2), 21, 1, 10, 30, 45, time.UTC) | ||
|
||
tests := []struct { | ||
description string | ||
startTime time.Time | ||
endTime time.Time | ||
expectedCode string | ||
request *http.Request | ||
expectedErr error | ||
expectedResponse *http.Response | ||
}{ | ||
{ | ||
description: "Success", | ||
startTime: date1, | ||
endTime: date2, | ||
expectedCode: "200", | ||
request: exampleRequest(1), | ||
expectedErr: nil, | ||
expectedResponse: &http.Response{ | ||
StatusCode: 200, | ||
}, | ||
}, | ||
{ | ||
description: "503 Service Unavailable", | ||
startTime: date1, | ||
endTime: date2, | ||
expectedCode: "503", | ||
request: exampleRequest(1), | ||
expectedErr: nil, | ||
expectedResponse: &http.Response{ | ||
StatusCode: 503, | ||
}, | ||
}, | ||
{ | ||
description: "Network Error", | ||
startTime: date1, | ||
endTime: date2, | ||
expectedCode: "network_err", | ||
request: exampleRequest(1), | ||
expectedErr: errTest, | ||
expectedResponse: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
|
||
t.Run(tc.description, func(t *testing.T) { | ||
|
||
fakeTime := mockTime(tc.startTime, tc.endTime) | ||
fakeHandler := new(mockHandler) | ||
fakeHist := new(mockHistogram) | ||
histogramFunctionCall := []string{"code", tc.expectedCode} | ||
fakeLatency := date2.Sub(date1) | ||
fakeHist.On("With", histogramFunctionCall).Return().Once() | ||
fakeHist.On("Observe", fakeLatency.Seconds()).Return().Once() | ||
|
||
// Create a roundtripper with mock time and mock histogram | ||
m, err := newMetricWrapper(fakeTime, fakeHist) | ||
require.NoError(t, err) | ||
require.NotNil(t, m) | ||
|
||
client := doerFunc(func(*http.Request) (*http.Response, error) { | ||
|
||
return tc.expectedResponse, tc.expectedErr | ||
}) | ||
|
||
c := m.roundTripper(client) | ||
resp, err := c.Do(tc.request) | ||
|
||
if tc.expectedErr == nil { | ||
// Read and close response body | ||
if resp.Body != nil { | ||
io.Copy(ioutil.Discard, resp.Body) | ||
resp.Body.Close() | ||
} | ||
assert.NoError(t, err) | ||
} else { | ||
assert.ErrorIs(t, tc.expectedErr, err) | ||
} | ||
|
||
// Check the histogram and expectations | ||
fakeHandler.AssertExpectations(t) | ||
fakeHist.AssertExpectations(t) | ||
|
||
}) | ||
} | ||
|
||
} | ||
|
||
func TestNewMetricWrapper(t *testing.T) { | ||
|
||
tests := []struct { | ||
description string | ||
expectedErr error | ||
fakeTime func() time.Time | ||
fakeHistogram metrics.Histogram | ||
}{ | ||
{ | ||
description: "Success", | ||
expectedErr: nil, | ||
fakeTime: time.Now, | ||
fakeHistogram: &mockHistogram{}, | ||
}, | ||
{ | ||
description: "Nil Histogram", | ||
expectedErr: errNilHistogram, | ||
fakeTime: time.Now, | ||
fakeHistogram: nil, | ||
}, | ||
{ | ||
description: "Nil Time", | ||
expectedErr: nil, | ||
fakeTime: nil, | ||
fakeHistogram: &mockHistogram{}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
|
||
t.Run(tc.description, func(t *testing.T) { | ||
|
||
// Make function call | ||
mw, err := newMetricWrapper(tc.fakeTime, tc.fakeHistogram) | ||
|
||
if tc.expectedErr == nil { | ||
// Check for no errors | ||
assert.NoError(t, err) | ||
require.NotNil(t, mw) | ||
|
||
// Check that the time and histogram aren't nil | ||
assert.NotNil(t, mw.now) | ||
assert.NotNil(t, mw.queryLatency) | ||
return | ||
} | ||
|
||
// with error checks | ||
assert.Nil(t, mw) | ||
assert.ErrorIs(t, err, tc.expectedErr) | ||
|
||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.