[bug] Cannot configure OpenTelemetry based on data from DI-registered service #6102
Labels
bug
Something isn't working
needs-triage
New issues which have not been classified or triaged by a community member
pkg:OpenTelemetry
Issues related to OpenTelemetry NuGet package
Package
OpenTelemetry
Package Version
Runtime Version
net9.0
Description
Hi,
We are trying to configure the OpenTelemetry exporters based on "dynamic" settings coming from a dependency-injected service. Our code is in a private repo, but the overall approach is somewhat like this (using a
Startup
class, i.e. classical ASP.NET Core 5 approach):Our problem is, as you might guess by now, that the required services are not yet available in the
ConfigureServices
phase.They would be available during the
Configure
phase, at which point it is trival to inject anything you like from the IOC container (by just adding more parameters to theConfigure
method signature). The framework will do the rest for you:However, it's not really possible to configure the OpenTelemetry things this later during the startup - or is it? At least when I've tried (perhaps a bit clumsily) to register the
IOpenTelemetryBuilder
and "do things" in theConfigure
step, nothing seemed to work. I believe this is perhaps because theTelemetryHostedService
has already started whenConfigure
gets called - I attached a debugger now and this does indeed seem to be the case.I thought about reverse-engineering something like what
AddOpenTelemetry
does under the hood:...but a major problem here is that
TelemetryHostedService
isinternal
, as well as theOpenTelemetryBuilder
constructor. I realize these kind of "hacks" goes against the "normal" way to use OpenTelemetry.NET. It would be great to hear some thoughts regarding this though. Are there any ways to configure (these aspects) of OpenTelemetry based on data not available until theIServiceProvider
is available? 🤔Steps to Reproduce
The
Startup
class referred to is available here, along with a MCVE (without any injected dependency), in case anyone wants to tinker around.Expected Result
A way to configure OpenTelemetry exporters (enable/disable console/OTLP exporters dynamically/after services have been configured).
Actual Result
No available way to configure OpenTelemetry once the ASP.NET Core web application has started.
Additional Context
Semi-related problem: #3416
The text was updated successfully, but these errors were encountered: