diff --git a/solarwinds_apm/apm_config.py b/solarwinds_apm/apm_config.py index ab04481ea..a0b893772 100644 --- a/solarwinds_apm/apm_config.py +++ b/solarwinds_apm/apm_config.py @@ -91,17 +91,19 @@ class SolarWindsApmConfig: def __init__( self, - otel_resource: Resource = Resource.create(), + otel_resource: Resource | None = None, **kwargs: int, ) -> None: """Initialize SolarWinds APM configuration. Parameters: - otel_resource (Resource): OpenTelemetry resource with detector attributes. - In normal usage, passed from Configurator after detector resource created. - Defaults to Resource.create() for backward compatibility. + otel_resource (optional): OpenTelemetry resource with detector attributes. + In normal distro usage, passed from Configurator after resource detectors created. **kwargs (int): Additional configuration keyword arguments. """ + if otel_resource is None: + otel_resource = Resource.create() + self.__config = {} # Update the config with default values self.__config = { diff --git a/tests/unit/test_apm_config/test_apm_config.py b/tests/unit/test_apm_config/test_apm_config.py index 22a5090c5..2371bd2a5 100644 --- a/tests/unit/test_apm_config/test_apm_config.py +++ b/tests/unit/test_apm_config/test_apm_config.py @@ -157,7 +157,7 @@ def test__init_valid_service_key_format_otel_service_name( "SW_APM_SERVICE_KEY": "service_key_with:sw_service_name", "OTEL_SERVICE_NAME": "from_otel_env" }) - # Otel picks up os mock if Resource.create here (same as default arg) + # Explicitly pass Resource.create() to pick up env vars set above test_config = apm_config.SolarWindsApmConfig(Resource.create()) assert test_config.agent_enabled assert test_config.service_name == "from_otel_env" @@ -504,7 +504,11 @@ def test__update_service_key_name_agent_enabled_and_service_name_ok_service_key_ assert result == "weird-key:bar-service" def test__validate_log_filepath_none(self, mocker): - mock_exists = mocker.patch("solarwinds_apm.apm_config.os.path.exists") + mocker.patch( + "solarwinds_apm.apm_config.Resource.create", + return_value=Resource.get_empty() + ) + mock_exists = mocker.patch("solarwinds_apm.apm_config.os.path.exists", return_value=False) mock_makedirs = mocker.patch("solarwinds_apm.apm_config.os.makedirs") test_config = apm_config.SolarWindsApmConfig() @@ -515,7 +519,11 @@ def test__validate_log_filepath_none(self, mocker): assert test_config.get("log_filepath") == "" def test__validate_log_filepath_no_parent_path(self, mocker): - mock_exists = mocker.patch("solarwinds_apm.apm_config.os.path.exists") + mocker.patch( + "solarwinds_apm.apm_config.Resource.create", + return_value=Resource.get_empty() + ) + mock_exists = mocker.patch("solarwinds_apm.apm_config.os.path.exists", return_value=False) mock_makedirs = mocker.patch("solarwinds_apm.apm_config.os.makedirs") test_config = apm_config.SolarWindsApmConfig() @@ -526,6 +534,10 @@ def test__validate_log_filepath_no_parent_path(self, mocker): assert test_config.get("log_filepath") == "foo" def test__validate_log_filepath_path_exists(self, mocker): + mocker.patch( + "solarwinds_apm.apm_config.Resource.create", + return_value=Resource.get_empty() + ) mock_exists = mocker.patch("solarwinds_apm.apm_config.os.path.exists", return_value=True) mock_makedirs = mocker.patch("solarwinds_apm.apm_config.os.makedirs") @@ -537,6 +549,10 @@ def test__validate_log_filepath_path_exists(self, mocker): assert test_config.get("log_filepath") == "/path/to/foo" def test__validate_log_filepath_create_path(self, mocker): + mocker.patch( + "solarwinds_apm.apm_config.Resource.create", + return_value=Resource.get_empty() + ) mock_exists = mocker.patch("solarwinds_apm.apm_config.os.path.exists", return_value=False) mock_makedirs = mocker.patch("solarwinds_apm.apm_config.os.makedirs") @@ -548,6 +564,10 @@ def test__validate_log_filepath_create_path(self, mocker): assert test_config.get("log_filepath") == "/path/to/foo" def test__validate_log_filepath_cannot_create_reset_settings(self, mocker): + mocker.patch( + "solarwinds_apm.apm_config.Resource.create", + return_value=Resource.get_empty() + ) mock_exists = mocker.patch("solarwinds_apm.apm_config.os.path.exists", return_value=False) mock_makedirs = mocker.patch( "solarwinds_apm.apm_config.os.makedirs",