# Control Sample Rate or \`recorded\` Flag Per Request

**URL:** <https://discuss.elastic.co/t/control-sample-rate-or-recorded-flag-per-request/177145>\
**Category:** APM\
**Created:** [April 16, 2019, 7:36pm UTC](https://discuss.elastic.co/t/control-sample-rate-or-recorded-flag-per-request/177145 "2019-04-16T19:36:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dvisz-inf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dvisz-inf/32/42617_2.png) [@dvisz-inf](https://discuss.elastic.co/u/dvisz-inf)\
**Post date:** [April 16, 2019, 7:36pm UTC](https://discuss.elastic.co/t/control-sample-rate-or-recorded-flag-per-request/177145/1 "2019-04-16T19:36:41Z")

</div>

In a production setting, I'd like to be able to control my sample rate, or simply enable recording of a distributed trace, on a per request basis.

Is this possible using the existing agents?

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [April 17, 2019, 6:10am UTC](https://discuss.elastic.co/t/control-sample-rate-or-recorded-flag-per-request/177145/2 "2019-04-17T06:10:10Z")

</div>

Hi and thanks for another great question 🙂

I don't think our agents currently support that. One challenge is that the sampling decision is made up front and when trying to change the sampling afterwards, there could be some spans which are already discarded or sent to the APM Server.

Could you elaborate on which conditions you would use to base the sampling decision on?

Thanks,  
Felix

---

<div class="post-metadata">

**Author:** ![dvisz-inf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dvisz-inf/32/42617_2.png) [@dvisz-inf](https://discuss.elastic.co/u/dvisz-inf)\
**Post date:** [April 17, 2019, 10:32pm UTC](https://discuss.elastic.co/t/control-sample-rate-or-recorded-flag-per-request/177145/3 "2019-04-17T22:32:56Z")

</div>

> [@felixbarny](#):
>
> One challenge is that the sampling decision is made up front

> [@felixbarny](#):
>
> Could you elaborate on which conditions you would use to base the sampling decision on?

At the front-end making the initial request 🙂

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [April 18, 2019, 7:47am UTC](https://discuss.elastic.co/t/control-sample-rate-or-recorded-flag-per-request/177145/4 "2019-04-18T07:47:33Z")

</div>

Are you creating the transactions yourself, with the agent's API, or do you rely on the auto instrumentation of the RUM agent? To you want to influence sampling for the initial page load or for subsequent AJAX requests? How and based on which properties would you decide whether to sample or not?

---

<div class="post-metadata">

**Author:** ![dvisz-inf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dvisz-inf/32/42617_2.png) [@dvisz-inf](https://discuss.elastic.co/u/dvisz-inf)\
**Post date:** [April 18, 2019, 8:36pm UTC](https://discuss.elastic.co/t/control-sample-rate-or-recorded-flag-per-request/177145/5 "2019-04-18T20:36:11Z")

</div>

On the front-end, I am currently doing nothing, and do not have near-term plans for using the full RUM.

Transactions are created automatically by APM agents in my services.

The front-end currently doesn't know about transactions, and ideally I'd like to restrict its knowledge about them to simply setting a header if possible.

I naively thought that if I just generated a valid `traceparent` header with the `recorded` flag set and included that in my HTTP request, that my services would pick that header up and use it. Is this not the case?

I want to be able to instrument individual requests a la carte at the moment. In the future I will investigate using RUM to instrument multi-request page loads and such, but for now the scope I am concerned with is smaller!

Thanks!

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [April 22, 2019, 4:29am UTC](https://discuss.elastic.co/t/control-sample-rate-or-recorded-flag-per-request/177145/6 "2019-04-22T04:29:03Z")

</div>

The `traceparent` header is meant for creating traces to support distributed tracing, so it is expected to contain a valid parent ID. Since you are not using the RUM agent, there is no parent span in your case.

However, you describe an interesting use case of forcing the backend agent to trace some of the transactions. We can consider using the existing header for that, but it will probably make more sense to use a different and simpler header format for that.  
Ideally, how would you expect this feature to look like?

---

<div class="post-metadata">

**Author:** ![dvisz-inf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dvisz-inf/32/42617_2.png) [@dvisz-inf](https://discuss.elastic.co/u/dvisz-inf)\
**Post date:** [April 23, 2019, 12:13am UTC](https://discuss.elastic.co/t/control-sample-rate-or-recorded-flag-per-request/177145/7 "2019-04-23T00:13:24Z")

</div>

> [@Eyal\_Koren](#):
>
> Since you are not using the RUM agent, there is no parent span in your case.

Will APM Server discard any transactions or spans which belong to a distributed trace for which the initial transaction/span has not been received by APM? I had kind of hoped that I could just create a valid `traceparent` value and send it with my initial request, and that would be all that was needed.

> [@Eyal\_Koren](#):
>
> Ideally, how would you expect this feature to look like?

I'd like it to be easier to decide myself when or how often I will record a distributed trace.

The simplest solution I think would be if APM Server did not discard a transaction or span which references a parent that doesn't exist. Perhaps that would lead to complications that I don't see, though!

I think another similar issue that I have is that the current W3 draft has no bits reserved for tuning the level of the trace. See here:

> [@Trace Level and Reserved Traceparent Flags](https://discuss.elastic.co/t/trace-level-and-reserved-traceparent-flags/176506):
>
> The W3 draft says The behavior of other flags, such as ( 00000100 ) is not defined and reserved for future use. Implementation MUST set those to zero. [https://www.w3.org/TR/trace-context/#other-flags](https://www.w3.org/TR/trace-context/#other-flags) I'm pretty sad about this. I have other trace flags that I'd like to have available to me, like controlling the level of trace detail, or the recording other diagnostic information. Does anyone else feel like this is a missed opportunity? What are the chances that some of those bits could be …

It has been suggested to use a `tracestate` header to transmit this information, and if Elastic APM agents propagate this header to all participants in a distributed trace, then it seems that that will problem will be solved.

---

<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:** [April 23, 2019, 2:58am UTC](https://discuss.elastic.co/t/control-sample-rate-or-recorded-flag-per-request/177145/8 "2019-04-23T02:58:07Z")

</div>

> [@dvisz-inf](#):
>
> Will APM Server discard any transactions or spans which belong to a distributed trace for which the initial transaction/span has not been received by APM? I had kind of hoped that I could just create a valid `traceparent` value and send it with my initial request, and that would be all that was needed.

Orphaned transactions/spans will not be discarded by the server - not currently anyway. It is however generally expected that if a parent ID is given that the parent should exist, so I'm not sure that we could guarantee that will hold true forever.

It sounds like you want something like [Zipkin's debug flag](https://github.com/apache/incubator-zipkin-b3-propagation#debug-flag), but perhaps with a bit more control? If the agents provided a way of passing a sampler into "startTransaction", would that satisfy your needs?

We're currently considering extending sampling configuration. If your criteria for deciding how to sample is something that could be captured in configuration, it would be of great interest to us to hear more details.

---

<div class="post-metadata">

**Author:** ![dvisz-inf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dvisz-inf/32/42617_2.png) [@dvisz-inf](https://discuss.elastic.co/u/dvisz-inf)\
**Post date:** [April 23, 2019, 8:32pm UTC](https://discuss.elastic.co/t/control-sample-rate-or-recorded-flag-per-request/177145/9 "2019-04-23T20:32:40Z")

</div>

Hmm... if they are not discarded, is it possible that they're there in ES, but won't show up in Kibana APM?

Zipkin's debug flag would be great, but what would be even greater would be if I could define my own flags. Propagation of the `tracestate` header should give me this.

I'd like for my sampling criteria to be something I can tweak on a per-request basis, where the request is the "first" request in a distributed trace, usually issued by a front-end. This flexibility would allow me to troubleshoot problems on an ad hoc basis by adding some sign to my first request, probably usually in an HTTP header.

I hope that helps clarify

---

<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:** [May 14, 2019, 4:32pm UTC](https://discuss.elastic.co/t/control-sample-rate-or-recorded-flag-per-request/177145/10 "2019-05-14T16:32:50Z")

</div>

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