-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathspan_processor.go
More file actions
160 lines (136 loc) · 5.2 KB
/
span_processor.go
File metadata and controls
160 lines (136 loc) · 5.2 KB
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
package sentryotel
import (
"context"
"time"
"github.com/getsentry/sentry-go"
"github.com/getsentry/sentry-go/otel/internal/common"
"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
// NewSentrySpanProcessor creates an OpenTelemetry span processor that mirrors
// OTel spans into Sentry's native span model.
//
// Deprecated: Prefer OTLP export via sentryotlp.NewTraceExporter.
// For collector-based setups, use the standard OTel exporter and register
// sentryotel.NewOtelIntegration for linking.
// Will be removed in 0.47.0.
func NewSentrySpanProcessor() otelSdkTrace.SpanProcessor {
if sentrySpanProcessorInstance != nil {
return sentrySpanProcessorInstance
}
sentry.AddGlobalEventProcessor(common.NewEventProcessor())
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()
txn, ok := sentrySpanMap.GetTransaction(otelTraceID)
if ok && txn.HasSpan(otelParentSpanID) {
span := txn.root.StartChild(s.Name())
span.SpanID = sentry.SpanID(otelSpanID)
span.ParentSpanID = sentry.SpanID(otelParentSpanID)
span.StartTime = s.StartTime()
sentrySpanMap.Set(otelSpanID, span, otelTraceID)
return
}
traceParentContext := getTraceParentContext(parent)
transaction := sentry.StartTransaction(
parent,
s.Name(),
sentry.WithSpanSampled(traceParentContext.Sampled),
)
transaction.SpanID = sentry.SpanID(otelSpanID)
transaction.TraceID = sentry.TraceID(otelTraceID)
transaction.ParentSpanID = sentry.SpanID(otelParentSpanID)
transaction.StartTime = s.StartTime()
if dsc, valid := parent.Value(dynamicSamplingContextKey{}).(sentry.DynamicSamplingContext); valid {
transaction.SetDynamicSamplingContext(dsc)
}
sentrySpanMap.Set(otelSpanID, transaction, otelTraceID)
}
// 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()
otelTraceID := s.SpanContext().TraceID()
sentrySpan, ok := sentrySpanMap.Get(otelTraceID, otelSpanID)
if !ok || sentrySpan == nil {
return
}
if utils.IsSentryRequestSpan(sentrySpan.Context(), s) {
sentrySpanMap.MarkFinished(otelSpanID, otelTraceID)
return
}
if sentrySpan.IsTransaction() {
updateTransactionWithOtelData(sentrySpan, s)
} else {
updateSpanWithOtelData(sentrySpan, s)
}
sentrySpan.EndTime = s.EndTime()
sentrySpan.Finish()
sentrySpanMap.MarkFinished(otelSpanID, otelTraceID)
}
// 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)
if hub == nil {
hub = sentry.CurrentHub()
}
// 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) {
// TODO(michi) This is crazy inefficient
attributes := map[attribute.Key]interface{}{}
resource := map[attribute.Key]interface{}{}
for _, kv := range s.Attributes() {
attributes[kv.Key] = kv.Value.AsInterface()
}
for _, kv := range s.Resource().Attributes() {
resource[kv.Key] = kv.Value.AsInterface()
}
transaction.SetContext("otel", map[string]interface{}{
"attributes": attributes,
"resource": resource,
})
spanAttributes := utils.ParseSpanAttributes(s)
transaction.Status = utils.MapOtelStatus(s)
transaction.Name = spanAttributes.Description
transaction.Op = spanAttributes.Op
transaction.Source = spanAttributes.Source
}
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.AsInterface())
}
}