Skip to content

Commit 33401e8

Browse files
committed
full function name with module name and class name
1 parent a9bcd78 commit 33401e8

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/code_correlation/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ def add_code_attributes_to_span(span, func_or_class: Callable[..., Any]) -> None
4848
return
4949

5050
if is_class:
51-
span.set_attribute(CODE_FUNCTION_NAME, func_or_class.__name__)
51+
span.set_attribute(CODE_FUNCTION_NAME, f"{func_or_class.__module__}.{func_or_class.__qualname__}")
5252
span.set_attribute(CODE_FILE_PATH, inspect.getfile(func_or_class))
5353
else:
5454
code = getattr(func_or_class, "__code__", None)
5555
if code:
56-
span.set_attribute(CODE_FUNCTION_NAME, func_or_class.__name__)
56+
span.set_attribute(CODE_FUNCTION_NAME, f"{func_or_class.__module__}.{func_or_class.__qualname__}")
5757
span.set_attribute(CODE_FILE_PATH, code.co_filename)
5858
span.set_attribute(CODE_LINE_NUMBER, code.co_firstlineno)
5959
except Exception: # pylint: disable=broad-exception-caught

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/code_correlation/test_code_correlation.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ def test_function():
4040
add_code_attributes_to_span(mock_span, test_function)
4141

4242
# Verify function name attribute is set
43-
mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, "test_function")
43+
expected_function_name = f"{test_function.__module__}.{test_function.__qualname__}"
44+
mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, expected_function_name)
4445

4546
# Verify file path attribute is set
4647
expected_file_path = test_function.__code__.co_filename
@@ -62,7 +63,8 @@ class TestClass:
6263
add_code_attributes_to_span(mock_span, TestClass)
6364

6465
# Verify class name attribute is set
65-
mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, "TestClass")
66+
expected_class_name = f"{TestClass.__module__}.{TestClass.__qualname__}"
67+
mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, expected_class_name)
6668

6769
# Verify file path attribute is set (classes have file paths too)
6870
mock_span.set_attribute.assert_any_call(CODE_FILE_PATH, __file__)
@@ -150,7 +152,8 @@ def test_sync_function(arg1, arg2=None):
150152
self.assertEqual(result, "sync result: test_arg, test_kwarg")
151153

152154
# Verify span attributes were set
153-
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, "test_sync_function")
155+
expected_function_name = f"{test_sync_function.__module__}.{test_sync_function.__qualname__}"
156+
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, expected_function_name)
154157

155158
@patch("amazon.opentelemetry.distro.code_correlation.trace.get_current_span")
156159
def test_decorator_async_function(self, mock_get_current_span):
@@ -173,7 +176,8 @@ async def test_async_function(arg1, arg2=None):
173176
self.assertEqual(result, "async result: test_arg, test_kwarg")
174177

175178
# Verify span attributes were set
176-
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, "test_async_function")
179+
expected_function_name = f"{test_async_function.__module__}.{test_async_function.__qualname__}"
180+
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, expected_function_name)
177181

178182
@patch("amazon.opentelemetry.distro.code_correlation.trace.get_current_span")
179183
def test_decorator_no_current_span(self, mock_get_current_span):
@@ -255,7 +259,8 @@ def test_function():
255259
test_function()
256260

257261
# Verify span attributes were still set before exception
258-
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, "test_function")
262+
expected_function_name = f"{test_function.__module__}.{test_function.__qualname__}"
263+
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, expected_function_name)
259264

260265
@patch("amazon.opentelemetry.distro.code_correlation.trace.get_current_span")
261266
def test_decorator_with_async_function_that_raises_exception(self, mock_get_current_span):
@@ -276,7 +281,8 @@ async def test_async_function():
276281
loop.close()
277282

278283
# Verify span attributes were still set before exception
279-
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, "test_async_function")
284+
expected_function_name = f"{test_async_function.__module__}.{test_async_function.__qualname__}"
285+
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, expected_function_name)
280286

281287
@patch("amazon.opentelemetry.distro.code_correlation.add_code_attributes_to_span")
282288
@patch("amazon.opentelemetry.distro.code_correlation.trace.get_current_span")

0 commit comments

Comments
 (0)