APM Java agent: How to customize transaction sample rates based on different controllers

I have a Java application that has multiple RESTful controllers and APM agent deployed. However, I only want certain actions/transactions to be traced instead of all of them (e.g., XXXController#update -> sample_rate 0.9). Is there an easy way to approach this?

Hi,

If you have a platinum or enterprise license, tail-based sampling is exactly what you are looking for:

- sample_rate: .9
  trace.name: "XXXController#update"
- sample_rate:  .0

This will cause the agent to still send all traces to the APM-Server and the server will only keep the matching traces. This is useful because you can also use the outcome of the REST request to discard all successfull traces and only keep traces for errors.

If you do not have such a license, your best bet would be to disable the auto instrumentation of controllers and use @CaptureTransaction to only instrument the methods you want: Public API | APM .NET Agent Reference [1.x] | Elastic

Best regards
Wolfram

Thanks for your prompt reply! I have tried adding tail-based sampling to apm-server.yml but while I was running the server setup I got something like “apm integration installed failed…index template matching [traces-apm] not found”. Our elastic and kibana are 7.10 while apm server is 7.17, so I doubt if it's a version incompatibility issue?

Tailbased sampling is available in 7.x, but it was made generally available in 8.1: APM version 8.1 | Elastic Observability [8.15] | Elastic

Maybe you should consider upgrading your stack some time.

I think you need to install the assets for the Elastic APM integration from Fleet - even if you use the standalone APM server: Install and uninstall Elastic Agent integration assets | Fleet and Elastic Agent Guide [7.17] | Elastic

Here is someone, that had a similar issue: APM Server index template matching [traces-apm.sampled] not found - #2 by simitt

1 Like