|
| 1 | +--- |
| 2 | +title: Sampling |
| 3 | +description: Configure sampling in OpenTelemetry .NET |
| 4 | +weight: 50 |
| 5 | +--- |
| 6 | + |
| 7 | +Sampling controls which traces are recorded and exported. By reducing the number |
| 8 | +of collected spans, sampling helps control overhead and telemetry volume. |
| 9 | + |
| 10 | +In OpenTelemetry, sampling decisions are typically made when a trace is started |
| 11 | +(head-based sampling) and then propagated to downstream services through |
| 12 | +context. |
| 13 | + |
| 14 | +## Configure a sampler |
| 15 | + |
| 16 | +In .NET, configure sampling on the `TracerProvider` using `SetSampler`: |
| 17 | + |
| 18 | +```csharp |
| 19 | +using OpenTelemetry; |
| 20 | +using OpenTelemetry.Trace; |
| 21 | + |
| 22 | +var tracerProvider = Sdk.CreateTracerProviderBuilder() |
| 23 | + .SetSampler(new TraceIdRatioBasedSampler(0.25)) |
| 24 | + .Build(); |
| 25 | +``` |
| 26 | + |
| 27 | +This example samples approximately 25% of traces. |
| 28 | + |
| 29 | +## Built-in samplers |
| 30 | + |
| 31 | +The OpenTelemetry .NET SDK provides several built-in sampler implementations. |
| 32 | +These can be configured in code or via environment variables. |
| 33 | + |
| 34 | +### AlwaysOn |
| 35 | + |
| 36 | +Samples every trace. |
| 37 | + |
| 38 | +Useful in development or debugging environments where complete visibility is |
| 39 | +required. |
| 40 | + |
| 41 | +### AlwaysOff |
| 42 | + |
| 43 | +Samples no traces. |
| 44 | + |
| 45 | +Useful for disabling tracing without removing instrumentation. |
| 46 | + |
| 47 | +### TraceIdRatioBased |
| 48 | + |
| 49 | +Samples traces based on a fixed probability. |
| 50 | + |
| 51 | +```csharp |
| 52 | +.SetSampler(new TraceIdRatioBasedSampler(0.1)) // 10% |
| 53 | +``` |
| 54 | + |
| 55 | +Commonly used in production to reduce telemetry volume while still capturing a |
| 56 | +representative subset of traces. |
| 57 | + |
| 58 | +### ParentBased |
| 59 | + |
| 60 | +Uses the sampling decision of the parent span when one exists. |
| 61 | + |
| 62 | +If there is no parent, it delegates to a root sampler. This helps ensure |
| 63 | +consistent sampling decisions across distributed services that participate in |
| 64 | +the same trace. |
| 65 | + |
| 66 | +## Default sampler |
| 67 | + |
| 68 | +By default, the .NET SDK uses a parent-based sampler with an always-on root |
| 69 | +sampler. |
| 70 | + |
| 71 | +This means: |
| 72 | + |
| 73 | +- new root traces are sampled |
| 74 | +- child spans follow the parent’s sampling decision |
| 75 | + |
| 76 | +## Environment variable configuration |
| 77 | + |
| 78 | +Sampling can also be configured using environment variables, which is useful for |
| 79 | +containerized and cloud native deployments. |
| 80 | + |
| 81 | +### OTEL_TRACES_SAMPLER |
| 82 | + |
| 83 | +Specifies which sampler to use. |
| 84 | + |
| 85 | +Common values include: |
| 86 | + |
| 87 | +- `always_on` |
| 88 | +- `always_off` |
| 89 | +- `traceidratio` |
| 90 | +- `parentbased_always_on` |
| 91 | +- `parentbased_always_off` |
| 92 | +- `parentbased_traceidratio` |
| 93 | + |
| 94 | +### OTEL_TRACES_SAMPLER_ARG |
| 95 | + |
| 96 | +Provides an argument for the configured sampler. |
| 97 | + |
| 98 | +For example: |
| 99 | + |
| 100 | +```bash |
| 101 | +OTEL_TRACES_SAMPLER=traceidratio |
| 102 | +OTEL_TRACES_SAMPLER_ARG=0.25 |
| 103 | +``` |
| 104 | + |
| 105 | +This configures a 25% sampling rate. |
| 106 | + |
| 107 | +## Production guidance |
| 108 | + |
| 109 | +In development, using an always-on sampler is often acceptable. |
| 110 | + |
| 111 | +In production, a common approach is to use parent-based sampling with a |
| 112 | +ratio-based root sampler. This balances telemetry volume with trace consistency |
| 113 | +across services. |
| 114 | + |
| 115 | +If you need to make sampling decisions based on completed traces (for example, |
| 116 | +keeping only slow or error traces), use tail-based sampling in the OpenTelemetry |
| 117 | +Collector. |
| 118 | + |
| 119 | +## Further reading |
| 120 | + |
| 121 | +- [Sampling concepts](/docs/concepts/sampling/) |
| 122 | +- [Tracing in .NET](/docs/languages/dotnet/traces/) |
0 commit comments