-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathSentryPropagationContextTests.cs
More file actions
237 lines (203 loc) · 10.3 KB
/
SentryPropagationContextTests.cs
File metadata and controls
237 lines (203 loc) · 10.3 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
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
namespace Sentry.Tests;
public class SentryPropagationContextTests
{
private class Fixture
{
public SentryId ActiveReplayId { get; } = SentryId.Create();
public IReplaySession InactiveReplaySession { get; }
public IReplaySession ActiveReplaySession { get; }
public Fixture()
{
ActiveReplaySession = Substitute.For<IReplaySession>();
ActiveReplaySession.ActiveReplayId.Returns(ActiveReplayId);
InactiveReplaySession = Substitute.For<IReplaySession>();
InactiveReplaySession.ActiveReplayId.Returns((SentryId?)null);
}
}
private readonly Fixture _fixture = new();
[Theory]
[InlineData(false)]
[InlineData(true)]
public void CopyConstructor_CreatesCopyWithReplayId(bool replaySessionIsActive)
{
var original = new SentryPropagationContext();
original.GetOrCreateDynamicSamplingContext(new SentryOptions { Dsn = ValidDsn }, _fixture.InactiveReplaySession);
var copy = new SentryPropagationContext(original);
Assert.Equal(original.TraceId, copy.TraceId);
Assert.Equal(original.SpanId, copy.SpanId);
Assert.Equal(original._dynamicSamplingContext!.Items.Count, copy._dynamicSamplingContext!.Items.Count);
foreach (var dscItem in original._dynamicSamplingContext!.Items)
{
if (dscItem.Key == "replay_id")
{
copy._dynamicSamplingContext!.Items["replay_id"].Should().Be(replaySessionIsActive
// We overwrite the replay_id when we have an active replay session
? _fixture.ActiveReplayId.ToString()
// Otherwise we propagate whatever was in the baggage header
: dscItem.Value);
}
else
{
copy._dynamicSamplingContext!.Items.Should()
.Contain(kvp => kvp.Key == dscItem.Key && kvp.Value == dscItem.Value);
}
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void GetOrCreateDynamicSamplingContext_DynamicSamplingContextIsNull_CreatesDynamicSamplingContext(bool replaySessionIsActive)
{
var options = new SentryOptions { Dsn = ValidDsn };
var propagationContext = new SentryPropagationContext();
Assert.Null(propagationContext._dynamicSamplingContext); // Sanity check
_ = propagationContext.GetOrCreateDynamicSamplingContext(options, replaySessionIsActive ? _fixture.ActiveReplaySession : _fixture.InactiveReplaySession);
Assert.NotNull(propagationContext._dynamicSamplingContext);
if (replaySessionIsActive)
{
// We add the replay_id automatically when we have an active replay session
Assert.Equal(_fixture.ActiveReplayId.ToString(), Assert.Contains("replay_id", propagationContext._dynamicSamplingContext.Items));
}
else
{
Assert.DoesNotContain("replay_id", propagationContext._dynamicSamplingContext.Items);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void GetOrCreateDynamicSamplingContext_DynamicSamplingContextIsNotNull_ReturnsSameDynamicSamplingContext(bool replaySessionIsActive)
{
var options = new SentryOptions { Dsn = ValidDsn };
var propagationContext = new SentryPropagationContext();
var firstDynamicSamplingContext = propagationContext.GetOrCreateDynamicSamplingContext(options, replaySessionIsActive ? _fixture.ActiveReplaySession : _fixture.InactiveReplaySession);
var secondDynamicSamplingContext = propagationContext.GetOrCreateDynamicSamplingContext(options, replaySessionIsActive ? _fixture.ActiveReplaySession : _fixture.InactiveReplaySession);
Assert.Same(firstDynamicSamplingContext, secondDynamicSamplingContext);
}
[Fact]
public void CreateFromHeaders_HeadersNull_CreatesPropagationContextWithTraceAndSpanAndReplayId()
{
var propagationContext = SentryPropagationContext.CreateFromHeaders(null, null, null, _fixture.ActiveReplaySession);
Assert.NotEqual(propagationContext.TraceId, SentryId.Empty);
Assert.NotEqual(propagationContext.SpanId, SpanId.Empty);
Assert.Null(propagationContext._dynamicSamplingContext);
}
[Fact]
public void CreateFromHeaders_TraceHeaderNotNull_CreatesPropagationContextFromTraceHeader()
{
var traceHeader = new SentryTraceHeader(SentryId.Create(), SpanId.Create(), null);
var propagationContext = SentryPropagationContext.CreateFromHeaders(null, traceHeader, null, _fixture.ActiveReplaySession);
Assert.Equal(traceHeader.TraceId, propagationContext.TraceId);
Assert.NotEqual(traceHeader.SpanId, propagationContext.SpanId); // Sanity check
Assert.Equal(traceHeader.SpanId, propagationContext.ParentSpanId);
Assert.Null(propagationContext._dynamicSamplingContext);
}
[Fact]
public void CreateFromHeaders_BaggageExistsButTraceHeaderNull_CreatesPropagationContextWithoutDynamicSamplingContext()
{
var baggageHeader = BaggageHeader.Create(new List<KeyValuePair<string, string>>
{
{ "sentry-sample_rate", "1.0" },
{ "sentry-trace_id", "75302ac48a024bde9a3b3734a82e36c8" },
{ "sentry-public_key", "d4d82fc1c2c4032a83f3a29aa3a3aff" },
{ "sentry-replay_id", "bfd31b89a59d41c99d96dc2baf840ecd" }
});
var propagationContext = SentryPropagationContext.CreateFromHeaders(null, null, baggageHeader, _fixture.InactiveReplaySession);
Assert.Null(propagationContext._dynamicSamplingContext);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void CreateFromHeaders_BaggageHeaderNotNull_CreatesPropagationContextWithDynamicSamplingContext(bool replaySessionIsActive)
{
// Arrange
var traceHeader = new SentryTraceHeader(SentryId.Create(), SpanId.Create(), null);
var baggageHeader = BaggageHeader.Create(new List<KeyValuePair<string, string>>
{
{ "sentry-sample_rate", "1.0" },
{ "sentry-sample_rand", "0.1234" },
{ "sentry-trace_id", "75302ac48a024bde9a3b3734a82e36c8" },
{ "sentry-public_key", "d4d82fc1c2c4032a83f3a29aa3a3aff" },
{ "sentry-replay_id", "bfd31b89a59d41c99d96dc2baf840ecd" }
});
var replaySession = replaySessionIsActive ? _fixture.ActiveReplaySession : _fixture.InactiveReplaySession;
// Act
var propagationContext = SentryPropagationContext.CreateFromHeaders(null, traceHeader, baggageHeader, replaySession);
// Assert
var dsc = propagationContext.GetOrCreateDynamicSamplingContext(new SentryOptions(), replaySession);
Assert.Equal(5, dsc.Items.Count);
if (replaySessionIsActive)
{
// We add the replay_id automatically when we have an active replay session
Assert.Equal(_fixture.ActiveReplayId.ToString(), Assert.Contains("replay_id", dsc.Items));
}
else
{
// Otherwise we inherit the replay_id from the baggage header
Assert.Equal("bfd31b89a59d41c99d96dc2baf840ecd", Assert.Contains("replay_id", dsc.Items));
}
}
// Decision matrix tests for ShouldContinueTrace
[Theory]
// strict=false cases
[InlineData("1", "1", false, true)] // matching orgs → continue
[InlineData(null, "1", false, true)] // baggage missing → continue
[InlineData("1", null, false, true)] // SDK missing → continue
[InlineData(null, null, false, true)] // both missing → continue
[InlineData("1", "2", false, false)] // mismatch → new trace
// strict=true cases
[InlineData("1", "1", true, true)] // matching orgs → continue
[InlineData(null, "1", true, false)] // baggage missing → new trace
[InlineData("1", null, true, false)] // SDK missing → new trace
[InlineData(null, null, true, true)] // both missing → continue
[InlineData("1", "2", true, false)] // mismatch → new trace
public void ShouldContinueTrace_DecisionMatrix(string baggageOrgId, string sdkOrgId, bool strict, bool expected)
{
var options = new SentryOptions
{
Dsn = ValidDsn,
StrictTraceContinuation = strict
};
if (sdkOrgId != null)
{
options.OrgId = sdkOrgId;
}
BaggageHeader baggage = null;
if (baggageOrgId != null)
{
baggage = BaggageHeader.TryParse($"sentry-trace_id=bc6d53f15eb88f4320054569b8c553d4,sentry-org_id={baggageOrgId}");
}
else
{
baggage = BaggageHeader.TryParse("sentry-trace_id=bc6d53f15eb88f4320054569b8c553d4");
}
var result = SentryPropagationContext.ShouldContinueTrace(options, baggage);
Assert.Equal(expected, result);
}
[Fact]
public void CreateFromHeaders_WithOrgMismatch_StartsNewTrace()
{
var options = new SentryOptions
{
Dsn = "https://key@o2.ingest.sentry.io/456",
StrictTraceContinuation = false
};
var traceHeader = SentryTraceHeader.Parse("bc6d53f15eb88f4320054569b8c553d4-b72fa28504b07285-1");
var baggage = BaggageHeader.TryParse("sentry-trace_id=bc6d53f15eb88f4320054569b8c553d4,sentry-org_id=1");
var propagationContext = SentryPropagationContext.CreateFromHeaders(null, traceHeader, baggage, _fixture.InactiveReplaySession, options);
Assert.NotEqual(SentryId.Parse("bc6d53f15eb88f4320054569b8c553d4"), propagationContext.TraceId);
}
[Fact]
public void CreateFromHeaders_WithOrgMatch_ContinuesTrace()
{
var options = new SentryOptions
{
Dsn = "https://key@o1.ingest.sentry.io/456",
StrictTraceContinuation = false
};
var traceHeader = SentryTraceHeader.Parse("bc6d53f15eb88f4320054569b8c553d4-b72fa28504b07285-1");
var baggage = BaggageHeader.TryParse("sentry-trace_id=bc6d53f15eb88f4320054569b8c553d4,sentry-org_id=1,sentry-public_key=key,sentry-sample_rate=1.0,sentry-sample_rand=0.5000,sentry-sampled=true");
var propagationContext = SentryPropagationContext.CreateFromHeaders(null, traceHeader, baggage, _fixture.InactiveReplaySession, options);
Assert.Equal(SentryId.Parse("bc6d53f15eb88f4320054569b8c553d4"), propagationContext.TraceId);
}
}