-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrace.go
103 lines (83 loc) · 3.15 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
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
package otelemetry
import (
"context"
"fmt"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
sdkresource "go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)
// Trace interface provides methods for tracing operations.
type Trace interface {
// Trace returns the original tracer.
Trace() trace.Tracer // original trace
// StartSpan starts a new span with the given name and options.
StartSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, Span)
// SpanFromContext retrieves the span from the given context.
SpanFromContext(ctx context.Context) Span
// ContextWithSpan returns a new context with the given span.
ContextWithSpan(ctx context.Context, span trace.Span) context.Context
// ContextWithRemoteSpanContext returns a new context with the remote span context.
ContextWithRemoteSpanContext(ctx context.Context, span trace.Span) context.Context
}
// oteltrace is an implementation of the Trace interface using OpenTelemetry.
type oteltrace struct {
trace trace.Tracer
}
func (t *oteltrace) Trace() trace.Tracer {
return t.trace
}
func (t *oteltrace) StartSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, Span) {
ctx, span := t.trace.Start(ctx, name, opts...)
return ctx, &otelspan{span: span}
}
func (t *oteltrace) SpanFromContext(ctx context.Context) Span {
span := trace.SpanFromContext(ctx)
if span == nil {
return nil
}
return &otelspan{span: span}
}
func (t *oteltrace) ContextWithSpan(ctx context.Context, span trace.Span) context.Context {
return trace.ContextWithSpan(ctx, span)
}
func (t *oteltrace) ContextWithRemoteSpanContext(ctx context.Context, span trace.Span) context.Context {
return trace.ContextWithRemoteSpanContext(ctx, span.SpanContext())
}
func newTraceProvider(ctx context.Context, otelAgentAddr string, res *sdkresource.Resource, opts TracerOptions) (*sdktrace.TracerProvider, error) {
client := otlptracegrpc.NewClient(traceClientOpts(otelAgentAddr, opts.ClientOption...)...)
exporter, err := otlptrace.New(ctx, client)
if err != nil {
return nil, err
}
bsp := sdktrace.NewBatchSpanProcessor(exporter, opts.BatchSpanProcessorOption...)
provider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(bsp),
)
return provider, nil
}
func newStdoutTraceProvider(res *sdkresource.Resource) (*sdktrace.TracerProvider, error) {
exporter, err := stdouttrace.New( /*stdouttrace.WithPrettyPrint()*/ )
if err != nil {
return nil, fmt.Errorf("creating stdout exporter: %w", err)
}
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(res),
)
return tracerProvider, nil
}
func traceClientOpts(otelAgentAddr string, opts ...otlptracegrpc.Option) []otlptracegrpc.Option {
options := []otlptracegrpc.Option{
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint(otelAgentAddr),
}
if len(opts) == 0 {
return options
}
return opts
}