-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracer.go
57 lines (49 loc) · 1.69 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package ddspanname
import (
"context"
"go.opentelemetry.io/otel/trace"
)
type tracer struct {
base trace.Tracer
operationName string
operationNameFormatter OperationNameFormatter
spanNameFormatter SpanNameFormatter
}
// NewTracer creates a [trace.Tracer] which sets the attributes "operation.name" and "resource.name",
// so that DataDog span names are formatted correctly.
func NewTracer(base trace.Tracer, operationName string, opts ...TracerOption) trace.Tracer {
ret := &tracer{
base: base,
operationName: operationName,
operationNameFormatter: defaultOperationNameFormatter,
spanNameFormatter: defaultSpanNameFormatter,
}
for _, opt := range opts {
opt(ret)
}
return ret
}
func (d *tracer) Start(ctx context.Context, spanName string,
opts ...trace.SpanStartOption) (context.Context, trace.Span) {
operationName := d.operationNameFormatter(ctx, d.operationName)
spanName = d.spanNameFormatter(ctx, operationName, spanName)
rctx, span := d.base.Start(ctx, spanName, opts...)
span.SetAttributes(
DDOperationNameKey.String(operationName),
DDResourceNameKey.String(spanName),
)
return rctx, span
}
type TracerOption func(*tracer)
// WithTracerOperationNameFormatter sets a function to customize the operation name.
func WithTracerOperationNameFormatter(operationNameFormatter OperationNameFormatter) TracerOption {
return func(p *tracer) {
p.operationNameFormatter = operationNameFormatter
}
}
// WithTracerSpanNameFormatter sets a function to customize the span name.
func WithTracerSpanNameFormatter(spanNameFormatter SpanNameFormatter) TracerOption {
return func(p *tracer) {
p.spanNameFormatter = spanNameFormatter
}
}