Use new Redis instrumentation in an ASP.NET Core app
See original GitHub issueAPM Agent version
1.8.0
Environment
.NET Framework/Core name and version (e.g. .NET 4.6.1, NET Core 3.1.100) : .NET 5.0
Application Target Framework(s) (e.g. net461, netcoreapp3.1): net5.0
Describe the bug
It seems to be impossible to configure the Redis instrumentation inside of an ASP.NET Core app while registering the ConnectionMultiplexer
into the DI services collection in the Startup.ConfigureServices
method. Calling UseElasticApm
on the ConnectionMultiplexer
triggers an access to the Agent.Instance
singleton therefore configuring the instance and locking it. When you later call app.UseElasticApm
in the Startup.Configure
method you get an Elastic.Apm.Agent.InstanceAlreadyCreatedException
with a message saying The singleton APM agent has already been instantiated and can no longer be configured.
Honestly, I have to say that working with the Elastic APM agent configuration has been a pretty poor experience as I have run into several roadblocks along the way.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:8 (4 by maintainers)
Thanks for your time earlier @ejsmith.
I’ve opened https://github.com/elastic/apm-agent-dotnet/pull/1192 to lazily access the agent in the redis integration when accessed through the
Agent.Instance
static property.An approach that might be of interest to other folks that do need to use Redis early in startup, and before the agent is initialized, it would be possible right now to register the redis integration after initialization of the agent in app configure. For example, in
ConfigureServices
Then, in
Configure
It does mean that those initial redis calls won’t be traced, but would avoid the
Elastic.Apm.Agent.InstanceAlreadyCreatedException
.As part of https://github.com/elastic/apm-agent-dotnet/pull/1161,
Elastic.Apm.Agent.InstanceAlreadyCreatedException
is removed and there will be an error log instead.Thanks @russcam for getting on a Zoom call with me and helping me figure out this issue. For anyone else looking at this, Russ is going to make the agent access in the
ElasticApmProfiler
lazy and he helped me find where my app was aggressively loading a dependency and causing the agent to be configured before the otherapp.UseElasticApm
call was made.