-
-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathSentryPropagationContext.cs
More file actions
77 lines (64 loc) · 2.74 KB
/
SentryPropagationContext.cs
File metadata and controls
77 lines (64 loc) · 2.74 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
using Sentry.Extensibility;
using Sentry.Internal;
namespace Sentry;
internal class SentryPropagationContext
{
public SentryId TraceId { get; }
public SpanId SpanId { get; }
public SpanId? ParentSpanId { get; }
internal DynamicSamplingContext? _dynamicSamplingContext;
public DynamicSamplingContext GetOrCreateDynamicSamplingContext(SentryOptions options, IReplaySession replaySession)
{
if (_dynamicSamplingContext is null)
{
options.LogDebug("Creating the Dynamic Sampling Context from the Propagation Context.");
_dynamicSamplingContext = this.CreateDynamicSamplingContext(options, replaySession);
}
return _dynamicSamplingContext;
}
internal SentryPropagationContext(
SentryId traceId,
SpanId parentSpanId,
DynamicSamplingContext? dynamicSamplingContext = null)
{
TraceId = traceId;
SpanId = SpanId.Create();
ParentSpanId = parentSpanId;
_dynamicSamplingContext = dynamicSamplingContext;
}
public SentryPropagationContext()
{
TraceId = SentryId.Create();
SpanId = SpanId.Create();
}
public SentryPropagationContext(SentryPropagationContext? other)
{
TraceId = other?.TraceId ?? SentryId.Create();
SpanId = other?.SpanId ?? SpanId.Create();
ParentSpanId = other?.ParentSpanId;
_dynamicSamplingContext = other?._dynamicSamplingContext;
}
public static SentryPropagationContext CreateFromHeaders(IDiagnosticLogger? logger, SentryTraceHeader? traceHeader, BaggageHeader? baggageHeader, IReplaySession replaySession, string? sdkOrgId = null)
{
logger?.LogDebug("Creating a propagation context from headers.");
if (traceHeader == null)
{
logger?.LogInfo("Sentry trace header is null. Creating new Sentry Propagation Context.");
return new SentryPropagationContext();
}
// Check for org ID mismatch between SDK configuration and incoming baggage
if (!string.IsNullOrEmpty(sdkOrgId) && baggageHeader is not null)
{
var sentryMembers = baggageHeader.GetSentryMembers();
if (sentryMembers.TryGetValue("org_id", out var baggageOrgId)
&& !string.IsNullOrEmpty(baggageOrgId)
&& sdkOrgId != baggageOrgId)
{
logger?.LogInfo("Org ID mismatch (SDK: {0}, baggage: {1}). Starting new trace.", sdkOrgId, baggageOrgId);
return new SentryPropagationContext();
}
}
var dynamicSamplingContext = baggageHeader?.CreateDynamicSamplingContext(replaySession);
return new SentryPropagationContext(traceHeader.TraceId, traceHeader.SpanId, dynamicSamplingContext);
}
}