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

Support propagation of trace context headers in http gateway #9168

Open
3 tasks done
Tracked by #17
iand opened this issue Aug 5, 2022 · 4 comments
Open
3 tasks done
Tracked by #17

Support propagation of trace context headers in http gateway #9168

iand opened this issue Aug 5, 2022 · 4 comments
Assignees
Labels
kind/feature A new feature

Comments

@iand
Copy link
Contributor

iand commented Aug 5, 2022

Checklist

  • My issue is specific & actionable.
  • I am not suggesting a protocol enhancement.
  • I have searched on the issue tracker for my issue.

Description

The HTTP gateway should support propagation of trace context headers using the W3C Trace Context recommendation.

This consists of supporting two optional HTTP headers traceparent and tracestate on incoming requests, adopting the trace id from the traceparent header for internal traces and emitting updated or new headers on responses.

This will allow:

  1. people using the gateway as a component in their infrastructure to correlate traces that pass through it
  2. gateway providers to trace incoming requests and correlate with logs
  3. performance optimizers to correlate traces with experiments

Of particular use will be the "sampled" flag on the emitted traceparent header that indicates whether the request was sampled for tracing by Kubo.

Implementation Note

This should be as simple as using the opentelemetry TraceContext, roughly:

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    p := propagation.TraceContext{}
    ctx := p.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
    
    // do stuff

   p.Inject(ctx, propagation.HeaderCarrier(w.Header()))
}
@iand iand added the kind/feature A new feature label Aug 5, 2022
@iand iand self-assigned this Aug 5, 2022
@guseggert
Copy link
Contributor

guseggert commented Aug 5, 2022

The HTTP handler is already wrapped with the OTel handler which applies any propagators (see https://github.com/ipfs/kubo/blob/master/core/corehttp/gateway.go#L106 and https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/instrumentation/net/http/otelhttp/handler.go#L130).

There's an env var in the spec, OTEL_PROPAGATORS, that allows one to install propagators, and it is specified with a default of "tracecontext,baggage" (see https://opentelemetry.io/docs/reference/specification/sdk-environment-variables/#general-sdk-configuration). I've wired that up in this PR: #9169 (just needs a sharness test to ensure the propagation works and doesn't break).

@iand
Copy link
Contributor Author

iand commented Aug 5, 2022

Neat. Do you need help on the tests for #9169 @guseggert ?

@guseggert
Copy link
Contributor

That would be great if you have the time!

@iand
Copy link
Contributor Author

iand commented Aug 11, 2022

That would be great if you have the time!

@guseggert see #9180

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A new feature
Projects
None yet
Development

No branches or pull requests

2 participants