apm-agent-dotnet icon indicating copy to clipboard operation
apm-agent-dotnet copied to clipboard

Can't load config settings from app.config/appSettings.json from outside of ASP.Net

Open jderose9 opened this issue 5 years ago • 3 comments
trafficstars

Is your feature request related to a problem? Please describe. There is no easy/obvious way to load Elastic APM config settings from app.config or appSettings.json in an application that is not using the HTTP Module/Middleware ASP.Net implementation.

Describe the solution you'd like Put config settings in app.config and have Elastic APM use them.

A "cherry on top" would be to easily allow custom configuration to be passed in as an initialization step at runtime, say via a Dictionary. This would allow most easy integration with a remote configuration management implementation.

Describe alternatives you've considered I've worked around this by reading the configuration settings I was interested in into Environment variables manually, before any access to Apm.Agent causes the Agent to be lazily initialized. Though, this took digging through the code to see where/when/if those environment variables were actually read when UseElasticApm() is not called.

Environment.SetEnvironmentVariable("ELASTIC_APM_SERVER_URLS", ConfigurationManager.AppSettings["ElasticApm:ServerUrls"]);
Environment.SetEnvironmentVariable("ELASTIC_APM_SECRET_TOKEN", ConfigurationManager.AppSettings["ElasticApm:SecretToken"]);
Environment.SetEnvironmentVariable("ELASTIC_APM_SERVICE_NAME", ConfigurationManager.AppSettings["ElasticApm:ServiceName"]);
Environment.SetEnvironmentVariable("ELASTIC_APM_TRANSACTION_SAMPLE_RATE", ConfigurationManager.AppSettings["ElasticApm:TransactionSampleRate"]);

Additional context I'm integrating Elastic APM into a Window service and wanted to store the config settings in the app.config. My first thought was to call Agent.Setup(new AgentComponents(configReader: new FullFrameworkConfigReader()) utilizing the APM AspNetFullFramework library, but it is internal, and it has many dependencies that are internal, and so on...

A possible solution would seem to be to create another layer between Elastic.Apm and Elastic.Apm.AspNetFullFramework/Elastic.Apm.AspNetCore namespaces (for example, Elastic.Apm.NetFullFramework/Elastic.Apm.NetCore) that contained the framework-specific configuration initialization (such as FullFrameworkConfigReader) for those frameworks regardless of whether the HTTP module/middleware were used.

jderose9 avatar Jan 17 '20 19:01 jderose9

Hi

I've just needed exactly the same thing.

It is easy to do adding a copy of this file to your project and replacing the Read method with something like this:

private ConfigurationKeyValue Read(string key) => new ConfigurationKeyValue(key, ConfigurationManager.AppSettings[key]?.Trim(), Origin);

Then call Agent.Setup(configReader: ....... ) as you suggest.

marcoregueira avatar Jan 27 '20 11:01 marcoregueira

Agreed, it is not very intuitive to use Elastic APM outside of ASP.Net (Core). You are always required to implement your own config reader, even if you just want to follow the standards of the configuration (as in ASP.Net)

TomDeVree avatar Jul 10 '20 07:07 TomDeVree

Related: https://github.com/elastic/apm-agent-dotnet/issues/825

AlexanderWert avatar Jun 02 '21 20:06 AlexanderWert