-
Notifications
You must be signed in to change notification settings - Fork 57
/
contextual_tracer.go
39 lines (31 loc) · 1.21 KB
/
contextual_tracer.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
package tracing
import (
"context"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/embedded"
"github.com/grafana/grafana-plugin-sdk-go/internal/tenant"
)
const (
attributeNameTenantID = "tenant_id"
)
// contextualTracer is a wrapper around a trace.Tracer that adds contextual attributes to spans.
// This is the default tracer used by the SDK.
type contextualTracer struct {
embedded.Tracer
tracer trace.Tracer
}
// setContextualSpanAttributes adds contextual attributes to the span.
func (t *contextualTracer) setContextualSpanAttributes(ctx context.Context, span trace.Span) {
if tid := tenant.IDFromContext(ctx); tid != "" {
span.SetAttributes(attribute.String(attributeNameTenantID, tid))
}
}
// Start starts a span with the given name and options using the underlying tracer.
// It then sets the contextual span attrbutes, and returns the span with the contextual attributes,
// along with the new context.
func (t *contextualTracer) Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
ctx, span := t.tracer.Start(ctx, spanName, opts...)
t.setContextualSpanAttributes(ctx, span)
return ctx, span
}