-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add support for OTEL_TRACES_SAMPLER #44535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
306d7fa
a865d35
3de7407
e35f233
db59955
43243ca
46944d7
8b952e7
70f8d38
16f469e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,8 @@ | |
| METRIC_READERS_ARG, | ||
| VIEWS_ARG, | ||
| ENABLE_TRACE_BASED_SAMPLING_ARG, | ||
| SAMPLING_ARG, | ||
| SAMPLER_TYPE, | ||
| ) | ||
| from azure.monitor.opentelemetry._types import ConfigurationValue | ||
| from azure.monitor.opentelemetry.exporter._quickpulse import ( # pylint: disable=import-error,no-name-in-module | ||
|
|
@@ -75,6 +77,7 @@ | |
| from azure.monitor.opentelemetry._utils.configurations import ( | ||
| _get_configurations, | ||
| _is_instrumentation_enabled, | ||
| _get_sampler_from_name, | ||
| ) | ||
| from azure.monitor.opentelemetry._utils.instrumentation import ( | ||
| get_dist_dependency_conflicts, | ||
|
|
@@ -155,7 +158,12 @@ def configure_azure_monitor(**kwargs) -> None: # pylint: disable=C4758 | |
| def _setup_tracing(configurations: Dict[str, ConfigurationValue]): | ||
| resource: Resource = configurations[RESOURCE_ARG] # type: ignore | ||
| enable_performance_counters_config = configurations[ENABLE_PERFORMANCE_COUNTERS_ARG] | ||
| if SAMPLING_TRACES_PER_SECOND_ARG in configurations: | ||
| if SAMPLING_ARG in configurations: | ||
| sampler_arg = configurations[SAMPLING_ARG] | ||
| sampler_type = configurations[SAMPLER_TYPE] | ||
| sampler = _get_sampler_from_name(sampler_type, sampler_arg) | ||
| tracer_provider = TracerProvider(sampler=sampler, resource=resource) | ||
| elif SAMPLING_TRACES_PER_SECOND_ARG in configurations: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disto sampling now has a lot of different and overlapping ways of configuring the sampler. Could you add one more samples to the repo?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I will include all the possible ways to set it. |
||
| traces_per_second = configurations[SAMPLING_TRACES_PER_SECOND_ARG] | ||
| tracer_provider = TracerProvider( | ||
| sampler=RateLimitedSampler(target_spans_per_second_limit=cast(float, traces_per_second)), resource=resource | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from azure.monitor.opentelemetry import configure_azure_monitor | ||
| from opentelemetry import trace | ||
|
|
||
| # Using always_on sampler | ||
| # Set the OTEL_TRACES_SAMPLER environment variable to "always_on" | ||
| # The sampling rate is 1.0, so 100% of the traces are sampled. | ||
|
|
||
| # Using always_off sampler | ||
| # Set the OTEL_TRACES_SAMPLER environment variable to "always_off" | ||
| # The sampling rate is 0.0, so None of the traces are sampled. | ||
|
|
||
| # Using trace_id_ratio sampler | ||
| # Set the OTEL_TRACES_SAMPLER environment variable to "trace_id_ratio" | ||
| # Set the OTEL_TRACES_SAMPLER_ARG environment variable to 0.1, it has to be a number between 0 and 1, else it will throw an error and default to 1.0 | ||
| # The sampling rate is 0.1 means approximately 10% of your traces are sent | ||
|
|
||
| # Using parentbased_always_on sampler | ||
| # Set the OTEL_TRACES_SAMPLER environment variable to "parentbased_always_on" | ||
| # The sampling rate is 1.0, so 100% of the traces are sampled. | ||
|
|
||
| # Using parentbased_always_off sampler | ||
| # Set the OTEL_TRACES_SAMPLER environment variable to "parentbased_always_off" | ||
| # The sampling rate is 0.0, so None of the traces are sampled. | ||
|
|
||
| # Using parentbased_trace_id_ratio sampler | ||
| # Set the OTEL_TRACES_SAMPLER environment variable to "parentbased_trace_id_ratio" | ||
| # Set the OTEL_TRACES_SAMPLER_ARG environment variable to 0.45, it has to be a number between 0 and 1, else it will throw an error and default to 1.0 | ||
| # The sampling rate is 0.45 means approximately 45% of your traces are sent | ||
|
|
||
| # Using rate limited sampler | ||
| # Set the OTEL_TRACES_SAMPLER environment variable to "microsoft.rate_limited" | ||
| # Set the OTEL_TRACES_SAMPLER_ARG environment variable to the desired rate limit (e.g., 0.5 means one trace every two seconds, while 5.0 means five traces per second) | ||
|
|
||
| # Using fixed percentage sampler | ||
| # Set the OTEL_TRACES_SAMPLER environment variable to "microsoft.fixed.percentage" | ||
| # Set the OTEL_TRACES_SAMPLER_ARG environment variable to 0.2, it has to be a number between 0 and 1, else it will throw an error and default to 1.0 | ||
|
|
||
| # Using trace_based_sampling configuration # cspell: ignore unsampled | ||
| # Determines whether the logger should drop log records associated with unsampled traces. | ||
| # Passing the enable_trace_based_sampling_for_logs=True argument to configure_azure_monitor ensure that log records associated with unsampled traces are dropped by the `Logger`. | ||
| # A log record is considered associated with an unsampled trace if it has a valid `SpanId` and its `TraceFlags` indicate that the trace is unsampled. | ||
| # The value of this config is False by default | ||
|
|
||
| """ | ||
| configure_azure_monitor ( | ||
| "enable_trace_based_sampling_for_logs": True, | ||
| ) | ||
| """ | ||
|
|
||
| configure_azure_monitor() | ||
|
|
||
| tracer = trace.get_tracer(__name__) | ||
|
|
||
| for i in range(100): | ||
| with tracer.start_as_current_span("hello"): | ||
| print("Hello, World!") | ||
|
|
||
| input() |
Uh oh!
There was an error while loading. Please reload this page.