Skip to content

Commit 97f7dcd

Browse files
Added guidance on capturing exceptions via OTel in .NET (#14343)
## DESCRIBE YOUR PR Resolves getsentry/sentry-dotnet#2520 and getsentry/sentry-dotnet#2519: - getsentry/sentry-dotnet#2520 - getsentry/sentry-dotnet#2519 ## IS YOUR CHANGE URGENT? - [x] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. CC: @Flash0ver ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) --------- Co-authored-by: Stefan Pölz <[email protected]>
1 parent b7e052f commit 97f7dcd

File tree

4 files changed

+57
-61
lines changed

4 files changed

+57
-61
lines changed

docs/platforms/dotnet/common/tracing/instrumentation/opentelemetry.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ You can configure your [OpenTelemetry SDK](https://opentelemetry.io/) to send tr
1818

1919
With Sentry’s OpenTelemetry SDK, an OpenTelemetry `Span` becomes a Sentry `Transaction` or `Span`. The first `Span` sent through the Sentry `SpanProcessor` is a `Transaction`, and any child `Span` gets attached to the first `Transaction` upon checking the parent `Span` context. This is true for the OpenTelemetry root `Span` and any top level `Span` in the system. For example, a request sent from frontend to backend will create an OpenTelemetry root `Span` with a corresponding Sentry `Transaction`. The backend request will create a new Sentry `Transaction` for the OpenTelemetry `Span`. The Sentry `Transaction` and `Span` are linked as a trace for navigation and error tracking purposes.
2020

21+
### Capturing exceptions
22+
23+
OpenTelemetry in .NET is implemented via the System.Diagnostics.Activity namespace. However, not all of the functionality in that namespace is supported by OpenTelemetry. In particular, it is [not recommended](https://github.com/open-telemetry/opentelemetry-specification/pull/4333) that you use the `Activity.RecordException` or `Activity.AddException` methods. Using either of these methods will result in valuable **information being removed from exceptions** before these get captured by Sentry.
24+
25+
Instead you should either log the exceptions (using `ILogger`) or capture the exceptions directly using `SentrySdk.CaptureException`.
26+
2127
## Additional Configuration
2228

2329
If you need more fine grained control over Sentry, take a look at the <PlatformLink to="/configuration/">Configuration page</PlatformLink>. In case you'd like to filter out transactions before sending them to Sentry (to get rid of health checks, for example), you may find the <PlatformLink to="/configuration/filtering/#filtering-transaction-events">Filtering page</PlatformLink> helpful.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
To start tracing in an ASP.NET application, you'll need to create a tracer provider.
2+
3+
```csharp
4+
var builder = Sdk.CreateTracerProviderBuilder()
5+
.AddAspNetInstrumentation(); // <-- Adds ASP.NET telemetry sources
6+
```
7+
8+
Next, initialize Sentry and opt into the use of OpenTelemetry. Provide the SDK with the builder for OpenTelemetry's tracer provider to allow sending spans to Sentry.
9+
10+
```csharp
11+
_sentry = SentrySdk.Init(options =>
12+
{
13+
//options.Dsn = "...Your DSN...";
14+
//options.SendDefaultPii = true;
15+
options.TracesSampleRate = 1.0;
16+
options.AddAspNet(RequestSize.Always);
17+
options.UseOpenTelemetry(builder);
18+
});
19+
```
20+
21+
Lastly, build the tracer provider.
22+
23+
```csharp
24+
_tracerProvider = builder.Build();
25+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
To start tracing in an ASP.NET Core app, add OpenTelemetry with tracing and add Sentry to the tracer provider.
2+
3+
```csharp
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
builder.Services.AddOpenTelemetry()
7+
.WithTracing(tracerProviderBuilder =>
8+
tracerProviderBuilder
9+
.AddAspNetCoreInstrumentation() // <-- Adds ASP.NET Core telemetry sources
10+
.AddHttpClientInstrumentation() // <-- Adds HttpClient telemetry sources
11+
.AddSentry() // <-- Configure OpenTelemetry to send trace information to Sentry
12+
);
13+
```
14+
15+
Next, initialize Sentry and opt into the use of OpenTelemetry. This allows the SDK to send OpenTelemetry spans to Sentry.
16+
17+
```csharp
18+
builder.WebHost.UseSentry(options =>
19+
{
20+
options.Dsn = "...Your DSN...";
21+
options.SendDefaultPii = true;
22+
options.TracesSampleRate = 1.0;
23+
options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
24+
});
25+
```
Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
To learn how to start tracing based on your application kind, read the instructions below.
2-
3-
### Console Applications
4-
5-
To start tracing in a console application, you'll need to add Sentry to the tracer provider. This will make it possible for OpenTelemetry spans to be captured by Sentry.
1+
To start tracing, you'll need to add Sentry to the tracer provider. This will make it possible for OpenTelemetry spans to be captured by Sentry.
62

73
```csharp
84
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
@@ -22,59 +18,3 @@ SentrySdk.Init(options =>
2218
options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
2319
});
2420
```
25-
26-
### ASP.NET Core Applications
27-
28-
To start tracing in an ASP.NET Core app, add OpenTelemetry with tracing and add Sentry to the tracer provider.
29-
30-
```csharp
31-
var builder = WebApplication.CreateBuilder(args);
32-
33-
builder.Services.AddOpenTelemetry()
34-
.WithTracing(tracerProviderBuilder =>
35-
tracerProviderBuilder
36-
.AddAspNetCoreInstrumentation() // <-- Adds ASP.NET Core telemetry sources
37-
.AddHttpClientInstrumentation() // <-- Adds HttpClient telemetry sources
38-
.AddSentry() // <-- Configure OpenTelemetry to send trace information to Sentry
39-
);
40-
```
41-
42-
Next, initialize Sentry and opt into the use of OpenTelemetry. This allows the SDK to send OpenTelemetry spans to Sentry.
43-
44-
```csharp
45-
builder.WebHost.UseSentry(options =>
46-
{
47-
options.Dsn = "...Your DSN...";
48-
options.SendDefaultPii = true;
49-
options.TracesSampleRate = 1.0;
50-
options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
51-
});
52-
```
53-
54-
### ASP.NET Applications
55-
56-
To start tracing in an ASP.NET application, you'll need to create a tracer provider.
57-
58-
```csharp
59-
var builder = Sdk.CreateTracerProviderBuilder()
60-
.AddAspNetInstrumentation(); // <-- Adds ASP.NET telemetry sources
61-
```
62-
63-
Next, initialize Sentry and opt into the use of OpenTelemetry. Provide the SDK with the builder for OpenTelemetry's tracer provider to allow sending spans to Sentry.
64-
65-
```csharp
66-
_sentry = SentrySdk.Init(options =>
67-
{
68-
//options.Dsn = "...Your DSN...";
69-
//options.SendDefaultPii = true;
70-
options.TracesSampleRate = 1.0;
71-
options.AddAspNet(RequestSize.Always);
72-
options.UseOpenTelemetry(builder);
73-
});
74-
```
75-
76-
Lastly, build the tracer provider.
77-
78-
```csharp
79-
_tracerProvider = builder.Build();
80-
```

0 commit comments

Comments
 (0)