Skip to content

Commit b5e7939

Browse files
committed
Guard against unexpected tracerprovider
Spotted by macroscope
1 parent adefa52 commit b5e7939

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

src/elasticotel/distro/config.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ def _rules_from_deactivate_instrumentations(csv: str) -> _TracerConfiguratorRule
212212

213213

214214
def _handle_deactivate_instrumentations(remote_config) -> ConfigUpdate:
215+
tracer_provider = trace.get_tracer_provider()
216+
set_tracer_configurator = getattr(tracer_provider, "_set_tracer_configurator", None)
217+
if set_tracer_configurator is None:
218+
logger.debug("Cannot get _set_tracer_configurator from tracer provider.")
219+
return ConfigUpdate()
220+
215221
config_deactivate_instrumentations = remote_config.get(DEACTIVATE_INSTRUMENTATIONS_CONFIG_KEY, "")
216222

217223
rules = _rules_from_deactivate_instrumentations(config_deactivate_instrumentations)
@@ -222,12 +228,9 @@ def _handle_deactivate_instrumentations(remote_config) -> ConfigUpdate:
222228
return ConfigUpdate()
223229
# when rules are updated we need to clear the cache of the tracer_configurator function
224230
tracer_configurator._updatable_tracer_configurator.cache_clear()
225-
tracer_provider = trace.get_tracer_provider()
226-
tracer_provider._set_tracer_configurator( # type: ignore [reportAttributeAccessIssue]
227-
tracer_configurator=tracer_configurator._updatable_tracer_configurator
228-
)
229231

230-
logger.debug('Updated deactivate instrumentations to "%s"', config_deactivate_instrumentations)
232+
set_tracer_configurator(tracer_configurator=tracer_configurator._updatable_tracer_configurator)
233+
logger.debug('Updated deactivate instrumentations to "%s".', config_deactivate_instrumentations)
231234
_config = _get_config()
232235
if _config:
233236
_config.deactivate_instrumentations.update(value=config_deactivate_instrumentations)

tests/distro/test_distro.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,38 @@ def test_sets_deactivate_instrumentations_with_same_rules(
931931
self.assertEqual(len(rule_based_tracer_configurator.rules), 1)
932932
_updatable_tracer_configurator.cache_clear()
933933

934+
@mock.patch("elasticotel.distro.config._get_config")
935+
@mock.patch("elasticotel.sdk.trace.tracer_configurator._get_tracer_configurator")
936+
@mock.patch("opentelemetry.trace.get_tracer_provider")
937+
def test_sets_deactivate_instrumentations_handles_unexpected_tracer_provider(
938+
self, get_tracer_provider_mock, get_tracer_configurator_mock, get_config_mock
939+
):
940+
config = Config()
941+
get_config_mock.return_value = config
942+
get_tracer_provider_mock.return_value = None
943+
944+
agent = mock.Mock()
945+
client = mock.Mock()
946+
config = opamp_pb2.AgentConfigMap()
947+
config.config_map["elastic"].body = json.dumps({"deactivate_instrumentations": ""}).encode()
948+
config.config_map["elastic"].content_type = "application/json"
949+
remote_config = opamp_pb2.AgentRemoteConfig(config=config, config_hash=b"1234")
950+
message = opamp_pb2.ServerToAgent(remote_config=remote_config)
951+
opamp_handler(agent, client, message)
952+
953+
client._update_remote_config_status.assert_called_once_with(
954+
remote_config_hash=b"1234", status=opamp_pb2.RemoteConfigStatuses_APPLIED, error_message=""
955+
)
956+
client._update_effective_config.assert_called_once_with(
957+
{"elastic": {"logging_level": "warn", "sampling_rate": "1.0", "deactivate_instrumentations": ""}}
958+
)
959+
client._build_remote_config_status_response_message.assert_called_once_with(
960+
client._update_remote_config_status()
961+
)
962+
agent.send.assert_called_once_with(payload=mock.ANY)
963+
client._build_full_state_message.assert_not_called()
964+
get_tracer_configurator_mock.assert_not_called()
965+
934966
@mock.patch("elasticotel.distro.config._get_config")
935967
@mock.patch("opentelemetry.trace.get_tracer_provider")
936968
def test_calls_build_full_state_message_when_report_full_state_flag_is_set(

0 commit comments

Comments
 (0)