Skip to content

Commit 13351a4

Browse files
committed
finalized observer
1 parent 65facbe commit 13351a4

File tree

9 files changed

+69
-9
lines changed

9 files changed

+69
-9
lines changed

src/Sentry/IScopeObserver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface IScopeObserver
2929
/// Sets the user information.
3030
/// </summary>
3131
public void SetUser(SentryUser? user);
32-
32+
3333
/// <summary>
3434
/// Sets the current trace
3535
/// </summary>

src/Sentry/Internal/ScopeObserver.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,17 @@ public void SetUser(SentryUser? user)
8181
}
8282
}
8383

84+
public abstract void SetUserImpl(SentryUser user);
85+
86+
public abstract void UnsetUserImpl();
87+
8488
public void SetTrace(SentryId traceId, SpanId parentSpanId)
8589
{
86-
throw new NotImplementedException();
90+
_options.DiagnosticLogger?.Log(
91+
SentryLevel.Debug,"{0} Scope Sync - Setting Trace traceId:{1} parentSpanId:{2}", null,
92+
_name, traceId, parentSpanId);
93+
SetTraceImpl(traceId, parentSpanId);
8794
}
8895

89-
public abstract void SetUserImpl(SentryUser user);
90-
91-
public abstract void UnsetUserImpl();
96+
public abstract void SetTraceImpl(SentryId traceId, SpanId parentSpanId);
9297
}

src/Sentry/InternalSentrySdk.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Sentry;
2+
3+
/// <summary>
4+
/// Sentry SDK internal API methods meant for being used by the Sentry Unity SDK
5+
/// </summary>
6+
public static class InternalSentrySdk
7+
{
8+
/// <summary>
9+
/// Allows to set the trace
10+
/// </summary>
11+
public static void SetTrace(SentryId traceId, SpanId parentSpanId) =>
12+
SentrySdk.CurrentHub.ConfigureScope(scope =>
13+
scope.SetPropagationContext(new SentryPropagationContext(traceId, parentSpanId)));
14+
}

src/Sentry/Platforms/Android/AndroidScopeObserver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,9 @@ public void SetUser(SentryUser? user)
9999
_innerObserver?.SetUser(user);
100100
}
101101
}
102+
103+
public void SetTrace(SentryId traceId, SpanId parentSpanId)
104+
{
105+
// TODO: This requires sentry-java 8.4.0
106+
}
102107
}

src/Sentry/Platforms/Cocoa/CocoaScopeObserver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,9 @@ public void SetUser(SentryUser? user)
107107
_innerObserver?.SetUser(user);
108108
}
109109
}
110+
111+
public void SetTrace(SentryId traceId, SpanId parentSpanId)
112+
{
113+
// TODO: Missing corresponding functionality on the Cocoa SDK
114+
}
110115
}

src/Sentry/Platforms/Native/CFunctions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ internal static string GetCacheDirectory(SentryOptions options)
241241
[DllImport("sentry-native")]
242242
internal static extern void sentry_remove_extra(string key);
243243

244+
[DllImport("sentry-native")]
245+
internal static extern void sentry_set_trace(string traceId, string parentSpanId);
246+
244247
internal static Dictionary<long, DebugImage> LoadDebugImages(IDiagnosticLogger? logger)
245248
{
246249
// It only makes sense to load them once because they're cached on the native side anyway. We could force

src/Sentry/Platforms/Native/NativeScopeObserver.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public override void SetUserImpl(SentryUser user)
4040

4141
public override void UnsetUserImpl() => C.sentry_remove_user();
4242

43+
public override void SetTraceImpl(SentryId traceId, SpanId parentSpanId) =>
44+
C.sentry_set_trace(traceId.ToString(), parentSpanId.ToString());
45+
4346
private static string GetTimestamp(DateTimeOffset timestamp) =>
4447
// "o": Using ISO 8601 to make sure the timestamp makes it to the bridge correctly.
4548
// https://docs.microsoft.com/en-gb/dotnet/standard/base-types/standard-date-and-time-format-strings#Roundtrip

test/Sentry.Tests/HubTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ public void GetTraceHeader_NoSpanActive_ReturnsHeaderFromPropagationContext()
10131013
var propagationContext = new SentryPropagationContext(
10141014
SentryId.Parse("75302ac48a024bde9a3b3734a82e36c8"),
10151015
SpanId.Parse("2000000000000000"));
1016-
hub.ConfigureScope(scope => scope.PropagationContext = propagationContext);
1016+
hub.ConfigureScope(scope => scope.SetPropagationContext(propagationContext));
10171017

10181018
// Act
10191019
var header = hub.GetTraceHeader();
@@ -1052,7 +1052,7 @@ public void GetBaggage_NoSpanActive_ReturnsBaggageFromPropagationContext()
10521052
var hub = _fixture.GetSut();
10531053
var propagationContext = new SentryPropagationContext(
10541054
SentryId.Parse("43365712692146d08ee11a729dfbcaca"), SpanId.Parse("1000000000000000"));
1055-
hub.ConfigureScope(scope => scope.PropagationContext = propagationContext);
1055+
hub.ConfigureScope(scope => scope.SetPropagationContext(propagationContext));
10561056

10571057
// Act
10581058
var baggage = hub.GetBaggage();
@@ -1069,7 +1069,7 @@ public void ContinueTrace_SetsPropagationContextAndReturnsTransactionContext()
10691069
var hub = _fixture.GetSut();
10701070
var propagationContext = new SentryPropagationContext(
10711071
SentryId.Parse("43365712692146d08ee11a729dfbcaca"), SpanId.Parse("1000000000000000"));
1072-
hub.ConfigureScope(scope => scope.PropagationContext = propagationContext);
1072+
hub.ConfigureScope(scope => scope.SetPropagationContext(propagationContext));
10731073

10741074
var traceHeader = new SentryTraceHeader(SentryId.Parse("5bd5f6d346b442dd9177dce9302fd737"),
10751075
SpanId.Parse("2000000000000000"), null);
@@ -1104,7 +1104,7 @@ public void ContinueTrace_ReceivesHeadersAsStrings_SetsPropagationContextAndRetu
11041104
var hub = _fixture.GetSut();
11051105
var propagationContext = new SentryPropagationContext(
11061106
SentryId.Parse("43365712692146d08ee11a729dfbcaca"), SpanId.Parse("1000000000000000"));
1107-
hub.ConfigureScope(scope => scope.PropagationContext = propagationContext);
1107+
hub.ConfigureScope(scope => scope.SetPropagationContext(propagationContext));
11081108
var traceHeader = "5bd5f6d346b442dd9177dce9302fd737-2000000000000000";
11091109
var baggageHeader = "sentry-trace_id=5bd5f6d346b442dd9177dce9302fd737, sentry-public_key=49d0f7386ad645858ae85020e393bef3, sentry-sample_rate=1.0";
11101110

test/Sentry.Tests/ScopeTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,31 @@ public void SetTag_NullValue_DoesNotThrowArgumentNullException()
633633

634634
Assert.Null(exception);
635635
}
636+
637+
[Theory]
638+
[InlineData(true)]
639+
[InlineData(false)]
640+
public void SetPropagationContext_ObserverExist_ObserverSetsTraceIfEnabled(bool enableScopeSync)
641+
{
642+
// Arrange
643+
var observer = Substitute.For<IScopeObserver>();
644+
var scope = new Scope(new SentryOptions
645+
{
646+
ScopeObserver = observer,
647+
EnableScopeSync = enableScopeSync
648+
});
649+
var propagationContext = new SentryPropagationContext();
650+
var expectedTraceId = propagationContext.TraceId;
651+
var expectedSpanId = propagationContext.SpanId;
652+
var expectedCount = enableScopeSync ? 1 : 0;
653+
654+
// Act
655+
scope.SetPropagationContext(propagationContext);
656+
657+
// Assert
658+
scope.PropagationContext.Should().Be(propagationContext);
659+
observer.Received(expectedCount).SetTrace(Arg.Is(expectedTraceId), Arg.Is(expectedSpanId));
660+
}
636661
}
637662

638663
public static class ScopeTestExtensions

0 commit comments

Comments
 (0)