-
Notifications
You must be signed in to change notification settings - Fork 18
/
otel.go
246 lines (209 loc) · 6.23 KB
/
otel.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Package otel contains an o11y.Provider that emits open telemetry gRPC.
// N.B. This has not been tried against a production collector, so we need to
// try it out on a safe / non production traffic service.
package otel
import (
"context"
"fmt"
"os"
"strings"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"go.opentelemetry.io/otel/trace"
"github.com/circleci/ex/o11y"
"github.com/circleci/ex/o11y/otel/texttrace"
)
type OTel struct {
metricsProvider o11y.ClosableMetricsProvider
tracer trace.Tracer
tp *sdktrace.TracerProvider
}
func New(conf Config) (o11y.Provider, error) {
var exporter sdktrace.SpanExporter
exporter, err := texttrace.New(os.Stdout)
if err != nil {
return nil, err
}
if conf.GrpcHostAndPort != "" {
grpc, err := newGRPC(context.Background(), conf.GrpcHostAndPort, conf.Dataset)
if err != nil {
return nil, err
}
// use gRPC and text - mainly so acceptance testing can harvest the ports.
exporter = multipleExporter{
exporters: []sdktrace.SpanExporter{
exporter,
grpc,
},
}
}
res := resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(conf.Service),
semconv.ServiceVersionKey.String(conf.Version),
// This custom resource attribute is used by our honeycomb otel collector to route these traces
// to the correct dataset.
attribute.String("x-honeycomb-dataset", conf.Dataset),
)
bsp := sdktrace.NewBatchSpanProcessor(exporter)
tp := sdktrace.NewTracerProvider(
sdktrace.WithSpanProcessor(bsp),
sdktrace.WithSpanProcessor(globalFields),
sdktrace.WithResource(res),
)
// set the global options
otel.SetTracerProvider(tp)
propagator := propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{})
otel.SetTextMapPropagator(propagator)
// TODO check baggage is wired up above
mProv, err := metricsProvider(conf.Config)
if err != nil {
return nil, fmt.Errorf("metrics provider failed: %w", err)
}
return &OTel{
metricsProvider: mProv,
tp: tp,
tracer: otel.Tracer(""),
}, nil
}
func newGRPC(ctx context.Context, endpoint, dataset string) (*otlptrace.Exporter, error) {
opts := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(endpoint),
otlptracegrpc.WithInsecure(),
// This header may be used by honeycomb ingestion pathways in the future, but
// it is not currently needed for how the collectors are currently set up, which
// expect a resource attribute instead.
otlptracegrpc.WithHeaders(map[string]string{"x-honeycomb-dataset": dataset}),
}
return otlptrace.New(ctx, otlptracegrpc.NewClient(opts...))
}
var spanCtxKey = struct{}{}
// RawProvider satisfies an interface the helpers need
func (o *OTel) RawProvider() *OTel {
return o
}
func (o OTel) AddGlobalField(key string, val interface{}) {
mustValidateKey(key)
globalFields.addField(key, val)
}
func (o OTel) StartSpan(ctx context.Context, name string) (context.Context, o11y.Span) {
ctx, span := o.tracer.Start(ctx, name)
s := o.wrapSpan(span)
if s != nil {
ctx = context.WithValue(ctx, spanCtxKey, s)
}
return ctx, s
}
func (o OTel) GetSpan(ctx context.Context) o11y.Span {
if s, ok := ctx.Value(spanCtxKey).(*span); ok {
return s
}
return nil
}
func (o OTel) AddField(ctx context.Context, key string, val interface{}) {
trace.SpanFromContext(ctx).SetAttributes(attr(key, val))
}
func (o OTel) AddFieldToTrace(ctx context.Context, key string, val interface{}) {
// TODO - some equivalent to adding this field to all child spans to the root span
o.AddField(ctx, key, val)
}
func (o OTel) Log(ctx context.Context, name string, fields ...o11y.Pair) {
// TODO Log
}
func (o OTel) Close(ctx context.Context) {
// TODO Handle these errors in a sensible manner where possible
_ = o.tp.Shutdown(ctx)
if o.metricsProvider != nil {
_ = o.metricsProvider.Close()
}
}
func (o OTel) MetricsProvider() o11y.MetricsProvider {
return o.metricsProvider
}
func (o OTel) Helpers() o11y.Helpers {
return helpers{}
}
func (o OTel) wrapSpan(s trace.Span) *span {
if s == nil {
return nil
}
return &span{
metricsProvider: o.metricsProvider,
span: s,
start: time.Now(),
fields: map[string]interface{}{},
}
}
type span struct {
span trace.Span
metrics []o11y.Metric
metricsProvider o11y.ClosableMetricsProvider
start time.Time
fields map[string]interface{}
}
func (s *span) AddField(key string, val interface{}) {
s.AddRawField("app."+key, val)
}
func (s *span) AddRawField(key string, val interface{}) {
mustValidateKey(key)
s.fields[key] = val
if err, ok := val.(error); ok {
// s.span.RecordError() TODO - maybe this
val = err.Error()
}
// Use otel SetName if we are overriding the name attribute
// TODO - should we set the name attribute as well
if key == "name" {
if v, ok := val.(string); ok {
s.span.SetName(v)
}
}
s.span.SetAttributes(attr(key, val))
}
// RecordMetric will only emit a metric if End is called specifically
func (s *span) RecordMetric(metric o11y.Metric) {
s.metrics = append(s.metrics, metric)
}
func (s *span) End() {
s.sendMetric()
s.span.End()
}
func (s *span) sendMetric() {
if s.metricsProvider == nil {
return
}
// insert the expected field for any timing metric
s.fields["duration_ms"] = time.Since(s.start) / time.Millisecond
extractAndSendMetrics(s.metricsProvider)(s.metrics, s.fields)
}
func mustValidateKey(key string) {
if strings.Contains(key, "-") {
panic(fmt.Errorf("key %q cannot contain '-'", key))
}
}
type multipleExporter struct {
exporters []sdktrace.SpanExporter
}
func (m multipleExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
for _, e := range m.exporters {
if err := e.ExportSpans(ctx, spans); err != nil {
return err
}
}
return nil
}
func (m multipleExporter) Shutdown(ctx context.Context) error {
for _, e := range m.exporters {
if err := e.Shutdown(ctx); err != nil {
return err
}
}
return nil
}