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

Add SentryOptions.DisableSentryHttpMessageHandler #3879

Merged
merged 5 commits into from
Jan 12, 2025
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Added `SentryOptions.DisableSentryHttpMessageHandler`. Useful if you're using `OpenTelemetry.Instrumentation.Http` and ending up with duplicate spans. ([#3879](https://github.com/getsentry/sentry-dotnet/pull/3879))

## 5.0.1

### Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public Action<HttpMessageHandlerBuilder> Configure(Action<HttpMessageHandlerBuil
handlerBuilder =>
{
var hub = _getHub();
if (!handlerBuilder.AdditionalHandlers.Any(h => h is SentryHttpMessageHandler))
var enableHandler = hub.GetSentryOptions()?.DisableSentryHttpMessageHandler == false;
if (enableHandler && !handlerBuilder.AdditionalHandlers.Any(h => h is SentryHttpMessageHandler))
{
handlerBuilder.AdditionalHandlers.Add(
new SentryHttpMessageHandler(hub)
Expand Down
2 changes: 2 additions & 0 deletions src/Sentry/BindableSentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ internal partial class BindableSentryOptions
public TimeSpan? AutoSessionTrackingInterval { get; set; }
public bool? AutoSessionTracking { get; set; }
public bool? UseAsyncFileIO { get; set; }
public bool? DisableSentryHttpMessageHandler { get; set; }
public bool? JsonPreserveReferences { get; set; }
public bool? EnableSpotlight { get; set; }
public string? SpotlightUrl { get; set; }
Expand Down Expand Up @@ -94,6 +95,7 @@ public void ApplyTo(SentryOptions options)
options.AutoSessionTrackingInterval = AutoSessionTrackingInterval ?? options.AutoSessionTrackingInterval;
options.AutoSessionTracking = AutoSessionTracking ?? options.AutoSessionTracking;
options.UseAsyncFileIO = UseAsyncFileIO ?? options.UseAsyncFileIO;
options.DisableSentryHttpMessageHandler = DisableSentryHttpMessageHandler ?? options.DisableSentryHttpMessageHandler;
options.JsonPreserveReferences = JsonPreserveReferences ?? options.JsonPreserveReferences;
options.EnableSpotlight = EnableSpotlight ?? options.EnableSpotlight;
options.SpotlightUrl = SpotlightUrl ?? options.SpotlightUrl;
Expand Down
3 changes: 2 additions & 1 deletion src/Sentry/SentryHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Sentry;

/// <summary>
/// Special HTTP message handler that can be used to propagate Sentry headers and other contextual information.
/// Special HTTP message handler that can be used to propagate Sentry headers and other contextual information. Will
/// also create events for failed requests if <see cref="SentryOptions.CaptureFailedRequests"/> is enabled.
/// </summary>
public class SentryHttpMessageHandler : SentryMessageHandler
{
Expand Down
8 changes: 8 additions & 0 deletions src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,14 @@ public StackTraceMode StackTraceMode
/// </summary>
internal Instrumenter Instrumenter { get; set; } = Instrumenter.Sentry;

/// <summary>
/// <para>
/// Set to `true` to prevents Sentry from automatically registering <see cref="SentryHttpMessageHandler"/>.
/// </para>
/// <para>Defaults to `false`. Should be set to `true` when using the OpenTelemetry.Instrumentation.Http.</para>
/// </summary>
public bool DisableSentryHttpMessageHandler { get; set; } = false;

/// <summary>
/// Adds a <see cref="JsonConverter"/> to be used when serializing or deserializing
/// objects to JSON with this SDK. For example, when custom context data might use
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Microsoft.Extensions.Http;

namespace Sentry.Extensions.Logging.Tests;

public class SentryHttpMessageHandlerBuilderFilterTests
{
[SkippableFact]
public void Configure_HandlerEnabled_ShouldAddSentryHttpMessageHandler()
{
#if __ANDROID__
Skip.If(true, "Can't create proxies for classes without parameterless constructors on Android");
#endif

// Arrange
var hub = Substitute.For<IHub>();
SentryClientExtensions.SentryOptionsForTestingOnly = new SentryOptions { DisableSentryHttpMessageHandler = false };

var filter = new SentryHttpMessageHandlerBuilderFilter(() => hub);
var handlerBuilder = Substitute.For<HttpMessageHandlerBuilder>();
handlerBuilder.AdditionalHandlers.Returns(new List<DelegatingHandler>());
Action<HttpMessageHandlerBuilder> next = _ => { };

// Act
var configure = filter.Configure(next);
configure(handlerBuilder);

// Assert
handlerBuilder.AdditionalHandlers.Should().ContainSingle(h => h is SentryHttpMessageHandler);
}

[SkippableFact]
public void Configure_HandlerDisabled_ShouldNotAddSentryHttpMessageHandler()
{
#if __ANDROID__
Skip.If(true, "Can't create proxies for classes without parameterless constructors on Android");
#endif

// Arrange
var hub = Substitute.For<IHub>();
SentryClientExtensions.SentryOptionsForTestingOnly = new SentryOptions { DisableSentryHttpMessageHandler = true };

var filter = new SentryHttpMessageHandlerBuilderFilter(() => hub);
var handlerBuilder = Substitute.For<HttpMessageHandlerBuilder>();
handlerBuilder.AdditionalHandlers.Returns(new List<DelegatingHandler>());
Action<HttpMessageHandlerBuilder> next = _ => { };

// Act
var configure = filter.Configure(next);
configure(handlerBuilder);

// Assert
handlerBuilder.AdditionalHandlers.Should().NotContain(h => h is SentryHttpMessageHandler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ namespace Sentry
public Sentry.SentryLevel DiagnosticLevel { get; set; }
public Sentry.Extensibility.IDiagnosticLogger? DiagnosticLogger { get; set; }
public bool DisableFileWrite { get; set; }
public bool DisableSentryHttpMessageHandler { get; set; }
public string? Distribution { get; set; }
public string? Dsn { get; set; }
public bool EnableScopeSync { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ namespace Sentry
public Sentry.SentryLevel DiagnosticLevel { get; set; }
public Sentry.Extensibility.IDiagnosticLogger? DiagnosticLogger { get; set; }
public bool DisableFileWrite { get; set; }
public bool DisableSentryHttpMessageHandler { get; set; }
public string? Distribution { get; set; }
public string? Dsn { get; set; }
public bool EnableScopeSync { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ namespace Sentry
public Sentry.SentryLevel DiagnosticLevel { get; set; }
public Sentry.Extensibility.IDiagnosticLogger? DiagnosticLogger { get; set; }
public bool DisableFileWrite { get; set; }
public bool DisableSentryHttpMessageHandler { get; set; }
public string? Distribution { get; set; }
public string? Dsn { get; set; }
public bool EnableScopeSync { get; set; }
Expand Down
Loading