-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.go
145 lines (119 loc) · 3.88 KB
/
tracing.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
package sdp
import (
"context"
"fmt"
"runtime/debug"
"connectrpc.com/connect"
"github.com/getsentry/sentry-go"
"github.com/nats-io/nats.go"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
"go.opentelemetry.io/otel/trace"
)
const (
instrumentationName = "github.com/overmindtech/sdp-go/sdp"
instrumentationVersion = "0.0.1"
)
var tracer = otel.GetTracerProvider().Tracer(
instrumentationName,
trace.WithInstrumentationVersion(instrumentationVersion),
trace.WithSchemaURL(semconv.SchemaURL),
)
type CtxMsgHandler func(ctx context.Context, msg *nats.Msg)
func NewOtelExtractingHandler(spanName string, h CtxMsgHandler, t trace.Tracer, spanOpts ...trace.SpanStartOption) nats.MsgHandler {
if h == nil {
return nil
}
return func(msg *nats.Msg) {
ctx := context.Background()
ctx = otel.GetTextMapPropagator().Extract(ctx, propagation.HeaderCarrier(msg.Header))
// don't start a span when we have no spanName
if spanName != "" {
var span trace.Span
ctx, span = t.Start(ctx, spanName, spanOpts...)
defer span.End()
}
h(ctx, msg)
}
}
func NewAsyncOtelExtractingHandler(spanName string, h CtxMsgHandler, t trace.Tracer, spanOpts ...trace.SpanStartOption) nats.MsgHandler {
if h == nil {
return nil
}
return func(msg *nats.Msg) {
go func() {
defer sentry.Recover()
ctx := context.Background()
ctx = otel.GetTextMapPropagator().Extract(ctx, propagation.HeaderCarrier(msg.Header))
// don't start a span when we have no spanName
if spanName != "" {
var span trace.Span
ctx, span = t.Start(ctx, spanName, spanOpts...)
defer span.End()
}
h(ctx, msg)
}()
}
}
func InjectOtelTraceContext(ctx context.Context, msg *nats.Msg) {
if msg.Header == nil {
msg.Header = make(nats.Header)
}
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(msg.Header))
}
type sentryInterceptor struct{}
// NewSentryInterceptor pass this to connect handlers as `connect.WithInterceptors(NewSentryInterceptor())` to recover from panics in the handler and report them to sentry. Otherwise panics get recovered by connect-go itself and do not get reported to sentry.
func NewSentryInterceptor() connect.Interceptor {
return &sentryInterceptor{}
}
func (i *sentryInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
// Same as previous UnaryInterceptorFunc.
return connect.UnaryFunc(func(
ctx context.Context,
req connect.AnyRequest,
) (connect.AnyResponse, error) {
defer sentry.Recover()
return next(ctx, req)
})
}
func (*sentryInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
return connect.StreamingClientFunc(func(
ctx context.Context,
spec connect.Spec,
) connect.StreamingClientConn {
defer sentry.Recover()
conn := next(ctx, spec)
return conn
})
}
func (i *sentryInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
return connect.StreamingHandlerFunc(func(
ctx context.Context,
conn connect.StreamingHandlerConn,
) error {
defer sentry.Recover()
return next(ctx, conn)
})
}
// LogRecoverToReturn Recovers from a panic, logs and forwards it sentry and otel, then returns
// Does nothing when there is no panic.
func LogRecoverToReturn(ctx context.Context, loc string) {
err := recover()
if err == nil {
return
}
stack := string(debug.Stack())
sentry.CurrentHub().Recover(err)
msg := fmt.Sprintf("unhandled panic in %v: %v", loc, err)
if ctx != nil {
span := trace.SpanFromContext(ctx)
span.SetAttributes(attribute.String("ovm.panic.loc", loc))
span.SetAttributes(attribute.String("ovm.panic.stack", stack))
log.WithContext(ctx).WithFields(log.Fields{"loc": loc, "stack": stack}).Error(msg)
} else {
log.WithFields(log.Fields{"loc": loc, "stack": stack}).Error(msg)
}
}