Skip to content

Commit 438ae83

Browse files
authored
Allow IScopeObserver to observe trace (#4026)
1 parent 13d4a9f commit 438ae83

16 files changed

+88
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Features
66

7+
- The `IScopeObserver` now has an `SetTrace` that allows observing changes to the scope's trace context. The SDK uses this to propagate the `trace ID` to `sentry-native`. This allows Sentry to connect errors coming from all layers of your application ([#4026](https://github.com/getsentry/sentry-dotnet/pull/4026))
78
- Exception.HResult is now included in the mechanism data for all exceptions ([#4029](https://github.com/getsentry/sentry-dotnet/pull/4029))
89

910
### Fixes

src/Sentry/IScopeObserver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ public interface IScopeObserver
2929
/// Sets the user information.
3030
/// </summary>
3131
public void SetUser(SentryUser? user);
32+
33+
/// <summary>
34+
/// Sets the current trace
35+
/// </summary>
36+
public void SetTrace(SentryId traceId, SpanId parentSpanId);
3237
}

src/Sentry/Internal/Hub.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public TransactionContext ContinueTrace(
255255
string? operation = null)
256256
{
257257
var propagationContext = SentryPropagationContext.CreateFromHeaders(_options.DiagnosticLogger, traceHeader, baggageHeader);
258-
ConfigureScope(scope => scope.PropagationContext = propagationContext);
258+
ConfigureScope(scope => scope.SetPropagationContext(propagationContext));
259259

260260
return new TransactionContext(
261261
name: name ?? string.Empty,

src/Sentry/Internal/ScopeObserver.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,14 @@ public void SetUser(SentryUser? user)
8484
public abstract void SetUserImpl(SentryUser user);
8585

8686
public abstract void UnsetUserImpl();
87+
88+
public void SetTrace(SentryId traceId, SpanId parentSpanId)
89+
{
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);
94+
}
95+
96+
public abstract void SetTraceImpl(SentryId traceId, SpanId parentSpanId);
8797
}

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

src/Sentry/Scope.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public ITransactionTracer? Transaction
233233
}
234234
}
235235

236-
internal SentryPropagationContext PropagationContext { get; set; }
236+
internal SentryPropagationContext PropagationContext { get; private set; }
237237

238238
internal SessionUpdate? SessionUpdate { get; set; }
239239

@@ -376,6 +376,15 @@ public void UnsetTag(string key)
376376
/// </summary>
377377
public void AddAttachment(SentryAttachment attachment) => _attachments.Add(attachment);
378378

379+
internal void SetPropagationContext(SentryPropagationContext propagationContext)
380+
{
381+
PropagationContext = propagationContext;
382+
if (Options.EnableScopeSync)
383+
{
384+
Options.ScopeObserver?.SetTrace(propagationContext.TraceId, propagationContext.SpanId);
385+
}
386+
}
387+
379388
/// <summary>
380389
/// Resets all the properties and collections within the scope to their default values.
381390
/// </summary>

src/Sentry/Sentry.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616

1717
<PropertyGroup Condition="'$(SolutionName)' == 'Sentry.Unity'">
1818
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
19+
<!-- To be able to make the internals visible, since we're not signing the assembly for Unity -->
20+
<SignAssembly>false</SignAssembly>
1921
</PropertyGroup>
2022

23+
<ItemGroup Condition="'$(SolutionName)' == 'Sentry.Unity'">
24+
<InternalsVisibleTo Include="Sentry.Unity" />
25+
</ItemGroup>
26+
2127
<!-- Platform-specific props included here -->
2228
<Import Project="Platforms\Android\Sentry.Android.props" Condition="'$(TargetPlatformIdentifier)' == 'android'" />
2329
<Import Project="Platforms\Cocoa\Sentry.Cocoa.props" Condition="'$(TargetPlatformIdentifier)' == 'ios' Or '$(TargetPlatformIdentifier)' == 'maccatalyst'" />

0 commit comments

Comments
 (0)