Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion sdk/monitor/azure-monitor-opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
{
Expand Down