Skip to content

Commit

Permalink
Merge pull request #496 from solarwinds/NH-100646-update-key-name-gen…
Browse files Browse the repository at this point in the history
…eral

NH-100646 Internal service_key updates more general
  • Loading branch information
tammy-baylis-swi authored Jan 29, 2025
2 parents cd6293a + 4488a82 commit 2e02c7d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
22 changes: 14 additions & 8 deletions solarwinds_apm/apm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,11 @@ def __init__(
self.agent_enabled,
otel_resource,
)
if not self.is_lambda:
self.__config["service_key"] = self._update_service_key_name(
self.agent_enabled,
self.__config["service_key"],
self.service_name,
)
self.__config["service_key"] = self._update_service_key_name(
self.agent_enabled,
self.__config["service_key"],
self.service_name,
)

# Update and apply logging settings to Python logger
self.update_log_settings()
Expand Down Expand Up @@ -516,8 +515,15 @@ def _update_service_key_name(
) -> str:
"""Update service key with service name"""
if agent_enabled and service_key and service_name:
# Only update if service_name and service_key exist and non-empty.
# When agent_enabled, assume service_key is formatted correctly.
# Only update if service_name and service_key exist and non-empty,
# and service_key in correct format.
key_parts = service_key.split(":")
if len(key_parts) < 2:
logger.debug(
"Service key is not in the correct format to update its own service name. Skipping."
)
return service_key

return ":".join([service_key.split(":")[0], service_name])

# Else no need to update service_key when not reporting
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/test_apm_config/test_apm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,43 @@ def test__update_service_key_name_agent_enabled_and_service_name_ok(self):
)
assert result == "valid_key_with:bar-service"

def test__update_service_key_name_agent_enabled_and_service_name_ok_but_service_key_missing(self):
test_config = apm_config.SolarWindsApmConfig()
result = test_config._update_service_key_name(
True,
None,
"bar-service"
)
assert result is None

def test__update_service_key_name_agent_enabled_and_service_name_ok_but_service_key_empty(self):
test_config = apm_config.SolarWindsApmConfig()
result = test_config._update_service_key_name(
True,
"",
"bar-service"
)
assert result == ""

def test__update_service_key_name_agent_enabled_and_service_name_ok_but_service_key_no_delimiter(self):
test_config = apm_config.SolarWindsApmConfig()
result = test_config._update_service_key_name(
True,
"weird-key-no-delimiter",
"bar-service"
)
assert result == "weird-key-no-delimiter"

def test__update_service_key_name_agent_enabled_and_service_name_ok_service_key_multiple_delimiter(self):
test_config = apm_config.SolarWindsApmConfig()
result = test_config._update_service_key_name(
True,
"weird-key:with:2-delimiters",
"bar-service"
)
# Updates everything after first delim
assert result == "weird-key:bar-service"

def test_update_log_settings(self, mocker):
mock_log_filepath = mocker.patch(
"solarwinds_apm.apm_config.SolarWindsApmConfig.update_log_filepath"
Expand Down

0 comments on commit 2e02c7d

Please sign in to comment.