Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions samples/Sentry.Samples.AspNetCore.Blazor.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

using Microsoft.AspNetCore.Components.Server.Circuits;
using OpenTelemetry.Trace;
using Sentry.OpenTelemetry;
using Sentry.OpenTelemetry.Exporter;
using Sentry.Samples.AspNetCore.Blazor.Server.Services;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

#if SENTRY_DSN_DEFINED_IN_ENV
var dsn = Environment.GetEnvironmentVariable("SENTRY_DSN")
?? throw new InvalidOperationException("SENTRY_DSN environment variable is not set");
#else
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
var dsn = SamplesShared.Dsn;
#endif

#if NET10_0_OR_GREATER
// OpenTelemetry is required for the new .NET 10 Blazor telemetry features
builder.Services.AddOpenTelemetry()
Expand All @@ -19,19 +28,15 @@
tracing.AddSource("Microsoft.AspNetCore.Components.Server.Circuits");
tracing.AddAspNetCoreInstrumentation();
// Add Sentry as an exporter
tracing.AddSentry();
tracing.AddSentryOtlpExporter(dsn);
});
#endif

builder.WebHost.UseSentry(options =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, in the SENTRY_DSN environment variable or in your appsettings.json
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = SamplesShared.Dsn;
#endif
options.Dsn = dsn;
#if NET10_0_OR_GREATER
options.UseOpenTelemetry();
options.UseOtlp();
options.AddEventProcessor(new BlazorEventProcessor());
#endif
options.TracesSampleRate = 1.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Sentry.AspNetCore.Blazor.WebAssembly\Sentry.AspNetCore.Blazor.WebAssembly.csproj" />
<ProjectReference Include="..\..\src\Sentry.AspNetCore\Sentry.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Sentry.OpenTelemetry\Sentry.OpenTelemetry.csproj" />
<ProjectReference Include="..\..\src\Sentry.OpenTelemetry.Exporter\Sentry.OpenTelemetry.Exporter.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.18.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.18.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.18.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.18.0" />
</ItemGroup>

</Project>
21 changes: 13 additions & 8 deletions samples/Sentry.Samples.GraphQL.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using GraphQL.Types;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Sentry.OpenTelemetry;
using Sentry.OpenTelemetry.Exporter;
using Sentry.Samples.GraphQL.Server.Notes;

namespace Sentry.Samples.GraphQL.Server;
Expand All @@ -29,28 +29,33 @@ public static WebApplication BuildWebApplication(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

#if SENTRY_DSN_DEFINED_IN_ENV
var dsn = Environment.GetEnvironmentVariable("SENTRY_DSN")
?? throw new InvalidOperationException("SENTRY_DSN environment variable is not set");
#else
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
var dsn = SamplesShared.Dsn;
#endif

builder.Services.AddOpenTelemetry()
.WithTracing(tracerProviderBuilder =>
tracerProviderBuilder
.AddSource(GraphQLTelemetryProvider.SourceName) // <-- Ensure telemetry is gathered from graphql
.ConfigureResource(resource => resource.AddService("Sentry.Samples.GraphQL.Server"))
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddSentry() // <-- Ensure telemetry is sent to Sentry
.AddSentryOtlpExporter(dsn) // <-- Ensure telemetry is sent to Sentry
);

builder.WebHost.UseSentry(options =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = SamplesShared.Dsn;
#endif
options.Dsn = dsn;

options.TracesSampleRate = 1.0;
options.Debug = true;
options.SendDefaultPii = true;
options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
options.UseOtlp(); // <-- Configure Sentry to use OpenTelemetry trace information
});

builder.Services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.18.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.18.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.18.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.18.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />
</ItemGroup>

Expand All @@ -29,7 +30,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Sentry.AspNetCore\Sentry.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Sentry.OpenTelemetry\Sentry.OpenTelemetry.csproj" />
<ProjectReference Include="..\..\src\Sentry.OpenTelemetry.Exporter\Sentry.OpenTelemetry.Exporter.csproj" />
<ProjectReference Include="..\..\src\Sentry\Sentry.csproj" />
</ItemGroup>
</Project>
26 changes: 2 additions & 24 deletions samples/Sentry.Samples.OpenTelemetry.AspNetCore/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
# Overview

This sample demonstrates how an ASP.NET Core application that is instrumented with the OpenTelemetry .NET SDK can be
configured to send trace information to Sentry, with the following initialization code:

```csharp
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry()
.WithTracing(tracerProviderBuilder =>
tracerProviderBuilder
.AddSource(Telemetry.ActivitySource.Name)
.ConfigureResource(resource => resource.AddService(Telemetry.ServiceName))
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddSentry() // <-- Configure OpenTelemetry to send trace information to Sentry
);

builder.WebHost.UseSentry(options =>
{
options.Dsn = "...Your DSN...";
options.Debug = builder.Environment.IsDevelopment();
options.TracesSampleRate = 1.0;
options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
});
```
configured to send trace information to Sentry.

## Customizing propagation

Expand All @@ -33,4 +11,4 @@ and/or to downstream services.

If you need to further customize header propagation in your application (e.g. propagating other vendor specific headers)
then you can do so by creating a `CompositeTextMapPropagator` consisting of the custom propagator(s) you need plus the
`SentryPropagator`. You can supply this as an optional parameter to the `AddSentry` method.
`SentryPropagator`. You can supply this as an optional parameter to the `AddSentryOtlpExporter` method.
22 changes: 2 additions & 20 deletions samples/Sentry.Samples.OpenTelemetry.Console/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
# Overview

This sample demonstrates how a console application that is instrumented with the OpenTelemetry .NET SDK can be
configured to send trace information to Sentry, with the following initialization code:

```csharp
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource(serviceName)
.ConfigureResource(resource =>
resource.AddService(
serviceName: serviceName,
serviceVersion: serviceVersion))
.AddSentry() // <-- Configure OpenTelemetry to send traces to Sentry
.Build();

SentrySdk.Init(o =>
{
options.Dsn = "...Your DSN...";
options.TracesSampleRate = 1.0;
options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
});
```
configured to send trace information to Sentry.

## Customizing propagation

Expand All @@ -29,4 +11,4 @@ and/or to downstream services.

If you need to further customize header propagation in your application (e.g. propagating other vendor specific headers)
then you can do so by creating a `CompositeTextMapPropagator` consisting of the custom propagator(s) you need plus the
`SentryPropagator`. You can supply this as an optional parameter to the `AddSentry` method.
`SentryPropagator`. You can supply this as an optional parameter to the `AddSentryOtlpExporter` method.
14 changes: 6 additions & 8 deletions src/Sentry.OpenTelemetry/SentryOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace Sentry.OpenTelemetry;
/// </summary>
public static class SentryOptionsExtensions
{
internal const string ObsoleteUseOpenTelemetry =
"Use UseOtlp from the Sentry.OpenTelemetry.Exporter package instead. " +
"This method will be removed in future versions.";

/// <summary>
/// Enables OpenTelemetry instrumentation with Sentry
/// </summary>
Expand All @@ -28,10 +32,7 @@ public static class SentryOptionsExtensions
/// It's recommended that you set this to <c>true</c> since mixing OpenTelemetry and Sentry traces may yield
/// unexpected results. It is <c>false</c> by default for backward compatibility only.
/// </param>
/// <remarks>
/// This method of initialising the Sentry OpenTelemetry integration will be deprecated in a future major release.
/// We recommend you use the Sentry.OpenTelemetry.Exporter integration instead.
/// </remarks>
[Obsolete(ObsoleteUseOpenTelemetry)]
public static void UseOpenTelemetry(this SentryOptions options, TracerProviderBuilder traceProviderBuilder,
TextMapPropagator? defaultTextMapPropagator = null, bool disableSentryTracing = false)
{
Comment thread
sentry[bot] marked this conversation as resolved.
Expand All @@ -52,10 +53,7 @@ public static void UseOpenTelemetry(this SentryOptions options, TracerProviderBu
/// It's recommended that you set this to <c>true</c> since mixing OpenTelemetry and Sentry traces may yield
/// unexpected results. It is <c>false</c> by default for backward compatibility only.
/// </param>
/// <remarks>
/// This method of initialising the Sentry OpenTelemetry integration will be deprecated in a future major release.
/// We recommend you use the Sentry.OpenTelemetry.Exporter integration instead.
/// </remarks>
[Obsolete(ObsoleteUseOpenTelemetry)]
public static void UseOpenTelemetry(this SentryOptions options, bool disableSentryTracing = false)
{
options.Instrumenter = Instrumenter.OpenTelemetry;
Expand Down
9 changes: 5 additions & 4 deletions src/Sentry.OpenTelemetry/TracerProviderBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
/// </summary>
public static class TracerProviderBuilderExtensions
{
internal const string ObsoleteAddSentry =
"Use AddSentryOtlpExporter from the Sentry.OpenTelemetry.Exporter package instead. " +
"This method will be removed in future versions.";

/// <summary>
/// <para>
/// Ensures OpenTelemetry trace information is sent to Sentry. OpenTelemetry spans will be converted to Sentry spans
Expand All @@ -35,12 +39,9 @@
/// </para>
/// </param>
/// <returns>The supplied <see cref="TracerProviderBuilder"/> for chaining.</returns>
/// <remarks>
/// This method of initialising the Sentry OpenTelemetry integration will be deprecated in a future major release.
/// We recommend you use the Sentry.OpenTelemetry.Exporter integration instead.
/// </remarks>
[Obsolete(ObsoleteAddSentry)]
public static TracerProviderBuilder AddSentry(this TracerProviderBuilder tracerProviderBuilder,
TextMapPropagator? defaultTextMapPropagator = null)

Check failure on line 44 in src/Sentry.OpenTelemetry/TracerProviderBuilderExtensions.cs

View check run for this annotation

@sentry/warden / warden: find-bugs

Obsolete AddSentry makes same-assembly call a build error

With TreatWarningsAsErrors, SentryOptionsExtensions.UseOpenTelemetry's call to AddSentry will fail CS0618; wrap that call (and the sibling UseOpenTelemetry call) in #pragma warning disable/restore CS0618.
{
defaultTextMapPropagator ??= new SentryPropagator();
Sdk.SetDefaultTextMapPropagator(defaultTextMapPropagator);
Expand Down
Loading