-
Notifications
You must be signed in to change notification settings - Fork 1
/
span_processor.go
362 lines (326 loc) · 10.9 KB
/
span_processor.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package sentry
// Based on the original: github.com/getsentry/sentry-go/otel
import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
"time"
sdk "github.com/getsentry/sentry-go"
"go.bryk.io/pkg/errors"
"go.bryk.io/pkg/otel"
"go.opentelemetry.io/otel/attribute"
sdkTrace "go.opentelemetry.io/otel/sdk/trace"
semConv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.opentelemetry.io/otel/trace"
)
type sentrySpanProcessor struct {
hub *sdk.Hub
flushTimeout time.Duration
maxEvents int
errCodec errors.Codec
mu sync.Mutex
}
// Singleton instance of the Sentry span processor.
// At the moment we do not support multiple instances.
var sentrySpanProcessorInstance *sentrySpanProcessor
func newSentrySpanProcessor(hub *sdk.Hub, ft time.Duration, maxEvents int) sdkTrace.SpanProcessor {
if sentrySpanProcessorInstance != nil {
return sentrySpanProcessorInstance
}
sdk.AddGlobalEventProcessor(linkTraceContextToErrorEvent)
sentrySpanProcessorInstance := &sentrySpanProcessor{
hub: hub,
flushTimeout: ft,
maxEvents: maxEvents,
errCodec: errors.CodecJSON(false), // ! make this configurable
}
return sentrySpanProcessorInstance
}
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#onstart
func (ssp *sentrySpanProcessor) OnStart(parent context.Context, s sdkTrace.ReadWriteSpan) {
// Get OTEL span details
otelSpanContext := s.SpanContext()
otelSpanID := otelSpanContext.SpanID()
otelTraceID := otelSpanContext.TraceID()
otelParentSpanID := s.Parent().SpanID()
// Get Sentry parent span
var sentryParentSpan *sdk.Span
if otelSpanContext.IsValid() {
sentryParentSpan, _ = sentrySpanMap.Get(otelParentSpanID)
}
// Add child span
if sentryParentSpan != nil {
span := sentryParentSpan.StartChild(s.Name())
span.SpanID = sdk.SpanID(otelSpanID)
span.StartTime = s.StartTime()
span.Status = sdk.SpanStatusOK
sentrySpanMap.Set(otelSpanID, span)
return
}
// Create new parent transaction
traceParentContext := getTraceParentContext(parent)
transaction := sdk.StartTransaction(
parent,
s.Name(),
sdk.WithSpanSampled(traceParentContext.Sampled),
)
transaction.SpanID = sdk.SpanID(otelSpanID)
transaction.TraceID = sdk.TraceID(otelTraceID)
transaction.ParentSpanID = sdk.SpanID(otelParentSpanID)
transaction.StartTime = s.StartTime()
transaction.Status = sdk.SpanStatusOK
if dynamicSamplingContext, valid := parent.Value(dynamicSamplingContextKey{}).(sdk.DynamicSamplingContext); valid {
transaction.SetDynamicSamplingContext(dynamicSamplingContext)
}
sentrySpanMap.Set(otelSpanID, transaction)
}
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#onendspan
func (ssp *sentrySpanProcessor) OnEnd(s sdkTrace.ReadOnlySpan) {
// retrieve Sentry span
otelSpanID := s.SpanContext().SpanID()
sentrySpan, ok := sentrySpanMap.Get(otelSpanID)
if !ok || sentrySpan == nil {
return
}
// ignore Sentry requests; internally used to report spans
if isSentryRequestSpan(sentrySpan.Context(), s) {
sentrySpanMap.Delete(otelSpanID)
return
}
// https://docs.sentry.io/platforms/go/concurrency/
ssp.mu.Lock()
currentHub := ssp.hub.Clone()
ssp.mu.Unlock()
currentHub.ConfigureScope(func(scope *sdk.Scope) {
scope.SetContext("trace", traceContext(sentrySpan).Map())
})
// attach OTEL data to Sentry span
if sentrySpan.IsTransaction() {
updateTransactionWithOtelData(sentrySpan, s, currentHub)
} else {
updateSpanWithOtelData(sentrySpan, s)
}
// Add span events as bredcrumbs
for _, ev := range s.Events() {
if bc := asBreadcrumb(ev); bc != nil {
// > this currently show breadcrumbs only on the specific span
// > that generates them, for example when an error occurs. In
// > order to show them on the transaction, we need to add them
// > to the transaction scope as well.
currentHub.AddBreadcrumb(bc, nil)
}
}
// Report span error(s), if any
for _, ev := range s.Events() {
if err := extractError(ev, ssp.errCodec); err != nil {
currentHub.CaptureException(err)
}
}
// capture span
sentrySpan.Status = getStatus(s)
sentrySpan.EndTime = s.EndTime()
sentrySpan.Finish()
sentrySpanMap.Delete(otelSpanID)
}
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#shutdown-1
func (ssp *sentrySpanProcessor) Shutdown(ctx context.Context) error {
// ~ per the spec: "shutdown MUST include the effects of ForceFlush"
sentrySpanMap.Clear()
return ssp.ForceFlush(ctx)
}
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#forceflush-1
func (ssp *sentrySpanProcessor) ForceFlush(_ context.Context) error {
return flushSpanProcessor(ssp.hub, ssp.flushTimeout)
}
func flushSpanProcessor(hub *sdk.Hub, ft time.Duration) error {
defer hub.Flush(ft)
return nil
}
func getTraceParentContext(ctx context.Context) sdk.TraceParentContext {
traceParentContext, ok := ctx.Value(sentryTraceParentContextKey{}).(sdk.TraceParentContext)
if !ok {
traceParentContext.Sampled = sdk.SampledUndefined
}
return traceParentContext
}
func updateTransactionWithOtelData(transaction *sdk.Span, s sdkTrace.ReadOnlySpan, hub *sdk.Hub) {
// parse OTEL standard attributes
attributes := map[attribute.Key]string{}
resource := map[attribute.Key]string{}
for _, kv := range s.Attributes() {
attributes[kv.Key] = asString(kv.Value)
}
for _, kv := range s.Resource().Attributes() {
resource[kv.Key] = asString(kv.Value)
}
otelCtx := map[string]interface{}{"Resource": resource}
if len(attributes) > 0 {
otelCtx["Attributes"] = attributes
}
transaction.SetContext("Open Telemetry", otelCtx)
// get span attributes
spanAttributes := parseSpanAttributes(s)
transaction.Name = spanAttributes.Description
transaction.Op = spanAttributes.Op
transaction.Source = spanAttributes.Source
if spanAttributes.User != nil {
hub.Scope().SetUser(*spanAttributes.User)
}
}
func updateSpanWithOtelData(span *sdk.Span, s sdkTrace.ReadOnlySpan) {
spanAttributes := parseSpanAttributes(s)
span.Op = spanAttributes.Op
span.Description = spanAttributes.Description
span.SetData("otel.kind", s.SpanKind().String())
for _, kv := range s.Attributes() {
span.SetData(string(kv.Key), asString(kv.Value))
}
}
// Sentry event processor that attaches trace information to the error event.
//
// Caveat: `hint.Context` should contain a valid context populated by
// OpenTelemetry's span context.
func linkTraceContextToErrorEvent(event *sdk.Event, hint *sdk.EventHint) *sdk.Event {
if hint == nil || hint.Context == nil {
return event
}
// compare with the (unexported) sentry.transactionType
if event.Type == "transaction" {
return event
}
otelSpanContext := trace.SpanContextFromContext(hint.Context)
var sentrySpan *sdk.Span
if otelSpanContext.IsValid() {
sentrySpan, _ = sentrySpanMap.Get(otelSpanContext.SpanID())
}
if sentrySpan == nil {
return event
}
traceContext, found := event.Contexts["trace"]
if !found {
event.Contexts["trace"] = make(map[string]interface{})
traceContext = event.Contexts["trace"]
}
traceContext["trace_id"] = sentrySpan.TraceID.String()
traceContext["span_id"] = sentrySpan.SpanID.String()
traceContext["parent_span_id"] = sentrySpan.ParentSpanID.String()
return event
}
func isSentryRequestSpan(ctx context.Context, s sdkTrace.ReadOnlySpan) bool {
// Look for the `http.url` attribute
for _, attribute := range s.Attributes() {
if attribute.Key == semConv.HTTPURLKey {
return isSentryRequestURL(ctx, attribute.Value.AsString())
}
}
return false
}
func isSentryRequestURL(ctx context.Context, url string) bool {
// get current hub
hub := sdk.GetHubFromContext(ctx)
if hub == nil {
hub = sdk.CurrentHub()
if hub == nil {
return false
}
}
// get client
client := hub.Client()
if client == nil {
return false
}
// get DSN
dsn, err := sdk.NewDsn(client.Options().Dsn)
if err != nil {
return false
}
return strings.Contains(url, dsn.GetAPIURL().String())
}
// Event (s) can be used to register activity worth reporting; this
// usually describes an activity/tasks progression leading to a
// potential error condition.
//
// There are some special attributes you can add to events:
// - event.kind: set to "debug" if not provided
// - event.category: set to "event" if not provided
// - event.data: provides additional payload data, "nil" by default
// - event.level: set to "debug" if not provided. Defines event's severity;
// allowed values are, from highest to lowest: fatal, error, warning, info, and debug.
//
// event.kind values:
// - debug: typically a log message
// - info: provide additional details to help identify the root cause of an issue
// - query: describe and report database interactions
// - ui: a user interaction with your app's UI.
// - user: describe user interactions
// - transaction: describe a tracing event
// - error: error/warning occurring prior to a reported exception
// - navigation: `event.data` must include key `from` and `to`
// - http: http requests started from the app; `event.data` can include attributes
// such as: `method`, `url`, `status_code`, `reason`, `headers`, `cookies`
//
// https://develop.sentry.dev/sdk/event-payloads/breadcrumbs/#breadcrumb-types
func asBreadcrumb(ev sdkTrace.Event) *sdk.Breadcrumb {
// don't report exceptions as events; these will be reported independently
if ev.Name == "exception" {
return nil
}
attrs := otel.Attributes{}
attrs.Load(ev.Attributes)
kind := "debug"
level := "debug"
category := "event"
data := make(map[string]interface{})
if k, ok := attrs["event.kind"]; ok {
kind = fmt.Sprintf("%v", k)
}
if lvl, ok := attrs["event.level"]; ok {
level = fmt.Sprintf("%v", lvl)
}
if cat, ok := attrs["event.category"]; ok {
category = fmt.Sprintf("%v", cat)
}
if dt, ok := attrs["event.data"]; ok {
if payload, ok := dt.(string); ok {
_ = json.Unmarshal([]byte(payload), &data)
}
}
return &sdk.Breadcrumb{
Type: kind,
Category: category,
Message: ev.Name,
Data: data,
Level: getLevel(level),
Timestamp: ev.Time,
}
}
// Look for "exception.report" data in "exception" events to be
// restored and reported on Sentry.
func extractError(ev sdkTrace.Event, codec errors.Codec) error {
// only process exception events
if ev.Name != "exception" {
return nil
}
// look for error details in event attributes
attrs := otel.Attributes{}
attrs.Load(ev.Attributes)
errMSg := attrs.Get("exception.message") // simple error message
errPayload := attrs.Get("exception.report") // original error report
// no error report available but a simple error message;
// return simple formatted error
if errPayload == nil && errMSg != nil {
return errors.WithStackAt(fmt.Errorf("%s", errMSg), 4)
}
// attempt to recover error instance from report
payload, ok := errPayload.(string)
if !ok {
return nil // invalid error payload
}
ok, recErr := codec.Unmarshal([]byte(payload))
if !ok {
return nil // failed to decode error payload
}
return recErr // return recovered error
}