-
Notifications
You must be signed in to change notification settings - Fork 2
/
sender_otel_honey.go
108 lines (93 loc) · 2.86 KB
/
sender_otel_honey.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
package main
import (
"context"
"fmt"
"math/rand"
"net/url"
"github.com/honeycombio/otel-config-go/otelconfig"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// make sure it implements Sender
var _ Sender = (*SenderOTel)(nil)
type OTelSendable struct {
trace.Span
}
func (s OTelSendable) Send() {
(trace.Span)(s).End()
}
type SenderOTel struct {
tracer trace.Tracer
shutdown func()
}
func otelTracesFromURL(u *url.URL) string {
target := fmt.Sprintf("%s://%s", u.Scheme, u.Host)
return target
}
type OtelLogger struct {
Logger
}
func (l OtelLogger) Debugf(format string, args ...interface{}) {
l.Logger.Debug(format, args...)
}
func (l OtelLogger) Fatalf(format string, args ...interface{}) {
l.Logger.Fatal(format, args...)
}
func NewSenderOTel(log Logger, opts *Options) *SenderOTel {
var protocol otelconfig.Protocol
switch opts.Output.Protocol {
case "grpc":
protocol = otelconfig.ProtocolGRPC
case "protobuf":
protocol = otelconfig.ProtocolHTTPProto
case "json":
protocol = otelconfig.ProtocolHTTPJSON
default:
log.Fatal("unknown protocol: %s", opts.Output.Protocol)
}
otelshutdown, err := otelconfig.ConfigureOpenTelemetry(
otelconfig.WithExporterProtocol(protocol),
otelconfig.WithServiceName(opts.Telemetry.Dataset),
otelconfig.WithTracesExporterEndpoint(otelTracesFromURL(opts.apihost)),
otelconfig.WithTracesExporterInsecure(opts.Telemetry.Insecure),
otelconfig.WithMetricsEnabled(false),
otelconfig.WithLogLevel(opts.Global.LogLevel),
otelconfig.WithLogger(OtelLogger{log}),
otelconfig.WithHeaders(map[string]string{
"x-honeycomb-team": opts.Telemetry.APIKey,
}),
)
if err != nil {
log.Fatal("failure configuring otel: %v", err)
}
return &SenderOTel{
tracer: otel.Tracer(ResourceLibrary, trace.WithInstrumentationVersion(ResourceVersion)),
shutdown: otelshutdown,
}
}
func (t *SenderOTel) Close() {
t.shutdown()
}
func (t *SenderOTel) CreateTrace(ctx context.Context, name string, fielder *Fielder, count int64) (context.Context, Sendable) {
ctx, root := t.tracer.Start(ctx, name)
fielder.AddFields(root, count, 0)
var ots OTelSendable
ots.Span = root
return ctx, ots
}
func (t *SenderOTel) CreateSpan(ctx context.Context, name string, level int, fielder *Fielder) (context.Context, Sendable) {
ctx, span := t.tracer.Start(ctx, name)
if rand.Intn(10) == 0 {
span.AddEvent("exception", trace.WithAttributes(
attribute.KeyValue{Key: "exception.type", Value: attribute.StringValue("error")},
attribute.KeyValue{Key: "exception.message", Value: attribute.StringValue("error message")},
attribute.KeyValue{Key: "exception.stacktrace", Value: attribute.StringValue("stacktrace")},
attribute.KeyValue{Key: "exception.escaped", Value: attribute.BoolValue(false)},
))
}
fielder.AddFields(span, 0, level)
var ots OTelSendable
ots.Span = span
return ctx, ots
}