-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtrace.go
53 lines (41 loc) · 1.71 KB
/
trace.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
package httpserver
import (
"context"
"go.opentelemetry.io/otel/attribute"
otelsdktrace "go.opentelemetry.io/otel/sdk/trace"
oteltrace "go.opentelemetry.io/otel/trace"
)
// TraceSpanAttributeHttpRequestId is a span attribute representing the request id.
const TraceSpanAttributeHttpRequestId = "guid:x-request-id"
// AnnotateTracerProvider extend adds the [TracerProviderRequestIdAnnotator] span processor to the provided [oteltrace.TracerProvider].
func AnnotateTracerProvider(base oteltrace.TracerProvider) oteltrace.TracerProvider {
if tp, ok := base.(*otelsdktrace.TracerProvider); ok {
tp.RegisterSpanProcessor(NewTracerProviderRequestIdAnnotator())
return tp
}
return base
}
// TracerProviderRequestIdAnnotator is a span processor to add the contextual request id as span attribute.
type TracerProviderRequestIdAnnotator struct{}
// NewTracerProviderRequestIdAnnotator returns a new [TracerProviderRequestIdAnnotator].
func NewTracerProviderRequestIdAnnotator() *TracerProviderRequestIdAnnotator {
return &TracerProviderRequestIdAnnotator{}
}
// OnStart adds the contextual request id as span attribute.
func (a *TracerProviderRequestIdAnnotator) OnStart(ctx context.Context, s otelsdktrace.ReadWriteSpan) {
if rid, ok := ctx.Value(CtxRequestIdKey{}).(string); ok {
s.SetAttributes(attribute.String(TraceSpanAttributeHttpRequestId, rid))
}
}
// Shutdown performs no operations.
func (a *TracerProviderRequestIdAnnotator) Shutdown(context.Context) error {
return nil
}
// ForceFlush performs no operations.
func (a *TracerProviderRequestIdAnnotator) ForceFlush(context.Context) error {
return nil
}
// OnEnd performs no operations.
func (a *TracerProviderRequestIdAnnotator) OnEnd(otelsdktrace.ReadOnlySpan) {
// noop
}