1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-25 22:41:46 +02:00

Add global propagators (#494)

* Add global propagators

The default global propagators are set to the chained W3C trace and
correlation context propagators.

* Use global propagators in plugins

The httptrace and grpcplugins should also get some API for setting a
propagator to use (the othttp plugin already has such an API), but
that can come in some other PR.

* Decrease indentation in trace propagators

* Drop obsolete TODOs

Now we do "something" with correlation context - it ends up in the
context, and we put the context into the request, so the chained HTTP
handler can access it too.

The other TODO was about tag.Upsert which is long gone.

* Do not unnecessarily update the request context

The request context already contains the span (and we add some
attribute there), so inserting it into context again is pointless.

Co-authored-by: Joshua MacDonald <jmacd@users.noreply.github.com>
This commit is contained in:
Rahul Patel
2020-03-05 10:12:10 -08:00
committed by GitHub
parent 6769330394
commit 547d584da8
7 changed files with 85 additions and 58 deletions

View File

@@ -19,7 +19,6 @@ import (
"net/http"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/correlation"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/propagation"
"go.opentelemetry.io/otel/api/trace"
@@ -78,8 +77,8 @@ func WithPublicEndpoint() Option {
}
// WithPropagators configures the Handler with specific propagators. If this
// option isn't specified then Propagators with
// go.opentelemetry.io/otel/api/trace.DefaultHTTPPropagator are used.
// option isn't specified then
// go.opentelemetry.io/otel/api/global.Propagators are used.
func WithPropagators(ps propagation.Propagators) Option {
return func(h *Handler) {
h.props = ps
@@ -128,15 +127,9 @@ func WithMessageEvents(events ...event) Option {
// named after the operation and with any provided HandlerOptions.
func NewHandler(handler http.Handler, operation string, opts ...Option) http.Handler {
h := Handler{handler: handler, operation: operation}
tcPropagator := trace.DefaultHTTPPropagator()
ccPropagator := correlation.DefaultHTTPPropagator()
props := propagation.New(
propagation.WithInjectors(tcPropagator, ccPropagator),
propagation.WithExtractors(tcPropagator, ccPropagator),
)
defaultOpts := []Option{
WithTracer(global.TraceProvider().Tracer("go.opentelemetry.io/plugin/othttp")),
WithPropagators(props),
WithPropagators(global.Propagators()),
WithSpanOptions(trace.WithSpanKind(trace.SpanKindServer)),
}
@@ -150,7 +143,6 @@ func NewHandler(handler http.Handler, operation string, opts ...Option) http.Han
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
opts := append([]trace.StartOption{}, h.spanStartOptions...) // start with the configured options
// TODO: do something with the correlation context
ctx := propagation.ExtractHTTP(r.Context(), h.props, r.Header)
ctx, span := h.tracer.Start(ctx, h.operation, opts...)
defer span.End()
@@ -215,8 +207,7 @@ func setAfterServeAttributes(span trace.Span, read, wrote, statusCode int64, rer
func WithRouteTag(route string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
span := trace.SpanFromContext(r.Context())
//TODO: Why doesn't tag.Upsert work?
span.SetAttributes(RouteKey.String(route))
h.ServeHTTP(w, r.WithContext(trace.ContextWithSpan(r.Context(), span)))
h.ServeHTTP(w, r)
})
}