Hello everyone! Today, we’re going to dive deep into OpenTelemetry (OTEL) sampling, a core feature of Observability. π While it would be great to collect all trace data generated by a system, it can be burdensome in terms of cost and performance. This is where ‘sampling’ comes in handy. Today, we’ll explore how to set up the traceidratio sampler, the most common method for selectively collecting a specific percentage of traces!

π§ What is Sampling?
Sampling is a technique for collecting only a representative subset of the total data. In OpenTelemetry, by selecting and sending only a portion of all traces to an analysis system, you can reduce data storage costs and system load. It’s like a public opinion poll, where you can understand overall trends with just a few samples!
π² The Start of a Probability Game: traceidratio Sampler
OpenTelemetry offers various sampling strategies, and among them, the most widely used is the traceidratio sampler. As the name suggests, it’s a method that determines sampling based on a ‘Ratio’ derived from the ‘Trace ID’.
The biggest advantage of this method is that it ensures trace consistency. π Once a trace is decided to be sampled, all its associated spans are collected together. Conversely, if it’s excluded from sampling, all related spans are also excluded. This ensures that collected traces always maintain a complete form, making them easy to analyze.
π οΈ Simple Configuration with Environment Variables
So, how do we actually set it up? Surprisingly, it can be configured with just two environment variables!
1οΈβ£ Specifying the Sampler Type: OTEL_TRACES_SAMPLER
First, you need to tell the SDK which sampler to use.
OTEL_TRACES_SAMPLER="traceidratio"
By setting it as above, the OpenTelemetry SDK will recognize, “Aha! From now on, I need to make sampling decisions based on the Trace ID!” π§
2οΈβ£ Setting the Sampling Ratio: OTEL_TRACES_SAMPLER_ARG
Next, you need to determine the ‘ratio’ of how much to sample. This involves setting the argument for the traceidratio sampler.
OTEL_TRACES_SAMPLER_ARG="0.1"
Here, 0.1 means 10%. If you want to sample 50%, set it to 0.5; if you want only 1%, set it to 0.01. Simple, right? ποΈ
β¨ The Synergy of Two Settings!
Now, let’s use these two environment variables together.
export OTEL_TRACES_SAMPLER="traceidratio"
export OTEL_TRACES_SAMPLER_ARG="0.1"
In an application configured like this, the following magical things happen:
- When a new request comes in and a trace starts, a unique Trace ID is generated.
- The OpenTelemetry SDK checks this Trace ID.
- Based on the configured ratio (0.1), it decides whether to sample this trace with a 10% probability.
- If it’s decided to be sampled, all spans with that Trace ID are collected and sent. π‘
- If it’s excluded, the spans related to that trace are not sent. ποΈ
By using the traceidratio sampler in this way, you can effectively control the amount of data while ensuring the completeness of traces needed for analysis. It’s a smart way to catch two rabbits with one stone: cost and data quality! π°π°
Conclusion
Today, we learned about the traceidratio sampling method, one of OpenTelemetry’s core features. Isn’t it fascinating that you can easily adjust data collection volume with just two lines of environment variable settings? Apply an appropriate sampling ratio to your system environment to build an efficient Observability environment!
Leave a Reply