|
23 | 23 | ) |
24 | 24 | from opentelemetry.sdk.resources import Resource |
25 | 25 |
|
| 26 | +from opentelemetry.sdk.trace.sampling import ( |
| 27 | + TraceIdRatioBased, |
| 28 | + ALWAYS_OFF, |
| 29 | + ALWAYS_ON, |
| 30 | + ParentBased, |
| 31 | +) |
| 32 | + |
26 | 33 | from azure.monitor.opentelemetry._constants import ( |
27 | 34 | _AZURE_APP_SERVICE_RESOURCE_DETECTOR_NAME, |
28 | 35 | _AZURE_VM_RESOURCE_DETECTOR_NAME, |
|
47 | 54 | RATE_LIMITED_SAMPLER, |
48 | 55 | FIXED_PERCENTAGE_SAMPLER, |
49 | 56 | ENABLE_TRACE_BASED_SAMPLING_ARG, |
| 57 | + SUPPORTED_OTEL_SAMPLERS, |
| 58 | + ALWAYS_OFF_SAMPLER, |
| 59 | + ALWAYS_ON_SAMPLER, |
| 60 | + TRACE_ID_RATIO_SAMPLER, |
| 61 | + PARENT_BASED_ALWAYS_ON_SAMPLER, |
| 62 | + PARENT_BASED_ALWAYS_OFF_SAMPLER, |
| 63 | + PARENT_BASED_TRACE_ID_RATIO_SAMPLER, |
| 64 | + SAMPLING_ARG, |
| 65 | + SAMPLER_TYPE, |
50 | 66 | ) |
51 | 67 | from azure.monitor.opentelemetry._types import ConfigurationValue |
52 | 68 | from azure.monitor.opentelemetry._version import VERSION |
@@ -145,6 +161,7 @@ def _default_resource(configurations): |
145 | 161 | configurations[RESOURCE_ARG] = Resource.create(configurations[RESOURCE_ARG].attributes) |
146 | 162 |
|
147 | 163 |
|
| 164 | +# pylint: disable=too-many-statements, disable=too-many-branches |
148 | 165 | def _default_sampling_ratio(configurations): |
149 | 166 | default_value = 1.0 |
150 | 167 | sampler_type = environ.get(OTEL_TRACES_SAMPLER) |
@@ -188,16 +205,75 @@ def _default_sampling_ratio(configurations): |
188 | 205 | ) |
189 | 206 | configurations[SAMPLING_RATIO_ARG] = default_value |
190 | 207 |
|
| 208 | + # Handle always_on sampler |
| 209 | + elif sampler_type == ALWAYS_ON_SAMPLER: |
| 210 | + configurations[SAMPLING_ARG] = 1.0 |
| 211 | + configurations[SAMPLER_TYPE] = ALWAYS_ON_SAMPLER |
| 212 | + |
| 213 | + # Handle always_off sampler |
| 214 | + elif sampler_type == ALWAYS_OFF_SAMPLER: |
| 215 | + configurations[SAMPLING_ARG] = 0.0 |
| 216 | + configurations[SAMPLER_TYPE] = ALWAYS_OFF_SAMPLER |
| 217 | + |
| 218 | + # Handle trace_id_ratio sampler |
| 219 | + elif sampler_type == TRACE_ID_RATIO_SAMPLER: |
| 220 | + try: |
| 221 | + sampler_value = float(sampler_arg) if sampler_arg is not None else default_value |
| 222 | + if sampler_value < 0 or sampler_value > 1: |
| 223 | + _logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a value between 0 and 1.") |
| 224 | + sampler_value = default_value |
| 225 | + else: |
| 226 | + _logger.info("Using sampling value: %s", sampler_value) |
| 227 | + configurations[SAMPLING_ARG] = sampler_value |
| 228 | + except ValueError as e: |
| 229 | + _logger.error( # pylint: disable=C |
| 230 | + _INVALID_FLOAT_MESSAGE, |
| 231 | + OTEL_TRACES_SAMPLER_ARG, |
| 232 | + default_value, |
| 233 | + e, |
| 234 | + ) |
| 235 | + configurations[SAMPLING_ARG] = sampler_value |
| 236 | + configurations[SAMPLER_TYPE] = TRACE_ID_RATIO_SAMPLER |
| 237 | + |
| 238 | + # Handle parentbased_always_on sampler |
| 239 | + elif sampler_type == PARENT_BASED_ALWAYS_ON_SAMPLER: |
| 240 | + configurations[SAMPLING_ARG] = 1.0 |
| 241 | + configurations[SAMPLER_TYPE] = PARENT_BASED_ALWAYS_ON_SAMPLER |
| 242 | + |
| 243 | + # Handle parentbased_always_off sampler |
| 244 | + elif sampler_type == PARENT_BASED_ALWAYS_OFF_SAMPLER: |
| 245 | + configurations[SAMPLING_ARG] = 0.0 |
| 246 | + configurations[SAMPLER_TYPE] = PARENT_BASED_ALWAYS_OFF_SAMPLER |
| 247 | + |
| 248 | + # Handle parentbased_traceidratio sampler |
| 249 | + elif sampler_type == PARENT_BASED_TRACE_ID_RATIO_SAMPLER: |
| 250 | + try: |
| 251 | + sampler_value = float(sampler_arg) if sampler_arg is not None else default_value |
| 252 | + if sampler_value < 0 or sampler_value > 1: |
| 253 | + _logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a value between 0 and 1.") |
| 254 | + sampler_value = default_value |
| 255 | + else: |
| 256 | + _logger.info("Using sampling value: %s", sampler_value) |
| 257 | + configurations[SAMPLING_ARG] = sampler_value |
| 258 | + except ValueError as e: |
| 259 | + _logger.error( # pylint: disable=C |
| 260 | + _INVALID_FLOAT_MESSAGE, |
| 261 | + OTEL_TRACES_SAMPLER_ARG, |
| 262 | + default_value, |
| 263 | + e, |
| 264 | + ) |
| 265 | + configurations[SAMPLING_ARG] = sampler_value |
| 266 | + configurations[SAMPLER_TYPE] = PARENT_BASED_TRACE_ID_RATIO_SAMPLER |
| 267 | + |
191 | 268 | # Handle all other cases (no sampler type specified or unsupported sampler type) |
192 | 269 | else: |
193 | 270 | if configurations.get(SAMPLING_RATIO_ARG) is None: |
194 | 271 | configurations[SAMPLING_RATIO_ARG] = default_value |
195 | 272 | if sampler_type is not None: |
196 | 273 | _logger.error( # pylint: disable=C |
197 | 274 | "Invalid argument for the sampler to be used for tracing. " |
198 | | - "Supported values are %s and %s. Defaulting to %s: %s", |
199 | | - RATE_LIMITED_SAMPLER, |
200 | | - FIXED_PERCENTAGE_SAMPLER, |
| 275 | + "Supported values are %s. Defaulting to %s: %s", |
| 276 | + SUPPORTED_OTEL_SAMPLERS, |
201 | 277 | FIXED_PERCENTAGE_SAMPLER, |
202 | 278 | configurations[SAMPLING_RATIO_ARG], |
203 | 279 | ) |
@@ -259,3 +335,19 @@ def _is_instrumentation_enabled(configurations, lib_name): |
259 | 335 |
|
260 | 336 | def _default_enable_trace_based_sampling(configurations): |
261 | 337 | configurations.setdefault(ENABLE_TRACE_BASED_SAMPLING_ARG, False) |
| 338 | + |
| 339 | + |
| 340 | +def _get_sampler_from_name(sampler_type, sampler_arg): |
| 341 | + if sampler_type == ALWAYS_ON_SAMPLER: |
| 342 | + return ALWAYS_ON |
| 343 | + if sampler_type == ALWAYS_OFF_SAMPLER: |
| 344 | + return ALWAYS_OFF |
| 345 | + if sampler_type == TRACE_ID_RATIO_SAMPLER: |
| 346 | + ratio = float(sampler_arg) if sampler_arg is not None else 1.0 |
| 347 | + return TraceIdRatioBased(ratio) |
| 348 | + if sampler_type == PARENT_BASED_ALWAYS_OFF_SAMPLER: |
| 349 | + return ParentBased(ALWAYS_OFF) |
| 350 | + if sampler_type == PARENT_BASED_TRACE_ID_RATIO_SAMPLER: |
| 351 | + ratio = float(sampler_arg) if sampler_arg is not None else 1.0 |
| 352 | + return ParentBased(TraceIdRatioBased(ratio)) |
| 353 | + return ParentBased(ALWAYS_ON) |
0 commit comments