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.

Difficulties with integration of Elastic.Apm to .Net Core console apps

See original GitHub issue

Is your feature request related to a problem? Please describe. In the era of microservices a lot of services are creating as simple .Net Core console apps: message handlers, batch jobs.

From monitoring perspective it would be nice to monitor such applications with Elastic APM, however, it’s unable to achieve it right now: Elastic.Apm.AspNetCore cannot be used due to integration is performed with IApplicationBuilder, which is available only in Asp .Net Core applications. The issue can be solved by manual configuration of Elastic.Apm agent except of reading configuration values from appsettings.json due to MicrosoftExtensionsConfig is internal class.

Describe the solution you’d like As a short term solution MicrosoftExtensionsConfig can be mark as public, but it seems as a workaround. In addition, manual setup is still needed. As a long term solution, it would be nice to add additonal extension method on IHostBuilder, which is introduced in .Net Core 2.1 and became as default builder for console and http-based applications in .Net Core 3.0.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
vhatsuracommented, Jan 23, 2020

@connorads, you can track a progress in #537. Only yesterday I returned to such a thing and started to polish code. As for workaround, yes I have it for our current services. It’s very simple:

  1. Copy-paste AspNetCoreLogger, AbstractConfigurationWithEnvFallbackReader and MicrosoftExtensionsConfig into your code.
  2. Register IApmLogger, IConfigurationReader and AgentComponents into dependency injection container:
var builder =  Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
    .ConfigureServices((ctx, services) => {
        services.AddSingleton<IApmLogger>(serviceProvider =>
                new AspNetCoreLogger(serviceProvider.GetService<ILoggerFactory>()));

        services.AddSingleton<IConfigurationReader>(serviceProvider => 
            new MicrosoftExtensionsConfig(ctx.Configuration, serviceProvider.GetService<IApmLogger>(),
                ctx.HostingEnvironment.EnvironmentName));

        services.AddSingleton(serviceProvider =>
            new AgentComponents(serviceProvider.GetService<IApmLogger>(),
                serviceProvider.GetService<IConfigurationReader>()));
    });
  1. Before running your host, configure ApmAgent via static API:
var host = builder.UseConsoleLifetime().Build();

var agentComponents = host.Services.GetRequiredService<AgentComponents>();

Agent.Setup(agentComponents);
using var _ = Agent.Subscribe(new HttpDiagnosticsSubscriber(), new MongoDiagnosticsSubscriber(), 
    new SqlClientDiagnosticSubscriber());

await host.RunAsync();

So, I know, it’s not very beautiful. Also, with agent update, this code can be broken and need to be updated due to some parts marked as internal classes and can be changed. However, right now it’s only one possible way for me to use it with proper logger and configuration reader setup.

0reactions
gregkalaposcommented, Jun 25, 2020

I think this is done - #537 implements it - thanks for the work @vhatsura. There are some related issues still open and probably there will be some follow up PRs on the topic, but overall the use-case is already covered and merged.

If something important is missing please check if there is already an issue for that and if not, feel free to either comment here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to enable Elastic APM for c# console application
app.UseAllElasticApm(Configuration) but for console app we cant access app instance . ... I am using .net Core Console application and tried this ...
Read more >
Common problems | APM Server Reference [7.15]
The Elastic APM integration became generally available in 7.16 — see the APM ... This section describes common problems you might encounter with...
Read more >
Supportability configuration options | APM .NET Agent ...
Find answers that matter with Elastic on your preferred cloud provider. ... Observability. Unify app and infrastructure visibility to proactively resolve issues.
Read more >
Public API | APM .NET Agent Reference [1.x]
The public API of the Elastic APM .NET agent lets you customize and manually create spans and transactions, as well as track errors....
Read more >
How auto instrumentation with the next iteration of ...
In this blog post, we will look at the next iteration of the Elastic APM .NET Agent and its new auto instrumentation capabilities....
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