Skip to content

Commit e5256a3

Browse files
committed
Fix the format of the fixed percentage sampler constant and ensure backward compatability
1 parent b6b227e commit e5256a3

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

sdk/monitor/azure-monitor-opentelemetry/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ You can configure further with [OpenTelemetry environment variables][ot_env_vars
8282
| `OTEL_TRACES_EXPORTER` | If set to `None`, disables collection and export of distributed tracing telemetry. |
8383
| `OTEL_BLRP_SCHEDULE_DELAY` | Specifies the logging export interval in milliseconds. Defaults to 5000. |
8484
| `OTEL_BSP_SCHEDULE_DELAY` | Specifies the distributed tracing export interval in milliseconds. Defaults to 5000. |
85-
| `OTEL_TRACES_SAMPLER` | Specifies the sampler to be used for traces. Supports `always_on`, `always_off`, `trace_id_ratio`, `parentbased_always_on`, `parentbased_always_off`, `parentbased_trace_id_ratio`, [application_insights_sampling] and [rate_limited_sampling]. Use `microsoft.fixed.percentage` for the Application Insights sampler or `microsoft.rate_limited` for the Rate Limited sampler. |
85+
| `OTEL_TRACES_SAMPLER` | Specifies the sampler to be used for traces. Supports `always_on`, `always_off`, `trace_id_ratio`, `parentbased_always_on`, `parentbased_always_off`, `parentbased_trace_id_ratio`, [application_insights_sampling] and [rate_limited_sampling]. Use `microsoft.fixed_percentage` for the Application Insights sampler or `microsoft.rate_limited` for the Rate Limited sampler. |
8686
| `OTEL_TRACES_SAMPLER_ARG` | Specifies the sampling parameter for the configured sampler. For the standard OpenTelemetry samplers `trace_id_ratio` and `parentbased_trace_id_ratio`, this is the sampling ratio in the range [0.0, 1.0]. Not needed to be specified for `always_on`, `always_off`, `parentbased_always_on`, or `parentbased_always_off` samplers. For the Application Insights sampler, this sets the ratio of distributed tracing telemetry to be [sampled][application_insights_sampling] with accepted values in the range [0,1]. Defaults to 1.0 (no sampling). For the Rate Limited sampler, this sets the maximum traces per second to be [sampled][rate_limited_sampler]. For example, 0.5 means one trace every two seconds, while 5.0 means five traces per second. |
8787
| `OTEL_PYTHON_DISABLED_INSTRUMENTATIONS` | Specifies which of the supported instrumentations to disable. Disabled instrumentations will not be instrumented as part of `configure_azure_monitor`. However, they can still be manually instrumented with `instrument()` directly. Accepts a comma-separated list of lowercase [Library Names](#officially-supported-instrumentations). For example, set to `"psycopg2,fastapi"` to disable the Psycopg2 and FastAPI instrumentations. Defaults to an empty list, enabling all supported instrumentations. |
8888
| `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` | An experimental OpenTelemetry environment variable used to specify Resource Detectors to be used to generate Resource Attributes. This is an experimental feature and the name of this variable and its behavior can change in a non-backwards compatible way. Defaults to "azure_app_service,azure_vm" to enable the [Azure Resource Detectors][ot_resource_detector_azure] for Azure App Service and Azure VM. To add or remove specific resource detectors, set the environment variable accordingly. See the [OpenTelemetry Python Resource Detector Documentation][ot_python_resource_detectors] for more. |

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
METRIC_READERS_ARG = "metric_readers"
2929
VIEWS_ARG = "views"
3030
RATE_LIMITED_SAMPLER = "microsoft.rate_limited"
31-
FIXED_PERCENTAGE_SAMPLER = "microsoft.fixed.percentage"
31+
FIXED_PERCENTAGE_SAMPLER = "microsoft.fixed_percentage"
3232
SAMPLING_TRACES_PER_SECOND_ARG = "traces_per_second"
3333
ENABLE_TRACE_BASED_SAMPLING_ARG = "enable_trace_based_sampling_for_logs"
3434
SAMPLER_TYPE = "sampler_type"

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_utils/configurations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def _default_sampling_ratio(configurations):
174174
# Handle rate-limited sampler
175175
if sampler_type == RATE_LIMITED_SAMPLER:
176176
try:
177-
sampler_value = float(sampler_arg)
177+
sampler_value = float(sampler_arg) if sampler_arg is not None else default_value
178178
if sampler_value < 0.0:
179179
_logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a non-negative number.")
180180
sampler_value = default_value
@@ -191,9 +191,9 @@ def _default_sampling_ratio(configurations):
191191
configurations[SAMPLING_TRACES_PER_SECOND_ARG] = default_value
192192

193193
# Handle fixed percentage sampler
194-
elif sampler_type == FIXED_PERCENTAGE_SAMPLER:
194+
elif sampler_type in (FIXED_PERCENTAGE_SAMPLER,"microsoft.fixed.percentage"): # to support older string
195195
try:
196-
sampler_value = float(sampler_arg)
196+
sampler_value = float(sampler_arg) if sampler_arg is not None else default_value
197197
if sampler_value < 0.0:
198198
_logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a non-negative number.")
199199
sampler_value = default_value

sdk/monitor/azure-monitor-opentelemetry/samples/tracing/sampling_configurations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# 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)
3333

3434
# Using fixed percentage sampler
35-
# Set the OTEL_TRACES_SAMPLER environment variable to "microsoft.fixed.percentage"
35+
# Set the OTEL_TRACES_SAMPLER environment variable to "microsoft.fixed_percentage"
3636
# 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
3737

3838
# Using trace_based_sampling configuration # cspell: ignore unsampled

sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_configurations.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,26 @@ def test_get_configurations_env_vars_validation(self, resource_create_mock):
221221
self.assertEqual(configurations["disable_tracing"], False)
222222
self.assertEqual(configurations["sampling_ratio"], 1.0)
223223

224+
@patch.dict(
225+
"os.environ",
226+
{
227+
OTEL_TRACES_SAMPLER: "microsoft.fixed.percentage",
228+
OTEL_TRACES_SAMPLER_ARG: "10.45",
229+
OTEL_TRACES_EXPORTER: "False",
230+
OTEL_LOGS_EXPORTER: "no",
231+
OTEL_METRICS_EXPORTER: "True",
232+
},
233+
clear=True,
234+
)
235+
@patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_DEFAULT_RESOURCE)
236+
def test_get_configurations_env_vars_validation_check_backward_compatibility(self, resource_create_mock):
237+
configurations = _get_configurations()
238+
self.assertTrue("connection_string" not in configurations)
239+
self.assertEqual(configurations["disable_logging"], False)
240+
self.assertEqual(configurations["disable_metrics"], False)
241+
self.assertEqual(configurations["disable_tracing"], False)
242+
self.assertEqual(configurations["sampling_ratio"], 10.45)
243+
224244
@patch.dict(
225245
"os.environ",
226246
{

0 commit comments

Comments
 (0)