Skip to content
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

fix: log warn in global mode #1132

Merged
merged 7 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

### Fixes

- Add IsParentSampled to ITransactionContext ([#1128](https://github.com/getsentry/sentry-dotnet/pull/1128)
- Avoid warn in global mode ([#1132](https://github.com/getsentry/sentry-dotnet/pull/1132))
- Fix `ParentSampledId` being reset on `Transaction` ([#1130](https://github.com/getsentry/sentry-dotnet/pull/1130))
- Add IsParentSampled to ITransactionContext ([#1128]https://github.com/getsentry/sentry-dotnet/pull/1128)

## 3.8.1

Expand Down
5 changes: 1 addition & 4 deletions src/Sentry/Internal/Enricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ internal class Enricher
};
});

public Enricher(SentryOptions options)
{
_options = options;
}
public Enricher(SentryOptions options) => _options = options;

public void Apply(IEventLike eventLike)
{
Expand Down
52 changes: 21 additions & 31 deletions src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,45 @@ internal class Hub : IHub, IDisposable
// Internal for testability
internal ConditionalWeakTable<Exception, ISpan> ExceptionToSpanMap { get; } = new();

internal SentryScopeManager ScopeManager { get; }
internal IInternalScopeManager ScopeManager { get; }

private int _isEnabled = 1;
public bool IsEnabled => _isEnabled == 1;

internal Hub(ISentryClient client, ISystemClock clock, ISessionManager sessionManager, SentryOptions options)
internal Hub(
SentryOptions options,
ISentryClient? client = null,
ISessionManager? sessionManager = null,
ISystemClock? clock = null,
IInternalScopeManager? scopeManager = null)
{
_ownedClient = client;
_clock = clock;
_sessionManager = sessionManager;
_options = options;

if (Dsn.TryParse(options.Dsn) is null)
if (string.IsNullOrWhiteSpace(options.Dsn))
{
const string msg = "Attempt to instantiate a Hub without a DSN.";
options.DiagnosticLogger?.LogFatal(msg);
throw new InvalidOperationException(msg);
}

options.DiagnosticLogger?.LogDebug("Initializing Hub for Dsn: '{0}'.", options.Dsn);

ScopeManager = new SentryScopeManager(
_options = options;
_ownedClient = client ?? new SentryClient(options);
_clock = clock ?? SystemClock.Clock;
_sessionManager = sessionManager ?? new GlobalSessionManager(options);

ScopeManager = scopeManager ?? new SentryScopeManager(
options.ScopeStackContainer ?? new AsyncLocalScopeStackContainer(),
options,
_ownedClient
);

_integrations = options.Integrations;
_rootScope = options.IsGlobalModeEnabled
? DisabledHub.Instance
// Push the first scope so the async local starts from here
: PushScope();

_enricher = new Enricher(options);

_integrations = options.Integrations;
if (_integrations?.Length > 0)
{
foreach (var integration in _integrations)
Expand All @@ -65,26 +75,6 @@ internal Hub(ISentryClient client, ISystemClock clock, ISessionManager sessionMa
integration.Register(this, options);
}
}

// Push the first scope so the async local starts from here
_rootScope = PushScope();

_enricher = new Enricher(options);
}

internal Hub(ISentryClient client, ISessionManager sessionManager, SentryOptions options)
: this(client, SystemClock.Clock, sessionManager, options)
{
}

internal Hub(ISentryClient client, SentryOptions options)
: this(client, new GlobalSessionManager(options), options)
{
}

public Hub(SentryOptions options)
: this(new SentryClient(options), options)
{
}

public void ConfigureScope(Action<Scope> configureScope)
Expand Down
5 changes: 4 additions & 1 deletion src/Sentry/Internal/IInternalScopeManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using Sentry.Internal.ScopeStack;

namespace Sentry.Internal
{
internal interface IInternalScopeManager : ISentryScopeManager
internal interface IInternalScopeManager : ISentryScopeManager, IDisposable
{
KeyValuePair<Scope, ISentryClient> GetCurrent();
IScopeStackContainer ScopeStackContainer { get; }
}
}
2 changes: 1 addition & 1 deletion src/Sentry/Internal/SentryScopeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Sentry.Internal
internal sealed class SentryScopeManager : IInternalScopeManager, IDisposable
{
// Internal for testing
internal IScopeStackContainer ScopeStackContainer { get; }
public IScopeStackContainer ScopeStackContainer { get; }

private readonly SentryOptions _options;

Expand Down
8 changes: 4 additions & 4 deletions test/Sentry.AspNet.Tests/HttpContextExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public void StartSentryTransaction_BindsToScope()
{
// Arrange
using var _ = SentrySdk.UseHub(new Sentry.Internal.Hub(
Substitute.For<ISentryClient>(),
new SentryOptions {Dsn = "https://d4d82fc1c2c4032a83f3a29aa3a3aff@fake-sentry.io:65535/2147483647"}
new SentryOptions {Dsn = "https://d4d82fc1c2c4032a83f3a29aa3a3aff@fake-sentry.io:65535/2147483647"},
Substitute.For<ISentryClient>()
));

var context = new HttpContext(
Expand All @@ -58,8 +58,8 @@ public void FinishSentryTransaction_FinishesTransaction()
{
// Arrange
using var _ = SentrySdk.UseHub(new Sentry.Internal.Hub(
Substitute.For<ISentryClient>(),
new SentryOptions {Dsn = "https://d4d82fc1c2c4032a83f3a29aa3a3aff@fake-sentry.io:65535/2147483647"}
new SentryOptions {Dsn = "https://d4d82fc1c2c4032a83f3a29aa3a3aff@fake-sentry.io:65535/2147483647"},
Substitute.For<ISentryClient>()
));

var context = new HttpContext(
Expand Down
24 changes: 12 additions & 12 deletions test/Sentry.AspNetCore.Tests/SentryTracingMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public async Task Transactions_are_grouped_by_route()
// Arrange
var sentryClient = Substitute.For<ISentryClient>();

var hub = new Internal.Hub(sentryClient, new SentryOptions
var hub = new Internal.Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithoutSecret,
TracesSampleRate = 1
});
}, sentryClient);

var server = new TestServer(new WebHostBuilder()
.UseDefaultServiceProvider(di => di.EnableValidation())
Expand Down Expand Up @@ -77,10 +77,10 @@ public async Task Transaction_is_bound_on_the_scope_automatically()

var sentryClient = Substitute.For<ISentryClient>();

var hub = new Internal.Hub(sentryClient, new SentryOptions
var hub = new Internal.Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithoutSecret
});
}, sentryClient);

var server = new TestServer(new WebHostBuilder()
.UseDefaultServiceProvider(di => di.EnableValidation())
Expand Down Expand Up @@ -124,11 +124,11 @@ public async Task Transaction_is_started_automatically_from_incoming_trace_heade
// Arrange
var sentryClient = Substitute.For<ISentryClient>();

var hub = new Internal.Hub(sentryClient, new SentryOptions
var hub = new Internal.Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithoutSecret,
TracesSampleRate = 1
});
}, sentryClient);

var server = new TestServer(new WebHostBuilder()
.UseDefaultServiceProvider(di => di.EnableValidation())
Expand Down Expand Up @@ -179,11 +179,11 @@ public async Task Transaction_is_automatically_populated_with_request_data()

var sentryClient = Substitute.For<ISentryClient>();

var hub = new Internal.Hub(sentryClient, new SentryOptions
var hub = new Internal.Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithoutSecret,
TracesSampleRate = 1
});
}, sentryClient);

var server = new TestServer(new WebHostBuilder()
.UseDefaultServiceProvider(di => di.EnableValidation())
Expand Down Expand Up @@ -236,15 +236,15 @@ public async Task Transaction_sampling_context_contains_HTTP_context_data()

var sentryClient = Substitute.For<ISentryClient>();

var hub = new Internal.Hub(sentryClient, new SentryOptions
var hub = new Internal.Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithoutSecret,
TracesSampler = ctx =>
{
samplingContext = ctx;
return 1;
}
});
}, sentryClient);

var server = new TestServer(new WebHostBuilder()
.UseDefaultServiceProvider(di => di.EnableValidation())
Expand Down Expand Up @@ -288,15 +288,15 @@ public async Task Transaction_binds_exception_thrown()

var sentryClient = Substitute.For<ISentryClient>();

var hub = new Internal.Hub(sentryClient, new SentryOptions
var hub = new Internal.Hub( new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithoutSecret,
TracesSampler = ctx =>
{
samplingContext = ctx;
return 1;
}
});
}, sentryClient);
var exception = new Exception();

var server = new TestServer(new WebHostBuilder()
Expand Down
Loading