Why static API for .net core agent?

APM Server version: 1.5.1

APM Agent language and version: dotnet

Hi,

We started working with APM agent for .net core (Elastic.Apm.NetCoreAll) and it works nice but I have couple of questions:

Why is everything static? It feels really awkward to use Singletons inside .net core app in 2020 - especially since .net core was build on top of IoC/DI.

It also makes it super hard to unit test code that uses Agent.Tracer.CaptureTransaction or Agent.Tracer.CaptureSpan (it is almost impossible to be honest - we have to refactor code to use the hack Mock an APM instance).

How will that singleton behave if we are using the APM tracing not only for http requests but also for rabbitmq events - since everything is a singleton is there a chance that by accessing agent singleton instance while we have both rabbitmq and http in progress might be problematic?

Thanks in advance! And keep up the great work :slight_smile:

Hi @Marcin_Druzgala,

I wrote about why we need a singleton here. If we let users new up tons of agent instances then it'd open up lots of other issues.

It also makes it super hard to unit test code that uses Agent.Tracer.CaptureTransaction or Agent.Tracer.CaptureSpan (it is almost impossible to be honest - we have to refactor code to use the hack Mock an APM instance).

One suggestion I can give you is that you could just inject the ITracer instance (that's the Agent.Tracer) into the classes you test and in a test environment just set it to null and use it with ?. in your code. That way if it's not prod and the agent is not configured there won't be traces and the API won't do anything.

How will that singleton behave if we are using the APM tracing not only for http requests but also for rabbitmq events - since everything is a singleton is there a chance that by accessing agent singleton instance while we have both rabbitmq and http in progress might be problematic?

I don't think it'd be problematic, in fact in order to correlate all the spans we can't have independent agent instances. If you'd have 2 independent agent instances for the 2 libraries it'd be even harder to correlate them.

Now, having said that - there are already some issues in the agent repo around this topic to make some improvements. This one is an example.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.