-
Notifications
You must be signed in to change notification settings - Fork 89
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 context in Callback #185
Comments
You can at least inject a import (
"context"
"net/http"
httpTrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
analytics "gopkg.in/segmentio/analytics-go.v3"
// sorry if I missed some
)
type tracedTransport struct {
base http.RoundTripper
ctx context.Context
}
func (r *ddTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// get datadog span from context
ctx := r.withTracingMetadata(r.ctx, req.Context())
req = req.WithContext(ctx)
return r.base.RoundTrip(req)
}
// framework-specific
func (r *ddTransport) withTracingMetadata(traceCtx, reqCtx context.Context) context.Context {
span, _ := tracer.SpanFromContext(traceCtx)
return tracer.ContextWithSpan(reqCtx, span)
}
func SegmentClientConstructor(ctx context.Context, writeKey) analytics.SegmentClient {
transport := http.DefaultTransport
// if you need to use a transport that expects to find tracing metadata in `req.Context`,
// here's where you'd put it
transport = httpTrace.WrapRoundTripper(transport)
transport = &tracedTransport{
base: transport,
ctx: ctx,
}
config := analytics.Config{
// ...
Transport: transport,
}
client, err := analytics.NewWithConfig(
writeKey,
config,
)
if err != nil {
// ...
}
return client
}
|
Typically, to trace requests I would
The Go segment API allows me to set the transport but not pass the current request context at the request time. The above snippet relies on a datadog wrapper, I haven't seen an equivalent for OTEL... but... it would be the wrong context if it was right, the background / global context rather than the request context. Is there a way to get the request context in the track/identify/group etc method that would pass to the outbound http request? |
Is it possible to add context in OnSuccess and OnFailure callbacks for proper span tracing?
The text was updated successfully, but these errors were encountered: