# How does sampling work

**URL:** <https://discuss.elastic.co/t/how-does-sampling-work/247617>\
**Category:** APM\
**Tags:** java\
**Created:** [September 5, 2020, 6:46am UTC](https://discuss.elastic.co/t/how-does-sampling-work/247617 "2020-09-05T06:46:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![manohar\_deepu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manohar_deepu/32/72797_2.png) [@manohar\_deepu](https://discuss.elastic.co/u/manohar_deepu)\
**Post date:** [September 5, 2020, 6:46am UTC](https://discuss.elastic.co/t/how-does-sampling-work/247617/1 "2020-09-05T06:46:22Z")

</div>

Hi  
Can you please clarify these questions around sampling,

1. Does agents push all the metrics to apm server irrespective of sampling and apm server chooses the data to ingest based on sampling %
2. Or does apm agents themselves discard some data based on sampling rate at machine level?
3. If the working mode is point 1, whats the use of having sampling rate configuration at agent level?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [September 7, 2020, 2:01am UTC](https://discuss.elastic.co/t/how-does-sampling-work/247617/2 "2020-09-07T02:01:54Z")

</div>

#2) The Java APM agent performs the sampling before the data is sent to the APM server.

To be clear even when transaction sampling is set to a value less than 1.0 every transaction time and duration is recorded. When a transaction is not sampled it means it does not include details for the spans / detailed trace information.

Perhaps also look at dynamic profiling since you appear to be using the Java agent.

> **[From distributed tracing to distributed profiling with Elastic APM](https://www.elastic.co/blog/from-distributed-tracing-to-distributed-profiling-with-elastic-apm)**
>
> Distributed tracing in free and open Elastic APM helps you identify (micro)services within complex architectures having issues interfering with user experience. And now we've added support for additional inferred spans by leveraging async-profiler,...

I would suggest taking a good look at all the sampling, reporting minimum span durations etc settings, as you can highly customize the Java agent behavior.

[https://www.elastic.co/guide/en/apm/agent/java/current/configuration.html](https://www.elastic.co/guide/en/apm/agent/java/current/configuration.html)

---

<div class="post-metadata">

**Author:** ![manohar\_deepu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manohar_deepu/32/72797_2.png) [@manohar\_deepu](https://discuss.elastic.co/u/manohar_deepu)\
**Post date:** [September 7, 2020, 5:34am UTC](https://discuss.elastic.co/t/how-does-sampling-work/247617/3 "2020-09-07T05:34:41Z")

</div>

Thanks for the reply, but my question is more around if the Agent itself discards sampling then how does multiple microservices involved in distributed tracing make sure that when the originator transaction is sampled, they also send their data irrespective of their own sampling rate.

Example if service 1 service 2 are on APM and service 1 calls service2 .  
service1 sampling rate is 0.1 and service 2 sampling rate is 0.01,

Then irrespective of service 2 sampling rate, if the service 2 sees that the call from service 1 is being sampled, then it has to push its data also (transaction + span data). This will take the service 2 sampling rate \> 0.01 rite?

Please can you clarify on the above scenario.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [September 7, 2020, 5:43am UTC](https://discuss.elastic.co/t/how-does-sampling-work/247617/4 "2020-09-07T05:43:43Z")

</div>

Here is some text from a discussion I had earlier on the subject.

In addition to that, the transferred trace context also contains the sampling decision of the calling transaction, so that the root transaction of the trace makes the sampling decision for all “downstream” transactions of that trace.  
This way, distributed tracing still works for 100% of the transactions and the sampling decisions are consistent within traces across different services.

Hopefully that makes sense.

I suggest setting up your services and take a look and then tune from there.

---

<div class="post-metadata">

**Author:** ![manohar\_deepu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manohar_deepu/32/72797_2.png) [@manohar\_deepu](https://discuss.elastic.co/u/manohar_deepu)\
**Post date:** [September 7, 2020, 6:09am UTC](https://discuss.elastic.co/t/how-does-sampling-work/247617/5 "2020-09-07T06:09:48Z")

</div>

I have already setup cluster and running APM.

My question is more from understanding purpose.

> [@stephenb](#):
>
> In addition to that, the transferred trace context also contains the sampling decision of the calling transaction, so that the root transaction of the trace makes the sampling decision for all “downstream” transactions of that trace.

By this statement, we are essentially saying that service 2 sampling rate can shoot above its own configured rate of 0.01 since it has to adhere to caller's sampling decision.

If there are multiple callers with high sampling rate, then, since the "called" microservice has to adhere to sampling decision which has already been taken by the caller, it can overshoot its own sampling rate.

Example Service 1 , 2 , 3 calling service 4.  
Service 1 , 2 and 3 on 100% sampling rate and service 4 on 1% sampling rate.  
Service 4 has to push its data for all the calls from 1,2,3 as every call by 1,2,3 is being sampled.  
Hence if you look at service 4 sample rate it will be 100% instead of 1%.

---

<div class="post-metadata">

**Author:** ![axw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/axw/32/28197_2.png) [@axw](https://discuss.elastic.co/u/axw)\
**Post date:** [September 7, 2020, 6:22am UTC](https://discuss.elastic.co/t/how-does-sampling-work/247617/6 "2020-09-07T06:22:31Z")

</div>

> By this statement, we are essentially saying that service 2 sampling rate can shoot above its own configured rate of 0.01 since it has to adhere to caller's sampling decision.

The sampling rate configured for an agent only affects traces started at that agent. If the agent is continuing a trace that started elsewhere, it will honour the propagated sampling decision.

> Example Service 1 , 2 , 3 calling service 4.  
> Service 1 , 2 and 3 on 100% sampling rate and service 4 on 1% sampling rate.  
> Service 4 has to push its data for all the calls from 1,2,3 as every call by 1,2,3 is being sampled.  
> Hence if you look at service 4 sample rate it will be 100% instead of 1%.

Correct. The sampling rate is applied to traces as a whole, and not transactions specific to one service/agent. In this example, it sounds like no traces originate at Service 4; they all all originate at Service 1, and therefore are 100% sampled.

---

<div class="post-metadata">

**Author:** ![manohar\_deepu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manohar_deepu/32/72797_2.png) [@manohar\_deepu](https://discuss.elastic.co/u/manohar_deepu)\
**Post date:** [September 7, 2020, 7:15am UTC](https://discuss.elastic.co/t/how-does-sampling-work/247617/7 "2020-09-07T07:15:39Z")

</div>

Thank you. That clarifies my doubt.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 28, 2020, 3:15am UTC](https://discuss.elastic.co/t/how-does-sampling-work/247617/8 "2020-09-28T03:15:51Z")

</div>

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