-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrace.go
More file actions
137 lines (122 loc) · 4.15 KB
/
trace.go
File metadata and controls
137 lines (122 loc) · 4.15 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cmd
import (
"context"
"fmt"
"github.com/ettle/strcase"
"github.com/hamba/logger/v2"
"github.com/hamba/logger/v2/ctx"
"github.com/urfave/cli/v3"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
)
// Tracing flag constants declared for CLI use.
const (
FlagTracingExporter = "tracing.exporter"
FlagTracingEndpoint = "tracing.endpoint"
FlagTracingEndpointInsecure = "tracing.endpoint-insecure"
FlagTracingTags = "tracing.tags"
FlagTracingHeaders = "tracing.headers"
FlagTracingRatio = "tracing.ratio"
)
// CategoryTracing is the tracing flag category.
const CategoryTracing = "Tracing"
// TracingFlags are flags that configure tracing.
var TracingFlags = Flags{
&cli.StringFlag{
Name: FlagTracingExporter,
Category: CategoryTracing,
Usage: "The tracing backend. Supported: 'otlphttp', 'otlpgrpc'.",
Sources: cli.EnvVars(strcase.ToSNAKE(FlagTracingExporter)),
},
&cli.StringFlag{
Name: FlagTracingEndpoint,
Category: CategoryTracing,
Usage: "The tracing backend endpoint.",
Sources: cli.EnvVars(strcase.ToSNAKE(FlagTracingEndpoint)),
},
&cli.BoolFlag{
Name: FlagTracingEndpointInsecure,
Category: CategoryTracing,
Usage: "Determines if the endpoint is insecure.",
Sources: cli.EnvVars(strcase.ToSNAKE(FlagTracingEndpointInsecure)),
},
&cli.StringMapFlag{
Name: FlagTracingTags,
Category: CategoryTracing,
Usage: "A list of tags appended to every trace.",
Sources: cli.EnvVars(strcase.ToSNAKE(FlagTracingTags)),
},
&cli.StringMapFlag{
Name: FlagTracingHeaders,
Category: CategoryTracing,
Usage: "A list of headers appended to every trace when supported by the exporter.",
Sources: cli.EnvVars(strcase.ToSNAKE(FlagTracingHeaders)),
},
&cli.FloatFlag{
Name: FlagTracingRatio,
Category: CategoryTracing,
Usage: "The ratio between 0 and 1 of sample traces to take.",
Value: 0.5,
Sources: cli.EnvVars(strcase.ToSNAKE(FlagTracingRatio)),
},
}
// NewTracer returns a tracer configured from the cli.
func NewTracer(ctx context.Context, cmd *cli.Command, log *logger.Logger, attrs ...attribute.KeyValue) (*trace.TracerProvider, error) {
otel.SetErrorHandler(logErrorHandler{log: log})
exp, err := createExporter(ctx, cmd)
if err != nil {
return nil, err
}
if exp == nil {
return trace.NewTracerProvider(), nil
}
proc := trace.NewBatchSpanProcessor(exp)
ratio := cmd.Float(FlagTracingRatio)
sampler := trace.ParentBased(trace.TraceIDRatioBased(ratio))
if tags := cmd.StringMap(FlagTracingTags); len(tags) > 0 {
for k, v := range tags {
attrs = append(attrs, attribute.String(k, v))
}
}
return trace.NewTracerProvider(
trace.WithSampler(sampler),
trace.WithResource(resource.NewWithAttributes(semconv.SchemaURL, attrs...)),
trace.WithSpanProcessor(proc),
), nil
}
func createExporter(ctx context.Context, cmd *cli.Command) (trace.SpanExporter, error) {
backend := cmd.String(FlagTracingExporter)
endpoint := cmd.String(FlagTracingEndpoint)
switch backend {
case "":
return nil, nil //nolint:nilnil
case "otlphttp":
opts := []otlptracehttp.Option{otlptracehttp.WithEndpoint(endpoint), otlptracehttp.WithHeaders(cmd.StringMap(FlagTracingHeaders))}
if cmd.Bool(FlagTracingEndpointInsecure) {
opts = append(opts, otlptracehttp.WithInsecure())
}
return otlptracehttp.New(ctx, opts...)
case "otlpgrpc":
opts := []otlptracegrpc.Option{otlptracegrpc.WithEndpoint(endpoint), otlptracegrpc.WithHeaders(cmd.StringMap(FlagTracingHeaders))}
if cmd.Bool(FlagTracingEndpointInsecure) {
opts = append(opts, otlptracegrpc.WithInsecure())
}
return otlptracegrpc.New(ctx, opts...)
default:
return nil, fmt.Errorf("unsupported tracing backend %q", backend)
}
}
type logErrorHandler struct {
log *logger.Logger
}
func (l logErrorHandler) Handle(err error) {
if err == nil {
return
}
l.log.Error(err.Error(), ctx.Str("component", "otel"))
}