diff --git a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md index 7f488ed1cb68..c88457fdd1aa 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md @@ -7,6 +7,8 @@ ### Breaking Changes ### Bugs Fixed +- Fix the format of the fixed percentage sampler constant and ensure backward compatability + ([#44656](https://github.com/Azure/azure-sdk-for-python/pull/44656)) ### Other Changes diff --git a/sdk/monitor/azure-monitor-opentelemetry/README.md b/sdk/monitor/azure-monitor-opentelemetry/README.md index 9b6e1bdbd0f8..03bf5325ab24 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/README.md +++ b/sdk/monitor/azure-monitor-opentelemetry/README.md @@ -82,7 +82,7 @@ You can configure further with [OpenTelemetry environment variables][ot_env_vars | `OTEL_TRACES_EXPORTER` | If set to `None`, disables collection and export of distributed tracing telemetry. | | `OTEL_BLRP_SCHEDULE_DELAY` | Specifies the logging export interval in milliseconds. Defaults to 5000. | | `OTEL_BSP_SCHEDULE_DELAY` | Specifies the distributed tracing export interval in milliseconds. Defaults to 5000. | -| `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. | +| `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. | | `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. | | `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. | | `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. | diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py index 236120fe4f2f..2cee5d093a4a 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py @@ -28,7 +28,7 @@ METRIC_READERS_ARG = "metric_readers" VIEWS_ARG = "views" RATE_LIMITED_SAMPLER = "microsoft.rate_limited" -FIXED_PERCENTAGE_SAMPLER = "microsoft.fixed.percentage" +FIXED_PERCENTAGE_SAMPLER = "microsoft.fixed_percentage" SAMPLING_TRACES_PER_SECOND_ARG = "traces_per_second" ENABLE_TRACE_BASED_SAMPLING_ARG = "enable_trace_based_sampling_for_logs" SAMPLER_TYPE = "sampler_type" diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_utils/configurations.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_utils/configurations.py index 8d825129b3a4..de694de31e57 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_utils/configurations.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_utils/configurations.py @@ -174,7 +174,7 @@ def _default_sampling_ratio(configurations): # Handle rate-limited sampler if sampler_type == RATE_LIMITED_SAMPLER: try: - sampler_value = float(sampler_arg) + sampler_value = float(sampler_arg) if sampler_arg is not None else default_value if sampler_value < 0.0: _logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a non-negative number.") sampler_value = default_value @@ -191,11 +191,11 @@ def _default_sampling_ratio(configurations): configurations[SAMPLING_TRACES_PER_SECOND_ARG] = default_value # Handle fixed percentage sampler - elif sampler_type == FIXED_PERCENTAGE_SAMPLER: + elif sampler_type in (FIXED_PERCENTAGE_SAMPLER, "microsoft.fixed.percentage"): # to support older string try: - sampler_value = float(sampler_arg) - if sampler_value < 0.0: - _logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a non-negative number.") + sampler_value = float(sampler_arg) if sampler_arg is not None else default_value + if sampler_value < 0.0 or sampler_value > 1.0: + _logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a value between 0 and 1.") sampler_value = default_value else: _logger.info("Using sampling ratio: %s", sampler_value) diff --git a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/sampling_configurations.py b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/sampling_configurations.py index 72627731f3b9..bb7e151a82b0 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/sampling_configurations.py +++ b/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/sampling_configurations.py @@ -32,7 +32,7 @@ # 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 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 diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_configurations.py b/sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_configurations.py index 336bcc767ece..7d2d22f22ff5 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_configurations.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_configurations.py @@ -221,6 +221,26 @@ def test_get_configurations_env_vars_validation(self, resource_create_mock): self.assertEqual(configurations["disable_tracing"], False) self.assertEqual(configurations["sampling_ratio"], 1.0) + @patch.dict( + "os.environ", + { + OTEL_TRACES_SAMPLER: "microsoft.fixed.percentage", + OTEL_TRACES_SAMPLER_ARG: "10.45", + OTEL_TRACES_EXPORTER: "False", + OTEL_LOGS_EXPORTER: "no", + OTEL_METRICS_EXPORTER: "True", + }, + clear=True, + ) + @patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_DEFAULT_RESOURCE) + def test_get_configurations_env_vars_validation_check_backward_compatibility(self, resource_create_mock): + configurations = _get_configurations() + self.assertTrue("connection_string" not in configurations) + self.assertEqual(configurations["disable_logging"], False) + self.assertEqual(configurations["disable_metrics"], False) + self.assertEqual(configurations["disable_tracing"], False) + self.assertEqual(configurations["sampling_ratio"], 1.0) + @patch.dict( "os.environ", {