-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathspan_processor.go
161 lines (133 loc) · 4.95 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
//go:build go1.18
package sentryotel
import (
"context"
"time"
"github.com/getsentry/sentry-go"
"github.com/getsentry/sentry-go/otel/internal/utils"
"go.opentelemetry.io/otel/attribute"
otelSdkTrace "go.opentelemetry.io/otel/sdk/trace"
)
type sentrySpanProcessor struct{}
// Singleton instance of the Sentry span processor.
// At the moment we do not support multiple instances.
var sentrySpanProcessorInstance *sentrySpanProcessor
func NewSentrySpanProcessor() otelSdkTrace.SpanProcessor {
if sentrySpanProcessorInstance != nil {
return sentrySpanProcessorInstance
}
sentry.AddGlobalEventProcessor(linkTraceContextToErrorEvent)
sentrySpanProcessorInstance := &sentrySpanProcessor{}
return sentrySpanProcessorInstance
}
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#onstart
func (ssp *sentrySpanProcessor) OnStart(parent context.Context, s otelSdkTrace.ReadWriteSpan) {
otelSpanContext := s.SpanContext()
otelSpanID := otelSpanContext.SpanID()
otelTraceID := otelSpanContext.TraceID()
otelParentSpanID := s.Parent().SpanID()
var sentryParentSpan *sentry.Span
if otelSpanContext.IsValid() {
sentryParentSpan, _ = sentrySpanMap.Get(otelParentSpanID)
}
if sentryParentSpan != nil {
span := sentryParentSpan.StartChild(s.Name())
span.SpanID = sentry.SpanID(otelSpanID)
span.StartTime = s.StartTime()
sentrySpanMap.Set(otelSpanID, span)
} else {
traceParentContext := getTraceParentContext(parent)
transaction := sentry.StartTransaction(
parent,
s.Name(),
sentry.SpanSampled(traceParentContext.Sampled),
)
transaction.SpanID = sentry.SpanID(otelSpanID)
transaction.TraceID = sentry.TraceID(otelTraceID)
transaction.ParentSpanID = sentry.SpanID(otelParentSpanID)
transaction.StartTime = s.StartTime()
if dynamicSamplingContext, valid := parent.Value(dynamicSamplingContextKey{}).(sentry.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 otelSdkTrace.ReadOnlySpan) {
otelSpanId := s.SpanContext().SpanID()
sentrySpan, ok := sentrySpanMap.Get(otelSpanId)
if !ok || sentrySpan == nil {
return
}
if utils.IsSentryRequestSpan(sentrySpan.Context(), s) {
sentrySpanMap.Delete(otelSpanId)
return
}
if sentrySpan.IsTransaction() {
updateTransactionWithOtelData(sentrySpan, s)
} else {
updateSpanWithOtelData(sentrySpan, 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 {
sentrySpanMap.Clear()
// Note: according to the spec, "Shutdown MUST include the effects of ForceFlush".
return ssp.ForceFlush(ctx)
}
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#forceflush-1
func (ssp *sentrySpanProcessor) ForceFlush(ctx context.Context) error {
return flushSpanProcessor(ctx)
}
func flushSpanProcessor(ctx context.Context) error {
hub := sentry.GetHubFromContext(ctx)
// TODO(michi) should we make this configurable?
defer hub.Flush(2 * time.Second)
return nil
}
func getTraceParentContext(ctx context.Context) sentry.TraceParentContext {
traceParentContext, ok := ctx.Value(sentryTraceParentContextKey{}).(sentry.TraceParentContext)
if !ok {
traceParentContext.Sampled = sentry.SampledUndefined
}
return traceParentContext
}
func updateTransactionWithOtelData(transaction *sentry.Span, s otelSdkTrace.ReadOnlySpan) {
hub := sentry.GetHubFromContext(transaction.Context())
if hub == nil {
return
}
// TODO(michi) This is crazy inefficient
attributes := map[attribute.Key]string{}
resource := map[attribute.Key]string{}
for _, kv := range s.Attributes() {
attributes[kv.Key] = kv.Value.AsString()
}
for _, kv := range s.Resource().Attributes() {
resource[kv.Key] = kv.Value.AsString()
}
// TODO(michi) We might need to set this somewhere else then on the scope
hub.Scope().SetContext("otel", map[string]interface{}{
"attributes": attributes,
"resource": resource,
})
spanAttributes := utils.ParseSpanAttributes(s)
transaction.Status = utils.MapOtelStatus(s)
transaction.Op = spanAttributes.Op
transaction.Source = spanAttributes.Source
// TODO(michi) We might need to set this somewhere else then on the scope
hub.Scope().SetTransaction(spanAttributes.Description)
}
func updateSpanWithOtelData(span *sentry.Span, s otelSdkTrace.ReadOnlySpan) {
spanAttributes := utils.ParseSpanAttributes(s)
span.Status = utils.MapOtelStatus(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), kv.Value.AsString())
}
}