Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NH-68421 Adjust service name, key usage when lambda #309

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions solarwinds_apm/apm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ def __init__(
self.agent_enabled,
otel_resource,
)
self.__config["service_key"] = self._update_service_key_name(
self.agent_enabled,
self.__config["service_key"],
self.service_name,
)
if not self.is_lambda:
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 @@ -415,7 +416,38 @@ def _calculate_agent_enabled(self) -> bool:
logger.debug("agent_enabled: %s", agent_enabled)
return agent_enabled

def _calculate_service_name(
def _calculate_service_name_lambda(
self,
otel_resource: "Resource",
) -> str:
"""Calculate `service.name` by priority system (decreasing):
1. OTEL_SERVICE_NAME
2. AWS_LAMBDA_FUNCTION_NAME

Note: 1 is always set by the current otel-instrument lambda exec wrapper
if used. The wrapper also sets service_name as the function_name, if
former is not provided.

If otel-instrument did not do the above, the passed in OTel Resource
likely has a `service.name` already calculated by merging OTEL_SERVICE_NAME
/ OTEL_RESOURCE_ATTRIBUTES with defaults.

We assume 2 is not none/empty because is_lambda check by caller should
be True.
"""
otel_service_name = otel_resource.attributes.get("service.name", None)

if not otel_service_name:
return self.lambda_function_name

if otel_service_name == "unknown_service":
# OTEL_SERVICE_NAME was not set
# and otel-instrument did not wrap instrumentation
return self.lambda_function_name

return otel_service_name

def _calculate_service_name_apm_proto(
self,
agent_enabled: bool,
otel_resource: "Resource",
Expand Down Expand Up @@ -455,6 +487,22 @@ def _calculate_service_name(
service_name = otel_service_name
return service_name

def _calculate_service_name(
self,
agent_enabled: bool,
otel_resource: "Resource",
) -> str:
"""Calculate service_name"""
if self.is_lambda:
return self._calculate_service_name_lambda(
otel_resource,
)

return self._calculate_service_name_apm_proto(
agent_enabled,
otel_resource,
)

def _update_service_key_name(
self,
agent_enabled: bool,
Expand Down
2 changes: 1 addition & 1 deletion solarwinds_apm/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.2.0"
__version__ = "1.2.1.1"
51 changes: 0 additions & 51 deletions tests/unit/test_apm_config/test_apm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,57 +679,6 @@ def test_set_config_value_default_log_type(
if old_log_type:
os.environ["SW_APM_LOG_TYPE"] = old_log_type

def test__calculate_service_name_agent_disabled(self):
test_config = apm_config.SolarWindsApmConfig()
result = test_config._calculate_service_name(
False,
{}
)
assert result == ""

def test__calculate_service_name_no_otel_service_name(
self,
mocker,
):
mocker.patch.dict(os.environ, {
"SW_APM_SERVICE_KEY": "service_key_with:sw_service_name",
})
test_config = apm_config.SolarWindsApmConfig()
result = test_config._calculate_service_name(
True,
Resource.create({"service.name": None})
)
assert result == "sw_service_name"

def test__calculate_service_name_default_unknown_otel_service_name(
self,
mocker,
):
mocker.patch.dict(os.environ, {
"SW_APM_SERVICE_KEY": "service_key_with:sw_service_name",
})
test_config = apm_config.SolarWindsApmConfig()
result = test_config._calculate_service_name(
True,
# default is unknown_service
Resource.create()
)
assert result == "sw_service_name"

def test__calculate_service_name_use_otel_service_name(
self,
mocker,
):
mocker.patch.dict(os.environ, {
"SW_APM_SERVICE_KEY": "service_key_with:sw_service_name",
})
test_config = apm_config.SolarWindsApmConfig()
result = test_config._calculate_service_name(
True,
Resource.create({"service.name": "foobar"})
)
assert result == "foobar"

def test__update_service_key_name_not_agent_enabled(self):
test_config = apm_config.SolarWindsApmConfig()
result = test_config._update_service_key_name(
Expand Down
166 changes: 166 additions & 0 deletions tests/unit/test_apm_config/test_apm_config_service_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# © 2024 SolarWinds Worldwide, LLC. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

import os

from opentelemetry.sdk.resources import Resource

from solarwinds_apm import apm_config


class TestSolarWindsApmConfigServiceName:
def test__calculate_service_name_is_lambda(self, mocker):
mock_calc_proto = mocker.patch(
"solarwinds_apm.apm_config.SolarWindsApmConfig._calculate_service_name_apm_proto"
)
mock_calc_lambda = mocker.patch(
"solarwinds_apm.apm_config.SolarWindsApmConfig._calculate_service_name_lambda"
)
mocker.patch(
"solarwinds_apm.apm_config.SolarWindsApmConfig.calculate_is_lambda",
return_value=True,
)
test_config = apm_config.SolarWindsApmConfig("foo")
test_config._calculate_service_name(
True,
{},
)
mock_calc_proto.assert_not_called()
# called twice because of init, and we call again
mock_calc_lambda.assert_has_calls(
[
mocker.call("foo"),
mocker.call({}),
]
)

def test__calculate_service_name_not_is_lambda(self, mocker):
mock_calc_proto = mocker.patch(
"solarwinds_apm.apm_config.SolarWindsApmConfig._calculate_service_name_apm_proto"
)
mock_calc_lambda = mocker.patch(
"solarwinds_apm.apm_config.SolarWindsApmConfig._calculate_service_name_lambda"
)
mocker.patch(
"solarwinds_apm.apm_config.SolarWindsApmConfig.calculate_is_lambda",
return_value=False,
)
test_config = apm_config.SolarWindsApmConfig("foo")
test_config._calculate_service_name(
True,
{},
)
# called twice because of init, and we call again
mock_calc_proto.assert_has_calls(
[
mocker.call(False, "foo"),
mocker.call(True, {}),
]
)
mock_calc_lambda.assert_not_called()


class TestSolarWindsApmConfigServiceNameApmProto:
def test__calculate_service_name_apm_proto_agent_disabled(self):
test_config = apm_config.SolarWindsApmConfig()
result = test_config._calculate_service_name_apm_proto(
False,
{}
)
assert result == ""

def test__calculate_service_name_apm_proto_no_otel_service_name(
self,
mocker,
):
mocker.patch.dict(os.environ, {
"SW_APM_SERVICE_KEY": "service_key_with:sw_service_name",
})
test_config = apm_config.SolarWindsApmConfig()
result = test_config._calculate_service_name_apm_proto(
True,
Resource.create({"service.name": None})
)
assert result == "sw_service_name"

def test__calculate_service_name_apm_proto_default_unknown_otel_service_name(
self,
mocker,
):
mocker.patch.dict(os.environ, {
"SW_APM_SERVICE_KEY": "service_key_with:sw_service_name",
})
test_config = apm_config.SolarWindsApmConfig()
result = test_config._calculate_service_name_apm_proto(
True,
# default is unknown_service
Resource.create()
)
assert result == "sw_service_name"

def test__calculate_service_name_apm_proto_use_otel_service_name(
self,
mocker,
):
mocker.patch.dict(os.environ, {
"SW_APM_SERVICE_KEY": "service_key_with:sw_service_name",
})
test_config = apm_config.SolarWindsApmConfig()
result = test_config._calculate_service_name_apm_proto(
True,
Resource.create({"service.name": "foobar"})
)
assert result == "foobar"

class TestSolarWindsApmConfigServiceNameLambda:
def test__calculate_service_name_lambda_no_otel_name(
self,
mocker,
):
mocker.patch.dict(os.environ, {
"AWS_LAMBDA_FUNCTION_NAME": "foo-fn",
})
test_config = apm_config.SolarWindsApmConfig()
result = test_config._calculate_service_name_lambda(
Resource.create({})
)
assert result == "foo-fn"

def test__calculate_service_name_lambda_empty_otel_name(
self,
mocker,
):
mocker.patch.dict(os.environ, {
"AWS_LAMBDA_FUNCTION_NAME": "foo-fn",
})
test_config = apm_config.SolarWindsApmConfig()
result = test_config._calculate_service_name_lambda(
Resource.create({"service.name": ""})
)
assert result == "foo-fn"

def test__calculate_service_name_lambda_otel_name_unknown(
self,
mocker,
):
mocker.patch.dict(os.environ, {
"AWS_LAMBDA_FUNCTION_NAME": "foo-fn",
})
test_config = apm_config.SolarWindsApmConfig()
result = test_config._calculate_service_name_lambda(
Resource.create({"service.name": "unknown_service"})
)
assert result == "foo-fn"

def test__calculate_service_name_lambda_otel_name_ok(
self,
mocker,
):
test_config = apm_config.SolarWindsApmConfig()
result = test_config._calculate_service_name_lambda(
Resource.create({"service.name": "foo-service-name"})
)
assert result == "foo-service-name"