-
Notifications
You must be signed in to change notification settings - Fork 2
/
propagation.go
104 lines (80 loc) · 2.9 KB
/
propagation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2022 Outreach Corporation. All Rights Reserved.
// Description: Provides propagation capabilities for traces
package trace
import (
"context"
"net/http"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
otelpropagation "go.opentelemetry.io/otel/propagation"
oteltrace "go.opentelemetry.io/otel/trace"
)
const (
// Header that enforces the tracing for particular request
HeaderForceTracing = "X-Force-Trace"
// Header used by OpenTelemetry to propagate traces
OtelPropagationHeader = "Traceparent"
)
type roundtripper struct {
old http.RoundTripper
}
func (rt roundtripper) RoundTrip(r *http.Request) (*http.Response, error) {
// Pass along the `X-Force-Trace` header if we received one.
if isTracingForced(r.Context()) {
r.Header.Set(HeaderForceTracing, "true")
}
return rt.old.RoundTrip(r)
}
func (t *otelTracer) newTransport(old http.RoundTripper) http.RoundTripper {
return otelhttp.NewTransport(&roundtripper{old})
}
type Handler struct {
handler http.Handler
operation string
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var startOptions oteltrace.SpanStartEventOption
handler := otelhttp.NewHandler(h.handler, h.operation)
force := r.Header.Get(HeaderForceTracing)
if force != "" {
startOptions = oteltrace.WithAttributes(attribute.String(fieldForceTrace, force))
handler = otelhttp.NewHandler(h.handler, h.operation, otelhttp.WithSpanOptions(startOptions))
r = r.WithContext(forceTracing(r.Context()))
}
handler.ServeHTTP(w, r)
}
func (t *otelTracer) newHandler(handler http.Handler, operation string) http.Handler {
h := Handler{
handler: handler,
operation: operation,
}
return &h
}
func (t *otelTracer) toHeaders(ctx context.Context) map[string][]string {
result := http.Header{}
if !oteltrace.SpanFromContext(ctx).SpanContext().HasTraceID() {
return result
}
propagator := otel.GetTextMapPropagator()
propagator.Inject(ctx, otelpropagation.HeaderCarrier(result))
return result
}
// contextFromHeaders loads the headers into a context as 'detached'. This method does not create a new span,
// thus do not end it and do not expose it directly to the callers (it is for FromHeaders and for WithLink usage only).
func (t *otelTracer) contextFromHeaders(ctx context.Context, hdrs map[string][]string) context.Context {
header := http.Header(hdrs)
force := header.Get(HeaderForceTracing)
if force != "" {
ctx = ForceTracing(ctx)
}
propagator := otel.GetTextMapPropagator()
ctx = propagator.Extract(ctx, otelpropagation.HeaderCarrier(header))
// this method does not create new span to allow WithLink-related methods use the context to generate otel's Link
return ctx
}
func (t *otelTracer) fromHeaders(ctx context.Context, hdrs map[string][]string, name string) context.Context {
ctx = t.contextFromHeaders(ctx, hdrs)
ctx = StartSpan(ctx, name)
return ctx
}