-
Notifications
You must be signed in to change notification settings - Fork 3
/
trace.go
154 lines (138 loc) · 4.33 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package cmd
import (
"fmt"
"github.com/hamba/logger/v2"
"github.com/hamba/logger/v2/ctx"
"github.com/urfave/cli/v2"
"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/exporters/zipkin"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.17.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: 'zipkin', 'otlphttp', 'otlpgrpc'.",
EnvVars: []string{"TRACING_EXPORTER"},
},
&cli.StringFlag{
Name: FlagTracingEndpoint,
Category: CategoryTracing,
Usage: "The tracing backend endpoint.",
EnvVars: []string{"TRACING_ENDPOINT"},
},
&cli.BoolFlag{
Name: FlagTracingEndpointInsecure,
Category: CategoryTracing,
Usage: "Determines if the endpoint is insecure.",
EnvVars: []string{"TRACING_ENDPOINT_INSECURE"},
},
&cli.StringSliceFlag{
Name: FlagTracingTags,
Category: CategoryTracing,
Usage: "A list of tags appended to every trace. Format: key=value.",
EnvVars: []string{"TRACING_TAGS"},
},
&cli.StringSliceFlag{
Name: FlagTracingHeaders,
Category: CategoryTracing,
Usage: "A list of headers appended to every trace when supported by the exporter. Format: key=value.",
EnvVars: []string{"TRACING_HEADERS"},
},
&cli.Float64Flag{
Name: FlagTracingRatio,
Category: CategoryTracing,
Usage: "The ratio between 0 and 1 of sample traces to take.",
Value: 0.5,
EnvVars: []string{"TRACING_RATIO"},
},
}
// NewTracer returns a tracer configured from the cli.
func NewTracer(c *cli.Context, log *logger.Logger, resAttributes ...attribute.KeyValue) (*trace.TracerProvider, error) {
otel.SetErrorHandler(logErrorHandler{log: log})
exp, err := createExporter(c)
if err != nil {
return nil, err
}
if exp == nil {
return trace.NewTracerProvider(), nil
}
proc := trace.NewBatchSpanProcessor(exp)
ratio := c.Float64(FlagTracingRatio)
sampler := trace.ParentBased(trace.TraceIDRatioBased(ratio))
attrs := resAttributes
if tags := c.StringSlice(FlagTracingTags); len(tags) > 0 {
strTags, err := Split(tags, "=")
if err != nil {
return nil, err
}
for _, kv := range strTags {
attrs = append(attrs, attribute.String(kv[0], kv[1]))
}
}
return trace.NewTracerProvider(
trace.WithSampler(sampler),
trace.WithResource(resource.NewWithAttributes(semconv.SchemaURL, attrs...)),
trace.WithSpanProcessor(proc),
), nil
}
func createExporter(c *cli.Context) (trace.SpanExporter, error) {
backend := c.String(FlagTracingExporter)
endpoint := c.String(FlagTracingEndpoint)
var (
headers map[string]string
err error
)
if h := c.StringSlice(FlagTracingHeaders); len(h) > 0 {
headers, err = sliceToMap(h)
if err != nil {
return nil, err
}
}
switch backend {
case "":
return nil, nil //nolint:nilnil
case "zipkin":
return zipkin.New(endpoint)
case "otlphttp":
opts := []otlptracehttp.Option{otlptracehttp.WithEndpoint(endpoint), otlptracehttp.WithHeaders(headers)}
if c.Bool(FlagTracingEndpointInsecure) {
opts = append(opts, otlptracehttp.WithInsecure())
}
return otlptracehttp.New(c.Context, opts...)
case "otlpgrpc":
opts := []otlptracegrpc.Option{otlptracegrpc.WithEndpoint(endpoint), otlptracegrpc.WithHeaders(headers)}
if c.Bool(FlagTracingEndpointInsecure) {
opts = append(opts, otlptracegrpc.WithInsecure())
}
return otlptracegrpc.New(c.Context, 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"))
}