-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
SentrySpanProcessor.cs
358 lines (308 loc) · 14.9 KB
/
SentrySpanProcessor.cs
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
using OpenTelemetry;
using Sentry.Extensibility;
using Sentry.Internal;
using Sentry.Internal.Extensions;
using Sentry.Internal.OpenTelemetry;
namespace Sentry.OpenTelemetry;
/// <summary>
/// Sentry span processor for Open Telemetry.
/// </summary>
public class SentrySpanProcessor : BaseProcessor<Activity>
{
private readonly IHub _hub;
private readonly IEnumerable<IOpenTelemetryEnricher> _enrichers;
// ReSharper disable once MemberCanBePrivate.Global - Used by tests
internal readonly ConcurrentDictionary<ActivitySpanId, ISpan> _map = new();
private readonly SentryOptions? _options;
private readonly Lazy<IDictionary<string, object>> _resourceAttributes;
/// <summary>
/// Constructs a <see cref="SentrySpanProcessor"/>.
/// </summary>
public SentrySpanProcessor() : this(SentrySdk.CurrentHub)
{
}
/// <summary>
/// Constructs a <see cref="SentrySpanProcessor"/>.
/// </summary>
public SentrySpanProcessor(IHub hub) : this(hub, null)
{
}
internal SentrySpanProcessor(IHub hub, IEnumerable<IOpenTelemetryEnricher>? enrichers)
{
_hub = hub;
_enrichers = enrichers ?? Enumerable.Empty<IOpenTelemetryEnricher>();
_options = hub.GetSentryOptions();
if (_options is not { })
{
throw new InvalidOperationException(
"The Sentry SDK has not been initialised. To use Sentry with OpenTelemetry tracing you need to " +
"initialize the Sentry SDK.");
}
if (_options.Instrumenter != Instrumenter.OpenTelemetry)
{
throw new InvalidOperationException(
"OpenTelemetry has not been configured on the Sentry SDK. To use OpenTelemetry tracing you need " +
"to initialize the Sentry SDK with options.UseOpenTelemetry()");
}
// Resource attributes are consistent between spans, but not available during construction.
// Thus, get a single instance lazily.
_resourceAttributes = new Lazy<IDictionary<string, object>>(() =>
ParentProvider?.GetResource().Attributes.ToDictionary() ?? new Dictionary<string, object>(0));
}
/// <inheritdoc />
public override void OnStart(Activity data)
{
if (data.ParentSpanId != default && _map.TryGetValue(data.ParentSpanId, out var parentSpan))
{
// We can find the parent span - start a child span.
var context = new SpanContext(
data.OperationName,
data.SpanId.AsSentrySpanId(),
data.ParentSpanId.AsSentrySpanId(),
data.TraceId.AsSentryId(),
data.DisplayName,
null,
null)
{
Instrumenter = Instrumenter.OpenTelemetry
};
var span = (SpanTracer)parentSpan.StartChild(context);
span.StartTimestamp = data.StartTimeUtc;
_map[data.SpanId] = span;
}
else
{
// If a parent exists at all, then copy its sampling decision.
bool? isSampled = data.HasRemoteParent ? data.Recorded : null;
// No parent span found - start a new transaction
var transactionContext = new TransactionContext(data.DisplayName,
data.OperationName,
data.SpanId.AsSentrySpanId(),
data.ParentSpanId.AsSentrySpanId(),
data.TraceId.AsSentryId(),
data.DisplayName, null, isSampled, isSampled)
{
Instrumenter = Instrumenter.OpenTelemetry
};
var baggageHeader = data.Baggage.AsBaggageHeader();
var dynamicSamplingContext = baggageHeader.CreateDynamicSamplingContext();
var transaction = (TransactionTracer)_hub.StartTransaction(
transactionContext, new Dictionary<string, object?>(), dynamicSamplingContext
);
transaction.StartTimestamp = data.StartTimeUtc;
_map[data.SpanId] = transaction;
}
}
/// <inheritdoc />
public override void OnEnd(Activity data)
{
// Make a dictionary of the attributes (aka "tags") for faster lookup when used throughout the processor.
var attributes = data.TagObjects.ToDictionary();
if (attributes.TryGetTypedValue("http.url", out string? url) && (_options?.IsSentryRequest(url) ?? false))
{
_options?.DiagnosticLogger?.LogDebug($"Ignoring Activity {data.SpanId} for Sentry request.");
if (_map.TryRemove(data.SpanId, out var removed))
{
if (removed is SpanTracer spanTracerToRemove)
{
spanTracerToRemove.IsSentryRequest = true;
}
if (removed is TransactionTracer transactionTracer)
{
transactionTracer.IsSentryRequest = true;
}
}
return;
}
if (!_map.TryGetValue(data.SpanId, out var span))
{
_options?.DiagnosticLogger?.LogError($"Span not found for SpanId: {data.SpanId}. Did OnStart run? We might have a bug in the SDK.");
return;
}
var (operation, description, source) = ParseOtelSpanDescription(data, attributes);
span.Operation = operation;
span.Description = description;
if (span is TransactionTracer transaction)
{
transaction.Name = description;
transaction.NameSource = source;
// Use the end timestamp from the activity data.
transaction.EndTimestamp = data.StartTimeUtc + data.Duration;
// Transactions set otel attributes (and resource attributes) as context.
transaction.Contexts["otel"] = GetOtelContext(attributes);
}
else
{
// Use the end timestamp from the activity data.
((SpanTracer)span).EndTimestamp = data.StartTimeUtc + data.Duration;
// Spans set otel attributes in extras (passed to Sentry as "data" on the span).
// Resource attributes do not need to be set, as they would be identical as those set on the transaction.
span.SetExtras(attributes);
span.SetExtra("otel.kind", data.Kind);
}
GenerateSentryErrorsFromOtelSpan(data, attributes);
var status = GetSpanStatus(data.Status, attributes);
foreach (var enricher in _enrichers)
{
enricher.Enrich(span, data, _hub, _options);
}
span.Finish(status);
_map.TryRemove(data.SpanId, out _);
}
internal static SpanStatus GetSpanStatus(ActivityStatusCode status, IDictionary<string, object?> attributes)
{
// See https://github.com/open-telemetry/opentelemetry-dotnet/discussions/4703
if (attributes.TryGetValue(OtelSpanAttributeConstants.StatusCodeKey, out var statusCode)
&& statusCode is OtelStatusTags.ErrorStatusCodeTagValue
)
{
return GetErrorSpanStatus(attributes);
}
return status switch {
ActivityStatusCode.Unset => SpanStatus.Ok,
ActivityStatusCode.Ok => SpanStatus.Ok,
ActivityStatusCode.Error => GetErrorSpanStatus(attributes),
_ => SpanStatus.UnknownError
};
}
private static SpanStatus GetErrorSpanStatus(IDictionary<string, object?> attributes)
{
if (attributes.TryGetTypedValue("http.status_code", out int httpCode))
{
return SpanStatusConverter.FromHttpStatusCode(httpCode);
}
if (attributes.TryGetTypedValue("rpc.grpc.status_code", out int grpcCode))
{
return SpanStatusConverter.FromGrpcStatusCode(grpcCode);
}
return SpanStatus.UnknownError;
}
private static (string operation, string description, TransactionNameSource source) ParseOtelSpanDescription(
Activity activity,
IDictionary<string, object?> attributes)
{
// This function should loosely match the JavaScript implementation at:
// https://github.com/getsentry/sentry-javascript/blob/3487fa3af7aa72ac7fdb0439047cb7367c591e77/packages/opentelemetry-node/src/utils/parseOtelSpanDescription.ts
// However, it should also follow the OpenTelemetry semantic conventions specification, as indicated.
// HTTP span
// https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/http/
if (attributes.TryGetTypedValue(OtelSemanticConventions.AttributeHttpMethod, out string httpMethod))
{
if (activity.Kind == ActivityKind.Client)
{
// Per OpenTelemetry spec, client spans use only the method.
return ("http.client", httpMethod, TransactionNameSource.Custom);
}
if (attributes.TryGetTypedValue(OtelSemanticConventions.AttributeHttpRoute, out string httpRoute))
{
// A route exists. Use the method and route.
return ("http.server", $"{httpMethod} {httpRoute}", TransactionNameSource.Route);
}
if (attributes.TryGetTypedValue(OtelSemanticConventions.AttributeHttpTarget, out string httpTarget))
{
// A target exists. Use the method and target. If the target is "/" we can treat it like a route.
var source = httpTarget == "/" ? TransactionNameSource.Route : TransactionNameSource.Url;
return ("http.server", $"{httpMethod} {httpTarget}", source);
}
// Some other type of HTTP server span. Pass it through with the original name.
return ("http.server", activity.DisplayName, TransactionNameSource.Custom);
}
// DB span
// https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/database/
if (attributes.ContainsKey(OtelSemanticConventions.AttributeDbSystem))
{
if (attributes.TryGetTypedValue(OtelSemanticConventions.AttributeDbStatement, out string dbStatement))
{
// We have a database statement. Use it.
return ("db", dbStatement, TransactionNameSource.Task);
}
// Some other type of DB span. Pass it through with the original name.
return ("db", activity.DisplayName, TransactionNameSource.Task);
}
// RPC span
// https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/rpc/
if (attributes.ContainsKey(OtelSemanticConventions.AttributeRpcService))
{
return ("rpc", activity.DisplayName, TransactionNameSource.Route);
}
// Messaging span
// https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/messaging/
if (attributes.ContainsKey(OtelSemanticConventions.AttributeMessagingSystem))
{
return ("message", activity.DisplayName, TransactionNameSource.Route);
}
// FaaS (Functions/Lambda) span
// https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/faas/
if (attributes.TryGetTypedValue(OtelSemanticConventions.AttributeFaasTrigger, out string faasTrigger))
{
return (faasTrigger, activity.DisplayName, TransactionNameSource.Route);
}
// Default - pass through unmodified.
return (activity.OperationName, activity.DisplayName, TransactionNameSource.Custom);
}
private Dictionary<string, object?> GetOtelContext(IDictionary<string, object?> attributes)
{
var otelContext = new Dictionary<string, object?>();
if (attributes.Count > 0)
{
otelContext.Add("attributes", attributes);
}
var resourceAttributes = _resourceAttributes.Value;
if (resourceAttributes.Count > 0)
{
otelContext.Add("resource", resourceAttributes);
}
return otelContext;
}
private void GenerateSentryErrorsFromOtelSpan(Activity activity, IDictionary<string, object?> spanAttributes)
{
// https://develop.sentry.dev/sdk/performance/opentelemetry/#step-7-define-generatesentryerrorsfromotelspan
// https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/exceptions/
foreach (var @event in activity.Events.Where(e => e.Name == OtelSemanticConventions.AttributeExceptionEventName))
{
var eventAttributes = @event.Tags.ToDictionary();
// This would be where we would ideally implement full exception capture. That's not possible at the
// moment since the full exception isn't yet available via the OpenTelemetry API.
// See https://github.com/open-telemetry/opentelemetry-dotnet/issues/2439#issuecomment-1577314568
// if (!eventAttributes.TryGetTypedValue("exception", out Exception exception))
// {
// continue;
// }
// At the moment, OTEL only gives us `exception.type`, `exception.message`, and `exception.stacktrace`...
// So the best we can do is a poor man's exception (no accurate symbolication or anything)
if (!eventAttributes.TryGetTypedValue(OtelSemanticConventions.AttributeExceptionType, out string exceptionType))
{
continue;
}
eventAttributes.TryGetTypedValue(OtelSemanticConventions.AttributeExceptionMessage, out string message);
eventAttributes.TryGetTypedValue(OtelSemanticConventions.AttributeExceptionStacktrace, out string stackTrace);
Exception exception;
try
{
var type = Type.GetType(exceptionType)!;
exception = (Exception)Activator.CreateInstance(type, message)!;
exception.SetSentryMechanism("SentrySpanProcessor.ErrorSpan");
}
catch
{
_options?.DiagnosticLogger?.LogError($"Failed to create poor man's exception for type : {exceptionType}");
continue;
}
// TODO: Validate that our `DuplicateEventDetectionEventProcessor` prevents this from doubling exceptions
// that are also caught by other means, such as our AspNetCore middleware, etc.
// (When options.RecordException = true is set on AddAspNetCoreInstrumentation...)
// Also, in such cases - how will we get the otel scope and trace context on the other one?
var sentryEvent = new SentryEvent(exception, @event.Timestamp);
var otelContext = GetOtelContext(spanAttributes);
otelContext.Add("stack_trace", stackTrace);
sentryEvent.Contexts["otel"] = otelContext;
_hub.CaptureEvent(sentryEvent, scope =>
{
var trace = scope.Contexts.Trace;
trace.SpanId = activity.SpanId.AsSentrySpanId();
trace.ParentSpanId = activity.ParentSpanId.AsSentrySpanId();
trace.TraceId = activity.TraceId.AsSentryId();
});
}
}
}