Skip to content

Commit b8d9452

Browse files
Set replay_id on DSC rather than directly on the event Context
1 parent 9872376 commit b8d9452

12 files changed

+76
-11
lines changed

src/Sentry/DynamicSamplingContext.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ private DynamicSamplingContext(
2828
double? sampleRand = null,
2929
string? release = null,
3030
string? environment = null,
31-
string? transactionName = null)
31+
string? transactionName = null,
32+
SentryId? replayId = null
33+
)
3234
{
3335
// Validate and set required values
3436
if (traceId == SentryId.Empty)
@@ -88,6 +90,11 @@ private DynamicSamplingContext(
8890
items.Add("transaction", transactionName);
8991
}
9092

93+
if (replayId is not null && replayId.Value != SentryId.Empty)
94+
{
95+
items.Add("replay_id", replayId.Value.ToString());
96+
}
97+
9198
Items = items;
9299
}
93100

@@ -156,6 +163,7 @@ public static DynamicSamplingContext CreateFromTransaction(TransactionTracer tra
156163
var sampleRate = transaction.SampleRate!.Value;
157164
var sampleRand = transaction.SampleRand;
158165
var transactionName = transaction.NameSource.IsHighQuality() ? transaction.Name : null;
166+
var replayId = transaction.Contexts.Replay.ReplayId;
159167

160168
// These two may not have been set yet on the transaction, but we can get them directly.
161169
var release = options.SettingLocator.GetRelease();
@@ -169,7 +177,8 @@ public static DynamicSamplingContext CreateFromTransaction(TransactionTracer tra
169177
sampleRand,
170178
release,
171179
environment,
172-
transactionName);
180+
transactionName,
181+
replayId);
173182
}
174183

175184
public static DynamicSamplingContext CreateFromPropagationContext(SentryPropagationContext propagationContext, SentryOptions options)
@@ -178,13 +187,15 @@ public static DynamicSamplingContext CreateFromPropagationContext(SentryPropagat
178187
var publicKey = options.ParsedDsn.PublicKey;
179188
var release = options.SettingLocator.GetRelease();
180189
var environment = options.SettingLocator.GetEnvironment();
190+
var replayId = ReplayHelper.GetReplayId();
181191

182192
return new DynamicSamplingContext(
183193
traceId,
184194
publicKey,
185195
null,
186196
release: release,
187-
environment: environment);
197+
environment: environment,
198+
replayId: replayId);
188199
}
189200
}
190201

src/Sentry/Internal/Hub.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ internal ITransactionTracer StartTransaction(
134134
: SampleRandHelper.GenerateSampleRand(context.TraceId.ToString())
135135
};
136136

137+
transaction.Contexts.Replay.ReplayId = ReplayHelper.GetReplayId();
138+
137139
// If the hub is disabled, we will always sample out. In other words, starting a transaction
138140
// after disposing the hub will result in that transaction not being sent to Sentry.
139141
// Additionally, we will always sample out if tracing is explicitly disabled.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#if __ANDROID__
2+
using Sentry.Android.Extensions;
3+
#endif
4+
5+
namespace Sentry.Internal;
6+
7+
internal static class ReplayHelper
8+
{
9+
internal static SentryId? GetReplayId()
10+
{
11+
#if __ANDROID__
12+
// Check to see if a Replay ID is available
13+
var replayId = JavaSdk.ScopesAdapter.Instance?.Options?.ReplayController?.ReplayId?.ToSentryId();
14+
return (replayId is { } id && id != SentryId.Empty) ? id : null;
15+
#else
16+
return null;
17+
#endif
18+
}
19+
}

src/Sentry/Platforms/Android/AndroidEventProcessor.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ public SentryEvent Process(SentryEvent @event)
3535
// Copy more information from the Android SDK
3636
if (_androidProcessor is { } androidProcessor)
3737
{
38-
// TODO: We should really do this when creating the DSC, so that it gets propagated correctly
39-
// Check to see if a Replay ID is available
40-
var activeReplayId = Sentry.JavaSdk.ScopesAdapter.Instance?.Options?.ReplayController?.ReplayId?.ToSentryId();
41-
if (activeReplayId is { } replayId && replayId != SentryId.Empty)
42-
{
43-
@event.Contexts.Replay.ReplayId = replayId;
44-
}
45-
4638
// TODO: Can we gather more data directly and remove this?
4739

4840
// Run a fake event through the Android processor, so we can get context info from the Android SDK.

test/Sentry.AspNetCore.Tests/WebIntegrationTests.Versioning.DotNet8_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
QueryString:
1818
},
1919
Contexts: {
20+
replay: {},
2021
trace: {
2122
Operation: http.server,
2223
Origin: auto.http.aspnetcore,

test/Sentry.AspNetCore.Tests/WebIntegrationTests.Versioning.DotNet9_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
QueryString:
1818
},
1919
Contexts: {
20+
replay: {},
2021
trace: {
2122
Operation: http.server,
2223
Origin: auto.http.aspnetcore,

test/Sentry.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ namespace Sentry
504504
public object this[string key] { get; set; }
505505
public System.Collections.Generic.ICollection<string> Keys { get; }
506506
public Sentry.Protocol.OperatingSystem OperatingSystem { get; }
507+
public Sentry.Protocol.Replay Replay { get; }
507508
public Sentry.Protocol.Response Response { get; }
508509
public Sentry.Protocol.Runtime Runtime { get; }
509510
public Sentry.Protocol.Trace Trace { get; }
@@ -1754,6 +1755,17 @@ namespace Sentry.Protocol
17541755
public void WriteTo(System.Text.Json.Utf8JsonWriter writer, Sentry.Extensibility.IDiagnosticLogger? _) { }
17551756
public static Sentry.Protocol.OperatingSystem FromJson(System.Text.Json.JsonElement json) { }
17561757
}
1758+
public sealed class Replay : Sentry.ISentryJsonSerializable
1759+
{
1760+
public const string Type = "replay";
1761+
public Replay() { }
1762+
public Sentry.SentryId? ReplayId { get; set; }
1763+
public Sentry.Protocol.Replay Clone() { }
1764+
public void UpdateFrom(Sentry.Protocol.Replay source) { }
1765+
public void UpdateFrom(object source) { }
1766+
public void WriteTo(System.Text.Json.Utf8JsonWriter writer, Sentry.Extensibility.IDiagnosticLogger? logger) { }
1767+
public static Sentry.Protocol.Replay FromJson(System.Text.Json.JsonElement json) { }
1768+
}
17571769
public sealed class Response : Sentry.ISentryJsonSerializable
17581770
{
17591771
public const string Type = "response";

test/Sentry.Tests/ApiApprovalTests.Run.DotNet9_0.verified.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ namespace Sentry
504504
public object this[string key] { get; set; }
505505
public System.Collections.Generic.ICollection<string> Keys { get; }
506506
public Sentry.Protocol.OperatingSystem OperatingSystem { get; }
507+
public Sentry.Protocol.Replay Replay { get; }
507508
public Sentry.Protocol.Response Response { get; }
508509
public Sentry.Protocol.Runtime Runtime { get; }
509510
public Sentry.Protocol.Trace Trace { get; }
@@ -1754,6 +1755,17 @@ namespace Sentry.Protocol
17541755
public void WriteTo(System.Text.Json.Utf8JsonWriter writer, Sentry.Extensibility.IDiagnosticLogger? _) { }
17551756
public static Sentry.Protocol.OperatingSystem FromJson(System.Text.Json.JsonElement json) { }
17561757
}
1758+
public sealed class Replay : Sentry.ISentryJsonSerializable
1759+
{
1760+
public const string Type = "replay";
1761+
public Replay() { }
1762+
public Sentry.SentryId? ReplayId { get; set; }
1763+
public Sentry.Protocol.Replay Clone() { }
1764+
public void UpdateFrom(Sentry.Protocol.Replay source) { }
1765+
public void UpdateFrom(object source) { }
1766+
public void WriteTo(System.Text.Json.Utf8JsonWriter writer, Sentry.Extensibility.IDiagnosticLogger? logger) { }
1767+
public static Sentry.Protocol.Replay FromJson(System.Text.Json.JsonElement json) { }
1768+
}
17571769
public sealed class Response : Sentry.ISentryJsonSerializable
17581770
{
17591771
public const string Type = "response";

test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ namespace Sentry
492492
public object this[string key] { get; set; }
493493
public System.Collections.Generic.ICollection<string> Keys { get; }
494494
public Sentry.Protocol.OperatingSystem OperatingSystem { get; }
495+
public Sentry.Protocol.Replay Replay { get; }
495496
public Sentry.Protocol.Response Response { get; }
496497
public Sentry.Protocol.Runtime Runtime { get; }
497498
public Sentry.Protocol.Trace Trace { get; }
@@ -1736,6 +1737,17 @@ namespace Sentry.Protocol
17361737
public void WriteTo(System.Text.Json.Utf8JsonWriter writer, Sentry.Extensibility.IDiagnosticLogger? _) { }
17371738
public static Sentry.Protocol.OperatingSystem FromJson(System.Text.Json.JsonElement json) { }
17381739
}
1740+
public sealed class Replay : Sentry.ISentryJsonSerializable
1741+
{
1742+
public const string Type = "replay";
1743+
public Replay() { }
1744+
public Sentry.SentryId? ReplayId { get; set; }
1745+
public Sentry.Protocol.Replay Clone() { }
1746+
public void UpdateFrom(Sentry.Protocol.Replay source) { }
1747+
public void UpdateFrom(object source) { }
1748+
public void WriteTo(System.Text.Json.Utf8JsonWriter writer, Sentry.Extensibility.IDiagnosticLogger? logger) { }
1749+
public static Sentry.Protocol.Replay FromJson(System.Text.Json.JsonElement json) { }
1750+
}
17391751
public sealed class Response : Sentry.ISentryJsonSerializable
17401752
{
17411753
public const string Type = "response";

test/Sentry.Tests/HubTests.CaptureEvent_ActiveTransaction_UnhandledExceptionTransactionEndedAsCrashed.DotNet8_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
SampleRate: 1.0,
171171
Request: {},
172172
Contexts: {
173+
replay: {},
173174
trace: {
174175
Operation: my operation,
175176
Description: ,

0 commit comments

Comments
 (0)