@@ -325,10 +325,134 @@ def test_warns_if_logging_level_does_not_match_our_map(self, get_logger_mock):
325325 remote_config = opamp_pb2 .AgentRemoteConfig (config = config , config_hash = b"1234" )
326326 message = opamp_pb2 .ServerToAgent (remote_config = remote_config )
327327
328- with self .assertLogs (config_logger , logging .WARNING ) :
328+ with self .assertLogs (config_logger , logging .ERROR ) as cm :
329329 opamp_handler (agent , client , message )
330+ self .assertEqual (cm .output , ["ERROR:elasticotel.distro.config:Logging level not handled: unexpected" ])
330331
331332 client ._build_remote_config_status_response_message .assert_called_once_with (
332333 client ._update_remote_config_status ()
333334 )
334335 agent .send .assert_called_once_with (payload = mock .ANY )
336+
337+ @mock .patch ("opentelemetry.trace.get_tracer_provider" )
338+ def test_sets_matching_sampling_rate (self , get_tracer_provider_mock ):
339+ sampler = sampling .ParentBasedTraceIdRatio (rate = 1.0 )
340+ get_tracer_provider_mock .return_value .sampler = sampler
341+ agent = mock .Mock ()
342+ client = mock .Mock ()
343+ config = opamp_pb2 .AgentConfigMap ()
344+ config .config_map ["elastic" ].body = json .dumps ({"sampling_rate" : "0.5" }).encode ()
345+ config .config_map ["elastic" ].content_type = "application/json"
346+ remote_config = opamp_pb2 .AgentRemoteConfig (config = config , config_hash = b"1234" )
347+ message = opamp_pb2 .ServerToAgent (remote_config = remote_config )
348+ opamp_handler (agent , client , message )
349+
350+ self .assertEqual (sampler ._root .rate , 0.5 )
351+
352+ client ._update_remote_config_status .assert_called_once_with (
353+ remote_config_hash = b"1234" , status = opamp_pb2 .RemoteConfigStatuses_APPLIED , error_message = ""
354+ )
355+ client ._build_remote_config_status_response_message .assert_called_once_with (
356+ client ._update_remote_config_status ()
357+ )
358+ agent .send .assert_called_once_with (payload = mock .ANY )
359+
360+ @mock .patch ("opentelemetry.trace.get_tracer_provider" )
361+ def test_sets_sampling_rate_to_default_info_without_sampling_rate_entry_in_config (self , get_tracer_provider_mock ):
362+ sampler = sampling .ParentBasedTraceIdRatio (rate = 1.0 )
363+ get_tracer_provider_mock .return_value .sampler = sampler
364+ agent = mock .Mock ()
365+ client = mock .Mock ()
366+ config = opamp_pb2 .AgentConfigMap ()
367+ config .config_map ["elastic" ].body = json .dumps ({}).encode ()
368+ config .config_map ["elastic" ].content_type = "application/json"
369+ remote_config = opamp_pb2 .AgentRemoteConfig (config = config , config_hash = b"1234" )
370+ message = opamp_pb2 .ServerToAgent (remote_config = remote_config )
371+ opamp_handler (agent , client , message )
372+
373+ self .assertEqual (sampler ._root .rate , 1.0 )
374+
375+ client ._update_remote_config_status .assert_called_once_with (
376+ remote_config_hash = b"1234" , status = opamp_pb2 .RemoteConfigStatuses_APPLIED , error_message = ""
377+ )
378+ client ._build_remote_config_status_response_message .assert_called_once_with (
379+ client ._update_remote_config_status ()
380+ )
381+ agent .send .assert_called_once_with (payload = mock .ANY )
382+
383+ @mock .patch ("opentelemetry.trace.get_tracer_provider" )
384+ def test_warns_if_sampling_rate_value_is_invalid (self , get_tracer_provider_mock ):
385+ sampler = sampling .ParentBasedTraceIdRatio (rate = 1.0 )
386+ get_tracer_provider_mock .return_value .sampler = sampler
387+ agent = mock .Mock ()
388+ client = mock .Mock ()
389+ config = opamp_pb2 .AgentConfigMap ()
390+ config .config_map ["elastic" ].body = json .dumps ({"sampling_rate" : "unexpected" }).encode ()
391+ config .config_map ["elastic" ].content_type = "application/json"
392+ remote_config = opamp_pb2 .AgentRemoteConfig (config = config , config_hash = b"1234" )
393+ message = opamp_pb2 .ServerToAgent (remote_config = remote_config )
394+
395+ with self .assertLogs (config_logger , logging .ERROR ) as cm :
396+ opamp_handler (agent , client , message )
397+ self .assertEqual (
398+ cm .output , ["ERROR:elasticotel.distro.config:Invalid `sampling_rate` from config `unexpected`" ]
399+ )
400+
401+ client ._update_remote_config_status .assert_called_once_with (
402+ remote_config_hash = b"1234" ,
403+ status = opamp_pb2 .RemoteConfigStatuses_FAILED ,
404+ error_message = "Invalid sampling_rate unexpected" ,
405+ )
406+ client ._build_remote_config_status_response_message .assert_called_once_with (
407+ client ._update_remote_config_status ()
408+ )
409+ agent .send .assert_called_once_with (payload = mock .ANY )
410+
411+ @mock .patch ("opentelemetry.trace.get_tracer_provider" )
412+ def test_warns_if_sampler_is_not_what_we_expect (self , get_tracer_provider_mock ):
413+ get_tracer_provider_mock .return_value .sampler = 5
414+ agent = mock .Mock ()
415+ client = mock .Mock ()
416+ config = opamp_pb2 .AgentConfigMap ()
417+ config .config_map ["elastic" ].body = json .dumps ({"sampling_rate" : "1.0" }).encode ()
418+ config .config_map ["elastic" ].content_type = "application/json"
419+ remote_config = opamp_pb2 .AgentRemoteConfig (config = config , config_hash = b"1234" )
420+ message = opamp_pb2 .ServerToAgent (remote_config = remote_config )
421+
422+ with self .assertLogs (config_logger , logging .WARNING ) as cm :
423+ opamp_handler (agent , client , message )
424+ self .assertEqual (
425+ cm .output ,
426+ ["WARNING:elasticotel.distro.config:Sampler <class 'int'> is not supported, not applying sampling_rate." ],
427+ )
428+
429+ client ._update_remote_config_status .assert_called_once_with (
430+ remote_config_hash = b"1234" , status = opamp_pb2 .RemoteConfigStatuses_APPLIED , error_message = ""
431+ )
432+ client ._build_remote_config_status_response_message .assert_called_once_with (
433+ client ._update_remote_config_status ()
434+ )
435+ agent .send .assert_called_once_with (payload = mock .ANY )
436+
437+ @mock .patch ("opentelemetry.trace.get_tracer_provider" )
438+ def test_ignores_tracer_provider_without_a_sampler (self , get_tracer_provider_mock ):
439+ get_tracer_provider_mock .return_value .sampler = None
440+ agent = mock .Mock ()
441+ client = mock .Mock ()
442+ config = opamp_pb2 .AgentConfigMap ()
443+ config .config_map ["elastic" ].body = json .dumps ({"sampling_rate" : "1.0" }).encode ()
444+ config .config_map ["elastic" ].content_type = "application/json"
445+ remote_config = opamp_pb2 .AgentRemoteConfig (config = config , config_hash = b"1234" )
446+ message = opamp_pb2 .ServerToAgent (remote_config = remote_config )
447+
448+ with self .assertLogs (config_logger , logging .DEBUG ) as cm :
449+ opamp_handler (agent , client , message )
450+ self .assertIn ("DEBUG:elasticotel.distro.config:Cannot get sampler from tracer provider." , cm .output )
451+
452+ client ._update_remote_config_status .assert_called_once_with (
453+ remote_config_hash = b"1234" , status = opamp_pb2 .RemoteConfigStatuses_APPLIED , error_message = ""
454+ )
455+ client ._build_remote_config_status_response_message .assert_called_once_with (
456+ client ._update_remote_config_status ()
457+ )
458+ agent .send .assert_called_once_with (payload = mock .ANY )
0 commit comments