Skip to content

Commit 3e70c31

Browse files
Added trace origin to distinguish Sentry traces from custom instrumented traces (#3400)
1 parent 2947946 commit 3e70c31

File tree

41 files changed

+286
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+286
-12
lines changed

Sentry.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=EF/@EntryIndexedValue">EF</s:String>
23
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IO/@EntryIndexedValue">IO</s:String>
34
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OS/@EntryIndexedValue">OS</s:String>
45
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=QL/@EntryIndexedValue">QL</s:String>

src/Sentry.AspNet/HttpContextExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Sentry.Extensibility;
2+
using Sentry.Protocol;
23

34
namespace Sentry.AspNet;
45

@@ -9,6 +10,7 @@ namespace Sentry.AspNet;
910
public static class HttpContextExtensions
1011
{
1112
private const string HttpContextTransactionItemName = "__SentryTransaction";
13+
internal const string AspNetOrigin = "auto.http.aspnet";
1214

1315
private static SentryTraceHeader? TryGetSentryTraceHeader(HttpContext context, SentryOptions? options)
1416
{
@@ -114,6 +116,7 @@ public static ITransactionTracer StartSentryTransaction(this HttpContext httpCon
114116
}
115117

116118
var transaction = SentrySdk.StartTransaction(transactionContext, customSamplingContext, dynamicSamplingContext);
119+
transaction.Contexts.Trace.Origin = AspNetOrigin;
117120

118121
SentrySdk.ConfigureScope(scope => scope.Transaction = transaction);
119122
httpContext.Items[HttpContextTransactionItemName] = transaction;

src/Sentry.AspNetCore/SentryTracingMiddleware.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Sentry.AspNetCore;
1212
internal class SentryTracingMiddleware
1313
{
1414
private const string OperationName = "http.server";
15+
internal const string AspNetCoreOrigin = "auto.http.aspnetcore";
1516

1617
private readonly RequestDelegate _next;
1718
private readonly Func<IHub> _getHub;
@@ -78,6 +79,7 @@ public SentryTracingMiddleware(
7879
}
7980

8081
var transaction = _getHub().StartTransaction(transactionContext, customSamplingContext, dynamicSamplingContext);
82+
transaction.Contexts.Trace.Origin = AspNetCoreOrigin;
8183

8284
_options.LogInfo(
8385
"Started transaction with span ID '{0}' and trace ID '{1}'.",

src/Sentry.Azure.Functions.Worker/SentryFunctionsWorkerMiddleware.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Sentry.Azure.Functions.Worker;
88
internal class SentryFunctionsWorkerMiddleware : IFunctionsWorkerMiddleware
99
{
1010
private const string Operation = "function";
11+
internal const string AzureFunctionsOrigin = "auto.function.azure";
1112

1213
private readonly IHub _hub;
1314
private readonly IDiagnosticLogger? _logger;
@@ -23,6 +24,7 @@ public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next
2324
{
2425
var transactionContext = await StartOrContinueTraceAsync(context);
2526
var transaction = _hub.StartTransaction(transactionContext);
27+
transaction.Contexts.Trace.Origin = AzureFunctionsOrigin;
2628
Exception? unhandledException = null;
2729

2830
try
@@ -98,7 +100,7 @@ private async Task<TransactionContext> StartOrContinueTraceAsync(FunctionContext
98100
// Note that, when Trimming is enabled, we can't use reflection to read route data from the HttpTrigger
99101
// attribute. In that case the route name will always be /api/<FUNCTION_NAME>
100102
// If this is ever a problem for customers, we can potentially see if there are alternate ways to get this info
101-
// from route tables or something. We're not even sure if anyone will use this functionality for now though.
103+
// from route tables or something. We're not even sure if anyone will use this functionality for now though.
102104
if (!AotHelper.IsNativeAot && !TransactionNameCache.TryGetValue(transactionNameKey, out transactionName))
103105
{
104106
// Find the HTTP Trigger attribute via reflection

src/Sentry.DiagnosticSource/Internal/DiagnosticSource/EFDiagnosticSourceHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ internal void AddSpan(object? diagnosticSourceValue)
5959
// 2. Sentry's performance errors functionality only works when all queries have the same parent span.
6060
var parent = Transaction.GetDbParentSpan();
6161
var child = parent.StartChild(Operation, GetDescription(diagnosticSourceValue));
62+
child.SetOrigin(SentryEFCoreListener.EFCoreListenerOrigin);
6263

6364
SetDbData(child, diagnosticSourceValue);
6465
SetSpanReference(child, diagnosticSourceValue);

src/Sentry.DiagnosticSource/Internal/DiagnosticSource/SentryEFCoreListener.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ internal class SentryEFCoreListener : IObserver<KeyValuePair<string, object?>>
1313
internal const string EFCommandExecuted = "Microsoft.EntityFrameworkCore.Database.Command.CommandExecuted";
1414
internal const string EFCommandFailed = "Microsoft.EntityFrameworkCore.Database.Command.CommandError";
1515

16+
internal const string EFCoreListenerOrigin = "auto.db.ef_core_listener";
17+
1618
/// <summary>
1719
/// Used for EF Core 2.X and 3.X.
1820
/// <seealso href="https://docs.microsoft.com/dotnet/api/microsoft.entityframeworkcore.diagnostics.coreeventid.querymodelcompiling?view=efcore-3.1"/>

src/Sentry.DiagnosticSource/Internal/DiagnosticSource/SentrySqlListener.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ private enum SentrySqlSpanType
1111
Execution
1212
};
1313

14+
internal const string SqlListenerOrigin = "auto.db.sql_listener";
15+
1416
internal const string SqlDataWriteConnectionOpenBeforeCommand = "System.Data.SqlClient.WriteConnectionOpenBefore";
1517
internal const string SqlMicrosoftWriteConnectionOpenBeforeCommand = "Microsoft.Data.SqlClient.WriteConnectionOpenBefore";
1618

@@ -80,6 +82,7 @@ private void AddSpan(string operation, object? value)
8082

8183
var parent = transaction.GetDbParentSpan();
8284
var span = parent.StartChild(operation);
85+
span.SetOrigin(SqlListenerOrigin);
8386
span.SetExtra(OTelKeys.DbSystem, "sql");
8487
SetOperationId(span, value?.GetGuidProperty("OperationId", _options.DiagnosticLogger));
8588
SetConnectionId(span, value?.GetGuidProperty("ConnectionId", _options.DiagnosticLogger));

src/Sentry.EntityFramework/SentryQueryPerformanceListener.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Sentry.Internal;
2+
13
namespace Sentry.EntityFramework;
24

35
internal class SentryQueryPerformanceListener : IDbCommandInterceptor
@@ -7,6 +9,8 @@ internal class SentryQueryPerformanceListener : IDbCommandInterceptor
79
internal const string DbNonQueryKey = "db.execute";
810
internal const string DbScalarKey = "db.query.scalar";
911

12+
internal static readonly string EntityFrameworkOrigin = "auto.db.entity_framework";
13+
1014
private SentryOptions _options;
1115
private IHub _hub;
1216

@@ -39,6 +43,7 @@ private void CreateSpan<T>(string key, string? command,
3943
{
4044
if (_hub.GetSpan()?.StartChild(key, command) is { } span)
4145
{
46+
span.SetOrigin(EntityFrameworkOrigin);
4247
interceptionContext.AttachSpan(span);
4348
}
4449
}

src/Sentry.OpenTelemetry/SentrySpanProcessor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class SentrySpanProcessor : BaseProcessor<Activity>
1313
{
1414
private readonly IHub _hub;
1515
internal readonly IEnumerable<IOpenTelemetryEnricher> _enrichers;
16+
internal const string OpenTelemetryOrigin = "auto.otel";
1617

1718
// ReSharper disable once MemberCanBePrivate.Global - Used by tests
1819
internal readonly ConcurrentDictionary<ActivitySpanId, ISpan> _map = new();
@@ -123,6 +124,7 @@ private void CreateChildSpan(Activity data, ISpan parentSpan, SpanId? parentSpan
123124
};
124125

125126
var span = (SpanTracer)parentSpan.StartChild(context);
127+
span.Origin = OpenTelemetryOrigin;
126128
span.StartTimestamp = data.StartTimeUtc;
127129
// Used to filter out spans that are not recorded when finishing a transaction.
128130
span.SetFused(data);
@@ -152,6 +154,7 @@ private void CreateRootSpan(Activity data)
152154
var transaction = (TransactionTracer)_hub.StartTransaction(
153155
transactionContext, new Dictionary<string, object?>(), dynamicSamplingContext
154156
);
157+
transaction.Contexts.Trace.Origin = OpenTelemetryOrigin;
155158
transaction.StartTimestamp = data.StartTimeUtc;
156159
_hub.ConfigureScope(scope => scope.Transaction = transaction);
157160
transaction.SetFused(data);

src/Sentry/IBaseTracer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Sentry;
22

3-
internal interface IBaseTracer
3+
internal interface IBaseTracer : ITraceContextInternal
44
{
55
internal bool IsOtelInstrumenter { get; }
66
}

0 commit comments

Comments
 (0)