apm-agent-dotnet
apm-agent-dotnet copied to clipboard
Optional Elastic APM agent initialization only if ServerUrl is acutally set
Currently there are two ways to enable Elastic APM in Net core. Either in IHostBuilder or ConfigureServices
and as it defaults ServerUrl to localhost APM instance there is no really clear way to disable initializing of Elastic APM agent only when ServerUrl
setting is specifically set. If in ConfigureServices
you can add manual checks and UseElasticApm only if ServerUrl is specified in either environment variable or configuration than in IHostBuilder
there is no really way to do that.
It would be nice if there would be way to call UseElasticApm
method with option to enable Elastic APM agent only if ServerUrl
is specifically set in configuration to not to have use workarounds for this
Hi @lafriks,
Not exactly what you're asking for, but there is the Enabled
property in 1.7.0+ that can be used to control whether the agent is enabled or not. When the agent is not enabled, the agent does not perform instrumentation or remote config polling.
Yes, I'm aware of that but that would be breaking change for us and would require asking all our clients to change configuration to either manually disable or enable APM agent. Currently we were checking if ServerUrls is set and only then added agent in ConfigureServices
but that is unfortunately not an option in IHostBuilder
@lafriks you should also be aware that disabling Apm with the Enabled
property can result in failures - see https://github.com/elastic/apm-agent-dotnet/issues/1080
The issues will be addressed by a mixture of PR and documentation changes but essentially Enabled == false
is a breaking change because you can get a transaction (internally a NoopTransaction
) that can throw when you access certain properties which are not initialised for the instance which were always initialised in version < 1.7.0.
Currently we were checking if ServerUrls is set and only then added agent in
ConfigureServices
but that is unfortunately not an option in IHostBuilder
@lafriks Looking at this now. Would you mind providing an example of what you're doing in ConfigureServices
, and what you'd like to do with IHostBuilder
? I'm guessing that in ConfigureServices
, since IConfiguration
is accessible, this is used to get the ServerUrl
and determine whether to initialize the agent. But in IHostBuilder
, the configuration is not accessible on IHostBuilder UseElasticApm(this IHostBuilder builder, params IDiagnosticsSubscriber[] subscribers)
. Be good to see a more concrete example
Currently I'm using in Configure
method of Startup
class such construct:
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("ELASTIC_APM_SERVER_URLS")) ||
!string.IsNullOrWhiteSpace(configuration.GetValue<string>("ElasticApm:ServerUrls")))
{
app.UseElasticApm(configuration, ...);
}
I can't really move this code to Program
class IHostBuilder UseElasticApm
as configuration is not accessible there to do that.
Would be nice if there was to way to use UseElasticApm
on IHostBuilder
by providing some option or something like that to enable Elastic APM support only when server URL is explicitly set (ex. not default localhost value)