diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e39fca8fa4..a7589dc3c70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added - The project is now tested against Go 1.18 (in addition to the existing 1.16 and 1.17) (#1976) +- Add the HTTP status code label to `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` instrumentation that allows to count requests by their response code. (#771, #2138) ### Changed diff --git a/instrumentation/net/http/otelhttp/handler.go b/instrumentation/net/http/otelhttp/handler.go index e3bc701a2a7..31893edb65a 100644 --- a/instrumentation/net/http/otelhttp/handler.go +++ b/instrumentation/net/http/otelhttp/handler.go @@ -200,7 +200,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Use floating point division here for higher precision (instead of Millisecond method). elapsedTime := float64(time.Since(requestStartTime)) / float64(time.Millisecond) - h.valueRecorders[ServerLatency].Record(ctx, elapsedTime, attributes...) + // Status code label is required to count the errors ratio + durationAttributes := append(attributes, semconv.HTTPStatusCodeKey.Int(rww.statusCode)) + h.valueRecorders[ServerLatency].Record(ctx, elapsedTime, durationAttributes...) } func setAfterServeAttributes(span trace.Span, read, wrote int64, statusCode int, rerr, werr error) {