Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: add requests count metric with status code label #771

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [0.21.0] - 2021-06-18

### Added

- Add total requests counter by response status code for the `net/http` instrumentation. (#771)

Comment on lines +13 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would need to be moved up into the "Unreleased" section above.

### Fixed

- Dockerfile based examples for `otelgin` and `otelmacaron`. (#767)
Expand Down
8 changes: 8 additions & 0 deletions instrumentation/net/http/otelhttp/handler.go
Expand Up @@ -102,11 +102,15 @@ func (h *Handler) createMeasures() {
responseBytesCounter, err := h.meter.NewInt64Counter(ResponseContentLength)
handleErr(err)

requestCount, err := h.meter.NewInt64Counter(RequestCount)
handleErr(err)

serverLatencyMeasure, err := h.meter.NewInt64ValueRecorder(ServerLatency)
handleErr(err)

h.counters[RequestContentLength] = requestBytesCounter
h.counters[ResponseContentLength] = responseBytesCounter
h.counters[RequestCount] = requestCount
h.valueRecorders[ServerLatency] = serverLatencyMeasure
}

Expand Down Expand Up @@ -185,6 +189,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.counters[RequestContentLength].Add(ctx, bw.read, attributes...)
h.counters[ResponseContentLength].Add(ctx, rww.written, attributes...)

// Count of request to record errors ratio
requestCountAttributes := append(attributes, semconv.HTTPStatusCodeKey.Int(rww.statusCode))
h.counters[RequestCount].Add(ctx, 1, requestCountAttributes...)

elapsedTime := time.Since(requestStartTime).Microseconds()

h.valueRecorders[ServerLatency].Record(ctx, elapsedTime, attributes...)
Expand Down
14 changes: 13 additions & 1 deletion instrumentation/net/http/otelhttp/handler_test.go
Expand Up @@ -82,7 +82,19 @@ func TestHandlerBasics(t *testing.T) {
attribute.String("test", "attribute"),
}

assertMetricAttributes(t, attributesToVerify, meterimpl.MeasurementBatches)
var statusCodeBatch oteltest.Batch
var measurementBatches []oteltest.Batch
for _, batch := range meterimpl.MeasurementBatches {
batchName := batch.Measurements[0].Instrument.Descriptor().Name()
if batchName == RequestCount {
statusCodeBatch = batch
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this continue needed?

If I am not missing anything then I think that we could assertMetricAttributes for RequestCount metric as well

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will check it. Maybe I missed something.

}
measurementBatches = append(measurementBatches, batch)
}

assertMetricAttributes(t, attributesToVerify, measurementBatches)
assert.ElementsMatch(t, append(attributesToVerify, semconv.HTTPStatusCodeKey.Int(200)), statusCodeBatch.Labels)

if got, expected := rr.Result().StatusCode, http.StatusOK; got != expected {
t.Fatalf("got %d, expected %d", got, expected)
Expand Down