2222from elasticotel .distro import ElasticOpenTelemetryConfigurator , ElasticOpenTelemetryDistro , logger as distro_logger
2323from elasticotel .distro .config import opamp_handler , logger as config_logger , Config
2424from elasticotel .distro .environment_variables import ELASTIC_OTEL_OPAMP_ENDPOINT , ELASTIC_OTEL_SYSTEM_METRICS_ENABLED
25+ from elasticotel .sdk .sampler import DefaultSampler
2526from opentelemetry .environment_variables import (
2627 OTEL_LOGS_EXPORTER ,
2728 OTEL_METRICS_EXPORTER ,
3536 OTEL_TRACES_SAMPLER ,
3637 OTEL_TRACES_SAMPLER_ARG ,
3738)
39+ from opentelemetry import trace
3840from opentelemetry .sdk .trace import sampling
3941from opentelemetry ._opamp .proto import opamp_pb2 as opamp_pb2
42+ from opentelemetry .util ._once import Once
4043
4144
4245class TestDistribution (TestCase ):
46+ def setUp (self ):
47+ # Hackily reset global trace provider to allow tests to initialize it.
48+ trace ._TRACER_PROVIDER = None
49+ trace ._TRACER_PROVIDER_SET_ONCE = Once ()
50+
4351 @mock .patch .dict ("os.environ" , {}, clear = True )
4452 def test_default_configuration (self ):
4553 distro = ElasticOpenTelemetryDistro ()
@@ -54,21 +62,39 @@ def test_default_configuration(self):
5462 )
5563 self .assertEqual ("always_off" , os .environ .get (OTEL_METRICS_EXEMPLAR_FILTER ))
5664 self .assertEqual ("DELTA" , os .environ .get (OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE ))
57- self .assertEqual ("parentbased_traceidratio " , os .environ .get (OTEL_TRACES_SAMPLER ))
65+ self .assertEqual ("experimental_composite_parentbased_traceidratio " , os .environ .get (OTEL_TRACES_SAMPLER ))
5866 self .assertEqual ("1.0" , os .environ .get (OTEL_TRACES_SAMPLER_ARG ))
5967
6068 @mock .patch .dict ("os.environ" , {}, clear = True )
6169 def test_sampler_configuration (self ):
62- distro = ElasticOpenTelemetryDistro ()
63- distro ._configure ()
64- parent_sampler = sampling ._get_from_env_or_default ()
65-
66- assert isinstance (parent_sampler , sampling .ParentBasedTraceIdRatio )
70+ ElasticOpenTelemetryDistro ()._configure ()
71+ ElasticOpenTelemetryConfigurator ()._configure ()
72+ sampler = getattr (trace .get_tracer_provider (), "sampler" , None )
73+ self .assertTrue (isinstance (sampler , DefaultSampler ))
74+ self .assertIn (
75+ "ComposableParentThreshold{root=ComposableTraceIDRatioBased{threshold=0, ratio=1.0}}" ,
76+ sampler .get_description (),
77+ )
6778
68- sampler = parent_sampler ._root
79+ @mock .patch .dict ("os.environ" , {}, clear = True )
80+ def test_sampler_configuration_sampler_arg (self ):
81+ os .environ [OTEL_TRACES_SAMPLER_ARG ] = "0.0"
82+ ElasticOpenTelemetryDistro ()._configure ()
83+ ElasticOpenTelemetryConfigurator ()._configure ()
84+ sampler = getattr (trace .get_tracer_provider (), "sampler" , None )
85+ self .assertTrue (isinstance (sampler , DefaultSampler ))
86+ self .assertIn (
87+ "ComposableParentThreshold{root=ComposableTraceIDRatioBased{threshold=max, ratio=0.0}}" ,
88+ sampler .get_description (),
89+ )
6990
70- assert isinstance (sampler , sampling .TraceIdRatioBased )
71- assert sampler .rate == 1.0
91+ @mock .patch .dict ("os.environ" , {}, clear = True )
92+ def test_sampler_configuration_user_configured (self ):
93+ os .environ [OTEL_TRACES_SAMPLER ] = "always_on"
94+ ElasticOpenTelemetryDistro ()._configure ()
95+ ElasticOpenTelemetryConfigurator ()._configure ()
96+ sampler = getattr (trace .get_tracer_provider (), "sampler" , None )
97+ self .assertTrue (isinstance (sampler , sampling ._AlwaysOn ))
7298
7399 @mock .patch .dict ("os.environ" , {}, clear = True )
74100 def test_load_instrumentor_call_with_default_kwargs_for_SystemMetricsInstrumentor (self ):
@@ -517,7 +543,7 @@ def test_warns_if_logging_level_does_not_match_our_map(self, get_logger_mock, ge
517543 @mock .patch ("opentelemetry.trace.get_tracer_provider" )
518544 def test_sets_matching_sampling_rate (self , get_tracer_provider_mock , get_config_mock ):
519545 get_config_mock .return_value = Config ()
520- sampler = sampling . ParentBasedTraceIdRatio ( rate = 1.0 )
546+ sampler = DefaultSampler ( 1.0 )
521547 get_tracer_provider_mock .return_value .sampler = sampler
522548 agent = mock .Mock ()
523549 client = mock .Mock ()
@@ -528,7 +554,10 @@ def test_sets_matching_sampling_rate(self, get_tracer_provider_mock, get_config_
528554 message = opamp_pb2 .ServerToAgent (remote_config = remote_config )
529555 opamp_handler (agent , client , message )
530556
531- self .assertEqual (sampler ._root .rate , 0.5 )
557+ self .assertIn (
558+ "ComposableParentThreshold{root=ComposableTraceIDRatioBased{threshold=8, ratio=0.5}}" ,
559+ sampler .get_description (),
560+ )
532561
533562 client ._update_remote_config_status .assert_called_once_with (
534563 remote_config_hash = b"1234" , status = opamp_pb2 .RemoteConfigStatuses_APPLIED , error_message = ""
0 commit comments