getsentry/sentry-dotnet

CaptureCheckIn always report production environment, even if SDK environment differs

digital88 opened this issue · 1 comments

Package

Sentry

.NET Flavor

.NET

.NET Version

5.0.17

OS

Windows

SDK Version

4.2.1

Self-Hosted Sentry Version

24.1.2

Steps to Reproduce

This is what I see in Crons Tab

image

This is one of transactions with correct Environment

image

Code:

appsettings.json

{
  "Sentry": {
    "Dsn": ...,
    "Environment": "test",
    ...
  },
  "Serilog": {
    "WriteTo": [
      {
        "Name": "Sentry",
        "Args": {
          "InitializeSdk": false,
          "MinimumBreadcrumbLevel": "Information",
          "MinimumEventLevel": "Warning"
        }
      }
    ]
  }
}

Sentry Init Code (simplified)

public static IHostBuilder CreateHostBuilder(string[] args) =>
	Host.CreateDefaultBuilder(args)
		.ConfigureAppConfiguration((_, builder) =>
		{
			builder.AddEnvironmentVariables(...);
			builder.AddJsonFile("appsettings.json", optional: false);
		})
		.ConfigureServices((hostContext, services) =>
		{
			services.AddHostedService<MyService>();
			services.RegisterDependencies(hostContext.Configuration);
			SentrySettings.ConfigureSentry(hostContext.Configuration);
		})
		.UseWindowsService()
		.UseSystemd()
		.UseSerilog((ctx, config) => config.ReadFrom.Configuration(ctx.Configuration));

.. SentrySettings class
public static void ConfigureSentry(IConfiguration configuration)
{
	if (!bool.TryParse(configuration.GetSection("SentryEnabled").Value, out bool sentryEnabled)) return;
	if (!sentryEnabled) return;

	var opts = configuration.GetSection("Sentry").Get<SentryOptions>();
	var proxySettings = configuration.GetSection("Proxy").Get<ProxySettings>();
	if (proxySettings != null && !string.IsNullOrWhiteSpace(proxySettings.Url))
	{
		opts.HttpProxy = new WebProxy()
		{
			Credentials = new NetworkCredential(proxySettings.Username, proxySettings.Password,
				proxySettings.Domain),
			Address = new Uri(proxySettings.Url)
		};
	}

	SentrySdk.Init(opts);
}

Monitor Code (simplified)

Task
.Factory
.StartNew(() =>
{
	var slug = $"my-monitor";
	var scope = SentrySdk.PushScope();
	var transaction = SentrySdk.StartTransaction(
		"scheduler-job",
		"job-run"
	);
	SentrySdk.ConfigureScope(sc => { sc.Transaction = transaction; });
	var sentryId = SentrySdk.CaptureCheckIn(slug, CheckInStatus.InProgress);
	var sentryIdStr = sentryId.ToString(); 
	return _httpService
		.SendAsync(...).ContinueWith(k =>
		{
			try
			{
				_logger.LogInformation(
					$"Job {slug} result: {(string.IsNullOrEmpty(k.Result) ? "Success" : k.Result)}. SentryId={sentryIdStr}");
				SentrySdk.CaptureCheckIn(slug, CheckInStatus.Ok, sentryId);
			}
			catch (AggregateException ex)
			{
				_logger.LogError(ex.Flatten(),
					$"Job {slug} error. SentryId={sentryIdStr}");
				SentrySdk.CaptureCheckIn(slug, CheckInStatus.Error, sentryId);
			}
			catch (Exception ex)
			{
				_logger.LogError(ex,
					$"Job {slug} error. SentryId={sentryIdStr}");
				SentrySdk.CaptureCheckIn(slug, CheckInStatus.Error, sentryId);
			}
			finally
			{
				transaction?.Finish();
				scope?.Dispose();
			}
		});
});

Expected Result

Environment in Crons tab match with env in appsettings and transaction environment

Actual Result

See screenshots above, environment is different.

That's a bug. Thanks for raising this @digital88. We'll be fixing that asap.