Scala integration

Hi all,

I would like to start with adding some scala support to the java-apm-agent.

Since the current repo is completely focussed on Java, I would like to discuss how this should be organized. For example, this is how Datadog does it: https://github.com/DataDog/dd-trace-java

One of the main things that I would like to work on, is adding support for the Scala ExecutionContext (and maybe some cats-effect ContextShift support).
This would allow adding some traces manually on the incoming and outgoing http-requests, but without the need to change your code-base drastically by having to pass the span-ids manually when you are writing Concurrent code.

What do you think?

Hi @milanvdm,

Providing support for Scala is definitely something that we have on our radar, thus any pull-request is welcome :-).

We already have a few issues listed: https://github.com/elastic/apm-agent-java/search?q=scala&type=Issues

There are quite a few challenges to expect, because the agent works at class/bytecode level (and not at source level), and I'm not familiar with the way scala compiler lays out classes from source code (in java it's close to a 1:1 mapping between bytecode and source).

A good start would be to start with building a wrapper for those classes, and make this wrapper call agent API to create transactions/spans.

Once you have that, you should be able to monitor with minimal code changes on the application, and it could then be used as a basis to create a plugin in the agent itself.

Hi @Sylvain_Juge,

I'll have a closer look to see if I can create a PR adding a Scala related plugin.

In the meantime, I've created a repo that does some jdbc calls and http-requests using Scala and added the ElasticAPM instrumentation to it: https://github.com/milanvdm/scala-elastic-apm

Since quite some Scala libraries use Java libraries underneath it, I expect things such as jdbc calls to work already.

Some observations:

  • JVM metrics are sent correctly (heap-usage, …)
  • The Manual Transaction I create at the start of the program and close at the end is also seen correctly
  • The debug logs of the ElasticAPMAgent seems to match on the JDBC methods and Thread switches
  • Not a single span is shown in the ElasticAPM dashboard
  • It doesn’t seem to match on http-calls in the debug logs (although AsyncHttpClient is used as an underlying library)

Since the ElasticAgent clearly logs that it is matching some Bytecode, I'm unsure what the exact reason is on why no spans are being created.