-
-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sentry.OpenTelemetry: The Sentry SDK has not been initialised. To use Sentry with OpenTelemetry tracing you need to initialize the Sentry SDK. #2738
Comments
Thanks for the report @sinanbozkus ! Are you able to try switching those statments, so the Sentry registration comes first? builder.WebHost.UseSentry(o =>
{
o.Dsn = "XXXXXXXXX";
o.TracesSampleRate = 1.0;
o.UseOpenTelemetry();
});
builder.Services.AddOpenTelemetry()
.WithTracing(tracerProviderBuilder =>
tracerProviderBuilder
.AddSource("Development-Application.Api")
.ConfigureResource(resource => resource.AddService("Development-Application.Api"))
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddSentry() // <-- Configure OpenTelemetry to send trace information to Sentry
); |
I've tried like this but I got the same error. |
Hi @sinanbozkus , can you try as I suggested ( |
I've tried, it's the same. |
Hm, the error itself is coming from here: sentry-dotnet/src/Sentry.OpenTelemetry/SentrySpanProcessor.cs Lines 40 to 47 in ebbb500
Typically that would indicate that You could try adding If not, would it be possible to get you to share a minimal reproducible example in a GitHub repo that we could clone? |
I have the same problem the Sentry SDK has not been initialised. To use Sentry with OpenTelemetry tracing you need to initialize the Sentry SDK. Follow the instructions @jamescrosswell |
@raulidavid I'm unable to reproduce this... the Sample app provided with Sentry works fine for me. Are you able to provide a copy of what you're seeing in the Debug window when you run the application with |
Version 3.40.1 Error |
@raulidavid I think we're maybe not on the same wavelength... I mean the output that you see in the Debug console in your IDE. For example, this is what I see when I run the application in debug mode with
|
Here is an example of such behavior, I'm limited to using the "Microsoft.Extensions.Hosting.Host" to ensure that I can configure the custom tracing context, that will clean up health check routes from all traces. Unfortunately, I get the same error. That's why my setup is another one than in your sample. If you can give me a hint on how could I make a customization for tracing context using using System;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Profile.Host.Config;
using Sentry;
using Sentry.OpenTelemetry;
using Serilog;
using Hosting = Microsoft.Extensions.Hosting.Host;
namespace Sample.Host
{
public static class Program
{
private const string EnvVariableName = "ASPNETCORE_ENVIRONMENT";
public static void Main(string[] args)
{
var envName = Environment.GetEnvironmentVariable(EnvVariableName);
var settingsFileName = !string.IsNullOrEmpty(envName)
? $"appsettings.{envName}.json"
: $"appsettings.json";
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(settingsFileName)
.AddEnvironmentVariables()
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
BuildWebHost(args).Build().Run();
}
public static IHostBuilder BuildWebHost(string[] args)
{
var builder = Hosting.CreateDefaultBuilder(args);
builder.ConfigureServices(t => t.AddOpenTelemetry()
.WithTracing(g => g
.AddSource(Startup.ServiceName)
.ConfigureResource(resource => resource.AddService(serviceName: Startup.ServiceName,
serviceVersion: Assembly.GetExecutingAssembly().GetName().Version?.ToString()))
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddConsoleExporter()
.AddSentry())
.WithMetrics(m => m
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddConsoleExporter()));
builder.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseSentry(options =>
{
options.TracesSampler = context =>
{
if (context.CustomSamplingContext.TryGetValue("__HttpPath", out var value))
{
string path = (string)value;
if (path!.Equals(Startup.HealthPath) || path.Equals(Startup.ReadinessPath) ||
path.Equals(Startup.LivenessPath))
return 0.0;
}
return options.TracesSampleRate;
};
options.UseOpenTelemetry();
options.AddEntityFramework();
});
webBuilder.UseStartup<Startup>();
})
.UseSerilog();
return builder;
}
}
} |
Even when I migrated my code to the .Net 6.0 style. It still doesn't work (even with WebApplication.CreateBuilder(args)). |
@leonid-lm again, would it be possible to get a copy of the debug output so that we can get some visibility into what is and what isn't working during SDK initialization? See my reply to @raulidavid above. |
I was having this issue and in my case it was an existing line in my project clearing the logging providers that was also clearing the SentryOptions. (...)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
})
.UseSentry(options =>
{
options.UseOpenTelemetry();
})
(...) |
I was able to identify the issue's root cause. For some reason existing initialization of the sentry goes into conflict with Serilog initialization. The current issue is easily reproducible if you just add the following code: var builder = WebApplication.CreateBuilder(args);
var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);
builder.Services.AddOpenTelemetry()
.WithTracing(traceBuilder =>
{
traceBuilder
.AddSource(Startup.ServiceName)
.ConfigureResource(resource => resource.AddService(serviceName: Startup.ServiceName,
serviceVersion: Assembly.GetExecutingAssembly().GetName().Version?.ToString()))
.AddAspNetCoreInstrumentation(option => option.RecordException = true)
.AddHttpClientInstrumentation(option => option.RecordException = true)
.AddSentry();
});
builder.Host.UseSerilog((context, configuration) =>
configuration.ReadFrom.Configuration(context.Configuration));
builder.WebHost.UseSentry(options =>
{
options.Debug = builder.Environment.IsDevelopment();
options.TracesSampler = context =>
{
if (context.CustomSamplingContext.TryGetValue("__HttpPath", out var value))
{
string path = (string)value;
if (path!.Equals(Startup.HealthPath) || path.Equals(Startup.ReadinessPath) ||
path.Equals(Startup.LivenessPath))
return 0.0;
}
return options.TracesSampleRate;
};
options.AddEntityFramework();
options.UseOpenTelemetry();
});
var app = builder.Build();
startup.Configure(app, app.Environment);
app.Run(); If you try to remove the following lines, it works: builder.Host.UseSerilog((context, configuration) =>
configuration.ReadFrom.Configuration(context.Configuration)); So, if you would like to have the Sentry traces enabled, you need to get rid of Serilog. |
It's a bit tricky to tell without seeing the config that is being passed to Serilog, but I suspect what's happening is that the call to sentry-dotnet/src/Sentry.AspNetCore/SentryWebHostBuilderExtensions.cs Lines 78 to 83 in aa25db9
Sentry's initialization is being delayed when the Microsoft Extension Logging backend has been replaced (by Serilog) and so when @leonid-lm are you able to share a sanitized version of the relevant bits of your serilog/sentry config? |
I'm having the same issue, and I've removed all Serilog config except just Here's my sentry config: static void ConfigureSentry(WebApplicationBuilder builder)
{
builder.Logging.AddOpenTelemetry(b =>
{
b.IncludeFormattedMessage = true;
b.IncludeScopes = true;
b.ParseStateValues = true;
b.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("xxxxxx"));
});
builder.Services
.AddOpenTelemetry()
.WithTracing(b =>
{
b.AddSource("xxxxxx");
b.ConfigureResource(r => r.AddService("xxxxxx"));
b.AddAspNetCoreInstrumentation();
b.AddHttpClientInstrumentation();
b.AddHotChocolateInstrumentation();
b.AddSentry(); // Removing this line "fixes" the issue
}
);
builder.WebHost.UseSentry(o =>
{
o.Dsn = builder.Configuration.GetSetting("Sentry:Dsn");
o.TracesSampleRate = 1.0;
o.Environment = builder.Configuration.GetSetting("Sentry:Environment");
o.UseOpenTelemetry();
o.SetBeforeSendTransaction((sentryTransaction, hint) =>
{
// Filter out requests to BCP
if (MatchBCPRegex().IsMatch(sentryTransaction.Name))
return null;
return sentryTransaction;
});
});
} |
OK, progress. I'm able to replicate the same by adding this to builder.Host.UseSerilog(); Now all that remains is to fix the problem 😜 |
Thanks a lot... I had a really simple config for sentry and serilog: "Sentry": {
"Dsn": "<LINK>",
"SendDefaultPii": true,
"IncludeActivityData": true,
"AutoSessionTracking": true,
"AttachStackTrace": true,
"EnableTracing": true,
"Debug": false,
"DiagnosticLevel": "Error",
"TracesSampleRate": 1.0,
"Environment": "Integration"
},
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.Seq" ],
"LevelSwitches": { "$controlSwitch": "Information" },
"MinimumLevel": {
"ControlledBy": "$controlSwitch",
"Override": {
"Microsoft": "Warning",
"System": "Warning",
"Hangfire": "Warning"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
}
},
{
"Name": "Seq",
"Args": {
"serverUrl": "http://seq.logs.svc.cluster.local:5341",
"apiKey": "<KEY>"
}
},
{
"Name": "Sentry",
"Args": {
"minimumBreadcrumbLevel": "Debug",
"minimumEventLevel": "Error"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithEventType" ],
"Properties": {
"Application": "app-api",
"Environment": "Integration"
}
}, |
@knd775 and @leonid-lm are you able to try with version 3.41.3? |
Working for me! Thank you! |
Great, I'll mark this as closed then. Any other issues I think we open a new ticket 🙂 |
Package
Other
.NET Flavor
.NET
.NET Version
7.0.12
OS
Linux
SDK Version
7.0.0
Self-Hosted Sentry Version
No response
Steps to Reproduce
When I was using "Sentry.OpenTelemetry" 3.39.1 or older everything was ok. After updating 3.40.0 or 3.40.1 I got this error.
The Sentry SDK has not been initialised. To use Sentry with OpenTelemetry tracing you need to initialize the Sentry SDK.
Expected Result
No error
Actual Result
The Sentry SDK has not been initialised. To use Sentry with OpenTelemetry tracing you need to initialize the Sentry SDK.initialized
The text was updated successfully, but these errors were encountered: