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

Add request count metric with status code label #2138

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -189,6 +189,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

Expand Down
4 changes: 3 additions & 1 deletion instrumentation/net/http/otelhttp/handler.go
Expand Up @@ -225,7 +225,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) {
Expand Down