Skip to content

Commit e63d0e1

Browse files
committed
Added tests and modified README
1 parent 650b6b5 commit e63d0e1

File tree

3 files changed

+96
-12
lines changed

3 files changed

+96
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ You can configure further with [OpenTelemetry environment variables][ot_env_vars
8181
| `OTEL_BLRP_SCHEDULE_DELAY` | Specifies the logging export interval in milliseconds. Defaults to 5000. |
8282
| `OTEL_BSP_SCHEDULE_DELAY` | Specifies the distributed tracing export interval in milliseconds. Defaults to 5000. |
8383
| `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. |
84-
| `OTEL_TRACES_SAMPLER_ARG` | Specifies the sampling parameter for the configured sampler. 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. |
84+
| `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. |
8585
| `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. |
8686
| `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. |
8787

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def _default_resource(configurations):
161161
configurations[RESOURCE_ARG] = Resource.create(configurations[RESOURCE_ARG].attributes)
162162

163163

164-
# pylint: disable=too-many-statements, disable=too-many-branches
164+
# pylint: disable=too-many-statements,too-many-branches
165165
def _default_sampling_ratio(configurations):
166166
default_value = 1.0
167167
sampler_type = environ.get(OTEL_TRACES_SAMPLER)
@@ -171,7 +171,7 @@ def _default_sampling_ratio(configurations):
171171
if sampler_type == RATE_LIMITED_SAMPLER:
172172
try:
173173
sampler_value = float(sampler_arg)
174-
if sampler_value < 0:
174+
if sampler_value < 0.0:
175175
_logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a non-negative number.")
176176
sampler_value = default_value
177177
else:
@@ -190,7 +190,7 @@ def _default_sampling_ratio(configurations):
190190
elif sampler_type == FIXED_PERCENTAGE_SAMPLER:
191191
try:
192192
sampler_value = float(sampler_arg)
193-
if sampler_value < 0:
193+
if sampler_value < 0.0:
194194
_logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a non-negative number.")
195195
sampler_value = default_value
196196
else:
@@ -219,7 +219,7 @@ def _default_sampling_ratio(configurations):
219219
elif sampler_type == TRACE_ID_RATIO_SAMPLER:
220220
try:
221221
sampler_value = float(sampler_arg) if sampler_arg is not None else default_value
222-
if sampler_value < 0 or sampler_value > 1:
222+
if sampler_value < 0.0 or sampler_value > 1.0:
223223
_logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a value between 0 and 1.")
224224
sampler_value = default_value
225225
else:
@@ -232,7 +232,7 @@ def _default_sampling_ratio(configurations):
232232
default_value,
233233
e,
234234
)
235-
configurations[SAMPLING_ARG] = sampler_value
235+
configurations[SAMPLING_ARG] = default_value
236236
configurations[SAMPLER_TYPE] = TRACE_ID_RATIO_SAMPLER
237237

238238
# Handle parentbased_always_on sampler
@@ -245,11 +245,11 @@ def _default_sampling_ratio(configurations):
245245
configurations[SAMPLING_ARG] = 0.0
246246
configurations[SAMPLER_TYPE] = PARENT_BASED_ALWAYS_OFF_SAMPLER
247247

248-
# Handle parentbased_traceidratio sampler
248+
# Handle parentbased_trace_id_ratio sampler
249249
elif sampler_type == PARENT_BASED_TRACE_ID_RATIO_SAMPLER:
250250
try:
251251
sampler_value = float(sampler_arg) if sampler_arg is not None else default_value
252-
if sampler_value < 0 or sampler_value > 1:
252+
if sampler_value < 0.0 or sampler_value > 1.0:
253253
_logger.error("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a value between 0 and 1.")
254254
sampler_value = default_value
255255
else:
@@ -262,7 +262,7 @@ def _default_sampling_ratio(configurations):
262262
default_value,
263263
e,
264264
)
265-
configurations[SAMPLING_ARG] = sampler_value
265+
configurations[SAMPLING_ARG] = default_value
266266
configurations[SAMPLER_TYPE] = PARENT_BASED_TRACE_ID_RATIO_SAMPLER
267267

268268
# Handle all other cases (no sampler type specified or unsupported sampler type)

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

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
from opentelemetry.instrumentation.environment_variables import (
2626
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS,
2727
)
28+
from opentelemetry.sdk.trace.sampling import ALWAYS_OFF, ALWAYS_ON, ParentBased, TraceIdRatioBased
2829
from azure.monitor.opentelemetry._utils.configurations import (
2930
_get_configurations,
31+
_get_sampler_from_name,
3032
)
3133
from azure.monitor.opentelemetry._constants import LOGGER_NAME_ENV_ARG, LOGGING_FORMAT_ENV_ARG
3234
from azure.monitor.opentelemetry._constants import (
@@ -668,6 +670,24 @@ def test_get_configurations_env_vars_trace_id_ratio(self, resource_create_mock):
668670
self.assertEqual(configurations["resource"].attributes, TEST_DEFAULT_RESOURCE.attributes)
669671
self.assertEqual(configurations["sampling_arg"], 0.75)
670672
self.assertEqual(configurations["sampler_type"], "trace_id_ratio")
673+
674+
@patch.dict(
675+
"os.environ",
676+
{
677+
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS: "flask,requests,fastapi,azure_sdk",
678+
OTEL_TRACES_SAMPLER: TRACE_ID_RATIO_SAMPLER,
679+
OTEL_TRACES_SAMPLER_ARG: "sampler",
680+
},
681+
clear=True,
682+
)
683+
@patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_DEFAULT_RESOURCE)
684+
def test_get_configurations_env_vars_trace_id_ratio_non_numeric_value(self, resource_create_mock):
685+
configurations = _get_configurations()
686+
687+
self.assertTrue("connection_string" not in configurations)
688+
self.assertEqual(configurations["resource"].attributes, TEST_DEFAULT_RESOURCE.attributes)
689+
self.assertEqual(configurations["sampling_arg"], 1.0)
690+
self.assertEqual(configurations["sampler_type"], "trace_id_ratio")
671691

672692
@patch.dict(
673693
"os.environ",
@@ -694,6 +714,7 @@ def test_get_configurations_env_vars_parentbased_always_on(self, resource_create
694714
},
695715
clear=True,
696716
)
717+
697718
@patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_DEFAULT_RESOURCE)
698719
def test_get_configurations_env_vars_parentbased_always_off(self, resource_create_mock):
699720
configurations = _get_configurations()
@@ -703,6 +724,26 @@ def test_get_configurations_env_vars_parentbased_always_off(self, resource_creat
703724
self.assertEqual(configurations["sampling_arg"], 0.0)
704725
self.assertEqual(configurations["sampler_type"], "parentbased_always_off")
705726

727+
@patch.dict(
728+
"os.environ",
729+
{
730+
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS: "flask,requests,fastapi,azure_sdk",
731+
OTEL_TRACES_SAMPLER: PARENT_BASED_TRACE_ID_RATIO_SAMPLER,
732+
OTEL_TRACES_SAMPLER_ARG: "0.89",
733+
},
734+
clear=True,
735+
)
736+
@patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_DEFAULT_RESOURCE)
737+
def test_get_configurations_env_vars_parentbased_trace_id_ratio(
738+
self, resource_create_mock
739+
):
740+
configurations = _get_configurations()
741+
742+
self.assertTrue("connection_string" not in configurations)
743+
self.assertEqual(configurations["resource"].attributes, TEST_DEFAULT_RESOURCE.attributes)
744+
self.assertEqual(configurations["sampling_arg"], 0.89)
745+
self.assertEqual(configurations["sampler_type"], "parentbased_trace_id_ratio")
746+
706747
@patch.dict(
707748
"os.environ",
708749
{
@@ -728,17 +769,17 @@ def test_get_configurations_env_vars_parentbased_trace_id_ratio_with_out_of_boun
728769
{
729770
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS: "flask,requests,fastapi,azure_sdk",
730771
OTEL_TRACES_SAMPLER: PARENT_BASED_TRACE_ID_RATIO_SAMPLER,
731-
OTEL_TRACES_SAMPLER_ARG: "0.45",
772+
OTEL_TRACES_SAMPLER_ARG: "non-numeric-value",
732773
},
733774
clear=True,
734775
)
735776
@patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_DEFAULT_RESOURCE)
736-
def test_get_configurations_env_vars_parentbased_trace_id_ratio(self, resource_create_mock):
777+
def test_get_configurations_env_vars_parentbased_trace_id_ratio_non_numeric_value(self, resource_create_mock):
737778
configurations = _get_configurations()
738779

739780
self.assertTrue("connection_string" not in configurations)
740781
self.assertEqual(configurations["resource"].attributes, TEST_DEFAULT_RESOURCE.attributes)
741-
self.assertEqual(configurations["sampling_arg"], 0.45)
782+
self.assertEqual(configurations["sampling_arg"], 1.0)
742783
self.assertEqual(configurations["sampler_type"], "parentbased_trace_id_ratio")
743784

744785
@patch.dict(
@@ -772,3 +813,46 @@ def test_get_configurations_env_vars_no_sampling_env_set(self, resource_create_m
772813
self.assertTrue("connection_string" not in configurations)
773814
self.assertEqual(configurations["resource"].attributes, TEST_DEFAULT_RESOURCE.attributes)
774815
self.assertEqual(configurations["sampling_ratio"], 1.0)
816+
817+
818+
# Tests for the _get_sampler_from_name function
819+
def test_get_sampler_from_name_always_on_off(self):
820+
self.assertIs(_get_sampler_from_name(ALWAYS_ON_SAMPLER, None), ALWAYS_ON)
821+
self.assertIs(_get_sampler_from_name(ALWAYS_OFF_SAMPLER, None), ALWAYS_OFF)
822+
823+
def test_get_sampler_from_name_trace_id_ratio(self):
824+
sampler = _get_sampler_from_name(TRACE_ID_RATIO_SAMPLER, "0.3")
825+
self.assertIsInstance(sampler, TraceIdRatioBased)
826+
self.assertEqual(sampler._rate, 0.3)
827+
828+
def test_get_sampler_from_name_trace_id_ratio_defaults_to_one(self):
829+
sampler = _get_sampler_from_name(TRACE_ID_RATIO_SAMPLER, None)
830+
self.assertIsInstance(sampler, TraceIdRatioBased)
831+
self.assertEqual(sampler._rate, 1.0)
832+
833+
def test_get_sampler_from_name_parent_based_fixed(self):
834+
sampler_on = _get_sampler_from_name(PARENT_BASED_ALWAYS_ON_SAMPLER, None)
835+
sampler_off = _get_sampler_from_name(PARENT_BASED_ALWAYS_OFF_SAMPLER, None)
836+
837+
self.assertIsInstance(sampler_on, ParentBased)
838+
self.assertIs(sampler_on._root, ALWAYS_ON)
839+
840+
self.assertIsInstance(sampler_off, ParentBased)
841+
self.assertIs(sampler_off._root, ALWAYS_OFF)
842+
843+
def test_get_sampler_from_name_parent_based_trace_id_ratio(self):
844+
sampler = _get_sampler_from_name(PARENT_BASED_TRACE_ID_RATIO_SAMPLER, "0.25")
845+
self.assertIsInstance(sampler, ParentBased)
846+
self.assertIsInstance(sampler._root, TraceIdRatioBased)
847+
self.assertEqual(sampler._root._rate, 0.25)
848+
849+
def test_get_sampler_from_name_parent_based_trace_id_ratio_defaults(self):
850+
sampler = _get_sampler_from_name(PARENT_BASED_TRACE_ID_RATIO_SAMPLER, None)
851+
self.assertIsInstance(sampler._root, TraceIdRatioBased)
852+
self.assertEqual(sampler._root._rate, 1.0)
853+
854+
def test_get_sampler_from_name_invalid_type_defaults_parentbased_always_on(self):
855+
sampler = _get_sampler_from_name("not-a-sampler", None)
856+
self.assertIsInstance(sampler, ParentBased)
857+
self.assertIs(sampler._root, ALWAYS_ON)
858+

0 commit comments

Comments
 (0)