question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use new Redis instrumentation in an ASP.NET Core app

See original GitHub issue

APM 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
russcamcommented, Feb 19, 2021

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

services.AddSingleton(_ => {
	const string redisConnection = @"localhost,abortConnect=false";
	var redisConnectionMultiplexer = ConnectionMultiplexer.Connect(redisConnection);
	return redisConnectionMultiplexer;
});

Then, in Configure

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
   // Some call to redis happens **before** the agent is initialized

    // agent initialized here
    app.UseAllElasticApm(Configuration);

    var muxer = app.ApplicationServices.GetService<ConnectionMultiplexer>();
    // register redis integration
    muxer.UseElasticApm();

    // other app setup
}

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.

1reaction
ejsmithcommented, Feb 19, 2021

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 other app.UseElasticApm call was made.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Distributed caching in ASP.NET Core
NET Core distributed cache to improve app performance and scalability, ... NET Core app, and use an Azure Redis Cache for local development....
Read more >
Redis Cache Calls In OpenTelemetry in DotNet
Currently I tried OpenTelemetry.StackExchange.redis.Instrumentation configured as Above but Not getting Redis call Traces. Is there Any Way to ...
Read more >
Configuration on ASP.NET Core | APM .NET Agent ...
With this setup, the Agent is able to be configured in the same way as any other library in your application. For example,...
Read more >
Automatic Instrumentation of Containerized .NET ...
Learn how to automatically instrument your containerized .NET applications using OpenTelemetry and see how it integrates with Jaeger and ...
Read more >
Getting started with OpenTelemetry and distributed tracing in ...
This means that users can instrument their applications/libraries to emit OpenTelemetry compatible traces by using just the .NET Runtime.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found