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.

Ability to programmatically control config settings

See original GitHub issue

My environment in aspnetcore is set to staging, but I want to change it to something more specific for Elastic APM. I know that I can control it via environment variables, but I need to set it programmatically based on other settings. It appears that you do not allow just passing a configuration object into UseElasticApm and force me to pass an IConfiguration object. Seems like it would be very useful to be able to read settings from IConfiguration and then be able to additionally change something programmatically before I give it to the UseElasticApm method and start the instrumentation process.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
xitaocrazycommented, Jul 7, 2020

Hello guys, I would like to share my experience and how I solved this type of problem, it’s nothing special, but it helped me to configure APM programmatically

First, I created a class with all the APM constants, I really don’t know if the agent already provides something like this.

public static class ElasticApmConstants
{
	public static string ServiceName => "ElasticApm:ServiceName";
	public static string ServiceNodeName => "ElasticApm:ServiceNodeName";
	public static string ServiceVersion => "ElasticApm:ServiceVersion";
	public static string Environment => "ElasticApm:Environment";
	public static string TransactionSampleRate => "ElasticApm:TransactionSampleRate";
	public static string TransactionMaxSpans => "ElasticApm:TransactionMaxSpans";
	public static string CentralConfig => "ElasticApm:CentralConfig";
	public static string SanitizeFieldNames => "ElasticApm:SanitizeFieldNames";
	public static string GlobalLabels => "ElasticApm:GlobalLabels";
	public static string ServerUrls => "ElasticApm:ServerUrls";
	public static string SecretToken => "ElasticApm:SecretToken";
	public static string ApiKey => "ElasticApm:ApiKey";
	public static string VerifyServerCert => "ElasticApm:VerifyServerCert";
	public static string FlushInterval => "ElasticApm:FlushInterval";
	public static string MaxBatchEventCount => "ElasticApm:MaxBatchEventCount";
	public static string MaxQueueEventCount => "ElasticApm:MaxQueueEventCount";
	public static string MetricsInterval => "ElasticApm:MetricsInterval";
	public static string DisableMetrics => "ElasticApm:DisableMetrics";
	public static string CaptureBody => "ElasticApm:CaptureBody";
	public static string CaptureBodyContentTypes => "ElasticApm:CaptureBodyContentTypes";
	public static string CaptureHeaders => "ElasticApm:CaptureHeaders";
	public static string ApplicationNamespaces => "ElasticApm:ApplicationNamespaces";
	public static string ExcludedNamespaces => "ElasticApm:ExcludedNamespaces";
	public static string StackTraceLimit => "ElasticApm:StackTraceLimit";
	public static string SpanFramesMinDuration => "ElasticApm:SpanFramesMinDuration";
	public static string LogLevel => "ElasticApm:LogLevel";
}

And I have created an extension method that receives IConfiguration and a dictionary with my settings defined on the fly.

public static IApplicationBuilder UseElasticApm(this IApplicationBuilder builder, IConfiguration configuration, Dictionary<string, string> myConfig)
{
	myConfig.ForEach(x => configuration[x.Key] = x.Value);
	return builder.UseAllElasticApm(configuration);
}

In my Configure method, I call my own extension by passing my list of settings, so I override the IConfiguration and send it to APM, of course, I have to use the properties in ElasticApmConstants as keys in my dictionary.

0reactions
ArveSystadcommented, Feb 8, 2023

Just wanna chime in and say that some way to inject a custom IConfigurationProvider (or whatever form it takes in the future) would be a very, very welcome change.

In our scenario, we’re on a long journey from old to new on a distributed system, where we have components that are ASPNET Full Framework, ASPNET Core and Windows Services (both hosted inside an NServicebus process and as separate generic hosts, the “modern way”). Some of the Windows services host ASPNET Core APIs on top of that. So for the Windows Services I need to do manual setup (which is fine), and then duplicate that same configuration into the ASPNETCore config files that are deployed along side it, which cannot be used in any standard way from the service as of now (NServiceBus hosted process, so no appsettings.json support out of the box).

I would like to use the auto instrumentation where possible (so for example let it hook up to HTTP requests honoring all config etc that is set), but at the same time be able to provide my own configuration implementation once, and only once. As of now, this doesn’t seem possible, since for example app.UseElasticApm() doesn’t accept any way to inject the static config, and the PayloadSenderV2 then ends up with a convention based configuration even if you’ve set the config on the Agent earlier.

It claims to be a singleton and stopping subsequent setup-attempts (It even reports The singleton APM agent has already been instantiated and can no longer be configured. Reusing existing instance.), which it does, but that doesn’t protect from misconfiguration if AgentComponents are newed up with a separate configuration set in app.UseElasticApm().

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modify Remote Config programmatically - Firebase
This document describes how you can programatically read and modify the set of JSON-formatted parameters and conditions known as the Remote ...
Read more >
Introducing Control API: provision & configure ...
Control API is a REST API that allows you to manage Ably's configuration programmatically, from your configuration and orchestration tools.
Read more >
Programmatically Initializing the Control System Designer
Configure Control System Designer from the command line and create functions to customize the startup of a Control System Designer session.
Read more >
How to programmatically configure the settings of a CLion ...
Specifically, the settings (File > Settings) I would like to be able to set via a command or script are: those found under...
Read more >
Frequently Asked Questions - Zebra Technologies TechDocs
Using DataWedge APIs to control the scanner - Provides the ability to programmatically control, modify and query the DataWedge configuration settings and ...
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