Skip to content

Commit

Permalink
New sampler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydvoss committed Oct 11, 2022
1 parent eb6a4fe commit 5748cb9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.13.0...HEAD)

- Enabled custom samplers via entry points
([#XXX](https://github.com/open-telemetry/opentelemetry-python/pull/XXX))
([#2972](https://github.com/open-telemetry/opentelemetry-python/pull/2972))
- Update explicit histogram bucket boundaries
([#2947](https://github.com/open-telemetry/opentelemetry-python/pull/2947))

Expand Down
8 changes: 5 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class CustomSampler(TraceIdRatioBased):
Sampling probability can be set with ``OTEL_TRACES_SAMPLER_ARG`` if the sampler is a ``TraceIdRatioBased`` Sampler, such as ``traceidratio`` and ``parentbased_traceidratio``. When not provided rate will be set to 1.0 (maximum rate possible).
Prev example but with environment vairables. Please make sure to set the env ``OTEL_TRACES_SAMPLER=traceidratio`` and ``OTEL_TRACES_SAMPLER_ARG=0.001``.
Prev example but with environment variables. Please make sure to set the env ``OTEL_TRACES_SAMPLER=traceidratio`` and ``OTEL_TRACES_SAMPLER_ARG=0.001``.
.. code:: python
Expand Down Expand Up @@ -404,15 +404,17 @@ def _get_from_env_or_default() -> Sampler:

if trace_sampler_name in _KNOWN_INITIALIZED_SAMPLERS:
return _KNOWN_INITIALIZED_SAMPLERS[trace_sampler_name]

trace_sampler_impl = None
if trace_sampler_name in _KNOWN_SAMPLER_CLASSES:
trace_sampler_impl = _KNOWN_SAMPLER_CLASSES[trace_sampler_name]
else:
try:
trace_sampler_impl = _import_sampler(trace_sampler_name)
except RuntimeError as err:
_logger.warning("Couldn't recognize sampler %s: %s", trace_sampler_name, err)
_logger.warning(
"Unable to recognize sampler %s: %s", trace_sampler_name, err
)
return _KNOWN_INITIALIZED_SAMPLERS["parentbased_always_on"]

if issubclass(trace_sampler_impl, TraceIdRatioBased):
Expand Down
38 changes: 37 additions & 1 deletion opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ def test_tracer_provider_accepts_concurrent_multi_span_processor(self):
)


class CustomSampler(sampling.Sampler):
def __init__():
super()


class CustomRatioSampler(sampling.TraceIdRatioBased):
def __init__(ratio):
super(ratio)


class TestTracerSampling(unittest.TestCase):
def tearDown(self):
reload(trace)
Expand Down Expand Up @@ -219,7 +229,9 @@ def test_ratio_sampler_with_env(self):
self.assertIsInstance(tracer_provider.sampler, sampling.ParentBased)
self.assertEqual(tracer_provider.sampler._root.rate, 0.25)

@mock.patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"})
@mock.patch.dict(
"os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"}
)
def test_sampler_with_env_non_existent_entry_point(self):
# pylint: disable=protected-access
reload(trace)
Expand All @@ -228,6 +240,30 @@ def test_sampler_with_env_non_existent_entry_point(self):
# pylint: disable=protected-access
self.assertEqual(tracer_provider.sampler._root, sampling.ALWAYS_ON)

@mock.patch("opentelemetry.sdk.util._import_config_components")
@mock.patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "custom_sampler"})
def test_custom_sampler_with_env(self, mock_import_config_components):
# pylint: disable=protected-access
reload(trace)
mock_import_config_components.return_value = [("custom_sampler", CustomSampler)]
tracer_provider = trace.TracerProvider()
self.assertIsInstance(tracer_provider.sampler, CustomSampler)

@mock.patch("opentelemetry.sdk.util._import_config_components")
@mock.patch.dict(
"os.environ",
{
OTEL_TRACES_SAMPLER: "custom_ratio_sampler",
OTEL_TRACES_SAMPLER_ARG: "0.5",
},
)
def test_custom_ratio_sampler_with_env(self, mock_import_config_components):
# pylint: disable=protected-access
reload(trace)
mock_import_config_components.return_value = [("custom_ratio_sampler", CustomRatioSampler)]
tracer_provider = trace.TracerProvider()
self.assertIsInstance(tracer_provider.sampler, CustomRatioSampler)
self.assertEqual(tracer_provider.sampler._root.rate, 0.5)

class TestSpanCreation(unittest.TestCase):
def test_start_span_invalid_spancontext(self):
Expand Down

0 comments on commit 5748cb9

Please sign in to comment.