-
Notifications
You must be signed in to change notification settings - Fork 662
otelhttp: Record metrics on timed out requests #4634
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
Changes from 7 commits
a971219
a52aa2d
39362c3
55b12a7
d521689
d6d3728
4cd08b4
2a310bd
c5e3cba
4bde186
ccf1f47
238bcb6
caf1167
b76e57b
0fbf817
85a8e2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
package otelhttp // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
@@ -232,13 +233,14 @@ func (h *middleware) serveHTTP(w http.ResponseWriter, r *http.Request, next http | |
attributes = append(attributes, semconv.HTTPStatusCode(rww.statusCode)) | ||
} | ||
o := metric.WithAttributes(attributes...) | ||
h.requestBytesCounter.Add(ctx, bw.read, o) | ||
h.responseBytesCounter.Add(ctx, rww.written, o) | ||
ctxWithoutCancel := withoutCancel(ctx) | ||
h.requestBytesCounter.Add(ctxWithoutCancel, bw.read, o) | ||
h.responseBytesCounter.Add(ctxWithoutCancel, rww.written, o) | ||
|
||
// Use floating point division here for higher precision (instead of Millisecond method). | ||
elapsedTime := float64(time.Since(requestStartTime)) / float64(time.Millisecond) | ||
|
||
h.serverLatencyMeasure.Record(ctx, elapsedTime, o) | ||
h.serverLatencyMeasure.Record(ctxWithoutCancel, elapsedTime, o) | ||
} | ||
|
||
func setAfterServeAttributes(span trace.Span, read, wrote int64, statusCode int, rerr, werr error) { | ||
|
@@ -281,3 +283,34 @@ func WithRouteTag(route string, h http.Handler) http.Handler { | |
h.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func withoutCancel(parent context.Context) context.Context { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a note, including a link, about where this is copied from. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Happy to do this, but it sounding like the better option might be the SDK native one (so long as we would not need the flexibility of calling those There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added, in case we opt to merge this PR. Thanks @MrAlias! |
||
if parent == nil { | ||
panic("cannot create context from nil parent") | ||
} | ||
return withoutCancelCtx{parent} | ||
} | ||
|
||
type withoutCancelCtx struct { | ||
c context.Context | ||
} | ||
|
||
func (withoutCancelCtx) Deadline() (deadline time.Time, ok bool) { | ||
return | ||
} | ||
|
||
func (withoutCancelCtx) Done() <-chan struct{} { | ||
return nil | ||
} | ||
|
||
func (withoutCancelCtx) Err() error { | ||
return nil | ||
} | ||
|
||
func (w withoutCancelCtx) Value(key any) any { | ||
return w.c.Value(key) | ||
} | ||
|
||
func (w withoutCancelCtx) String() string { | ||
return "withoutCancel" | ||
} |
Uh oh!
There was an error while loading. Please reload this page.