Skip to content

SentryHttpMessageHandler not being added when AddHttpClient is before UseSentry #2373

@mattjohnsonpint

Description

@mattjohnsonpint

Package

Sentry.AspNetCore

.NET Flavor

.NET

.NET Version

7.0.5

OS

Any (not platform specific)

SDK Version

3.31.0

Self-Hosted Sentry Version

No response

Steps to Reproduce

An an ASP.NET Core app, such as Sentry.Samples.AspNetCore.Basic use IHttpClientFactory per ms docs to make an outbound request from within a web controller.

Expected Result

The SentryHttpMessageHandler should automatically be connected by use of the UseSentry call from Sentry.AspNetCore. The order that it is added in relation to AddHttpClient should not matter.

Actual Result

It only works if AddHttpClient is after UseSentry. If it's before UseSentry, then the message handler is not added, and HTTP Client spans are not auto-instrumented.

This works. HttpClient requests will be auto-instrumented in Sentry.

builder.WebHost.UseSentry();
builder.Services.AddHttpClient();

This does not, but should. HttpClient requests succeed, but are not auto-instremented in Sentry.

builder.Services.AddHttpClient();
builder.WebHost.UseSentry();

Further info

SentryHttpMessageHandler gets wired up for outgoing requests in SentryWebHostBuilderExtensions. Specifically, this is done when configuring logging via:

var sentryBuilder = logging.Services.AddSentry();

Eventually that calls Sentry.Extensions.Logging.ServiceCollectionExtensions.AddSentry which contains this call that refers to #785:

// Custom handler for HttpClientFactory.
// Must be singleton: https://github.com/getsentry/sentry-dotnet/issues/785
services.TryAddSingleton<IHttpMessageHandlerBuilderFilter, SentryHttpMessageHandlerBuilderFilter>();

Metadata

Metadata

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions