Skip to content

Commit aa3d0a3

Browse files
committed
fix pylint
1 parent 3f9d2b7 commit aa3d0a3

File tree

4 files changed

+51
-52
lines changed

4 files changed

+51
-52
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_configurator.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -619,27 +619,29 @@ def _is_application_signals_runtime_enabled():
619619
def _get_code_correlation_enabled_status() -> Optional[bool]:
620620
"""
621621
Get the code correlation enabled status from environment variable.
622-
622+
623623
Returns:
624624
True if OTEL_AWS_CODE_CORRELATION_ENABLED is set to 'true'
625625
False if OTEL_AWS_CODE_CORRELATION_ENABLED is set to 'false'
626626
None if OTEL_AWS_CODE_CORRELATION_ENABLED is not set (default state)
627627
"""
628628
env_value = os.environ.get(CODE_CORRELATION_ENABLED_CONFIG)
629-
629+
630630
if env_value is None:
631631
return None # Default state - environment variable not set
632-
632+
633633
env_value_lower = env_value.strip().lower()
634634
if env_value_lower == "true":
635635
return True
636-
elif env_value_lower == "false":
636+
if env_value_lower == "false":
637637
return False
638-
else:
639-
# Invalid value, treat as default and log warning
640-
_logger.warning("Invalid value for %s: %s. Expected 'true' or 'false'. Using default.",
641-
CODE_CORRELATION_ENABLED_CONFIG, env_value)
642-
return None
638+
# Invalid value, treat as default and log warning
639+
_logger.warning(
640+
"Invalid value for %s: %s. Expected 'true' or 'false'. Using default.",
641+
CODE_CORRELATION_ENABLED_CONFIG,
642+
env_value,
643+
)
644+
return None
643645

644646

645647
def _is_lambda_environment():

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ def _add_code_attributes_to_span(span, func: Callable[..., Any]) -> None:
4040

4141
try:
4242
# Get function name
43-
function_name = getattr(func, '__name__', str(func))
43+
function_name = getattr(func, "__name__", str(func))
4444
span.set_attribute(CODE_FUNCTION_NAME, function_name)
4545

4646
# Get function source file from code object
4747
try:
48-
if hasattr(func, '__code__'):
48+
if hasattr(func, "__code__"):
4949
source_file = func.__code__.co_filename
5050
span.set_attribute(CODE_FILE_PATH, source_file)
5151
except (AttributeError, TypeError):
@@ -55,14 +55,14 @@ def _add_code_attributes_to_span(span, func: Callable[..., Any]) -> None:
5555

5656
# Get function line number from code object
5757
try:
58-
if hasattr(func, '__code__'):
58+
if hasattr(func, "__code__"):
5959
line_number = func.__code__.co_firstlineno
6060
span.set_attribute(CODE_LINE_NUMBER, line_number)
6161
except (AttributeError, TypeError):
6262
# Handle cases where code object is not available
6363
pass
6464

65-
except Exception:
65+
except Exception: # pylint: disable=broad-exception-caught
6666
# Silently handle any unexpected errors to avoid breaking
6767
# the instrumentation flow
6868
pass
@@ -99,8 +99,7 @@ async def my_async_function():
9999
"""
100100
# Detect async functions: check function code object flags or special attributes
101101
# CO_ITERABLE_COROUTINE = 0x80, async functions will have this flag set
102-
is_async = (hasattr(func, '__code__') and
103-
func.__code__.co_flags & 0x80) or hasattr(func, '_is_coroutine')
102+
is_async = (hasattr(func, "__code__") and func.__code__.co_flags & 0x80) or hasattr(func, "_is_coroutine")
104103

105104
if is_async:
106105
# Async function wrapper
@@ -111,28 +110,28 @@ async def async_wrapper(*args, **kwargs):
111110
current_span = trace.get_current_span()
112111
if current_span:
113112
_add_code_attributes_to_span(current_span, func)
114-
except Exception:
113+
except Exception: # pylint: disable=broad-exception-caught
115114
# Silently handle any unexpected errors
116115
pass
117116

118117
# Call and await the original async function
119118
return await func(*args, **kwargs)
120119

121120
return async_wrapper
122-
else:
123-
# Sync function wrapper
124-
@wraps(func)
125-
def sync_wrapper(*args, **kwargs):
126-
# Add code attributes to current span
127-
try:
128-
current_span = trace.get_current_span()
129-
if current_span:
130-
_add_code_attributes_to_span(current_span, func)
131-
except Exception:
132-
# Silently handle any unexpected errors
133-
pass
134121

135-
# Call the original sync function
136-
return func(*args, **kwargs)
122+
# Sync function wrapper
123+
@wraps(func)
124+
def sync_wrapper(*args, **kwargs):
125+
# Add code attributes to current span
126+
try:
127+
current_span = trace.get_current_span()
128+
if current_span:
129+
_add_code_attributes_to_span(current_span, func)
130+
except Exception: # pylint: disable=broad-exception-caught
131+
# Silently handle any unexpected errors
132+
pass
133+
134+
# Call the original sync function
135+
return func(*args, **kwargs)
137136

138-
return sync_wrapper
137+
return sync_wrapper

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
import asyncio
55
from unittest import TestCase
6-
from unittest.mock import MagicMock, patch, PropertyMock
6+
from unittest.mock import MagicMock, PropertyMock, patch
77

88
from amazon.opentelemetry.distro.code_correlation import (
9-
CODE_FUNCTION_NAME,
109
CODE_FILE_PATH,
10+
CODE_FUNCTION_NAME,
1111
CODE_LINE_NUMBER,
1212
_add_code_attributes_to_span,
1313
add_code_attributes_to_span,
@@ -35,6 +35,7 @@ def setUp(self):
3535

3636
def test_add_code_attributes_to_recording_span(self):
3737
"""Test adding code attributes to a recording span."""
38+
3839
def test_function():
3940
pass
4041

@@ -68,7 +69,7 @@ def test_add_code_attributes_function_without_code(self):
6869
# Create a mock function without __code__ attribute
6970
mock_func = MagicMock()
7071
mock_func.__name__ = "mock_function"
71-
delattr(mock_func, '__code__')
72+
delattr(mock_func, "__code__")
7273

7374
_add_code_attributes_to_span(self.mock_span, mock_func)
7475

@@ -87,7 +88,7 @@ def test_add_code_attributes_function_without_name(self):
8788
"""Test handling of functions without __name__ attribute."""
8889
# Create an object without __name__ attribute
8990
mock_func = MagicMock()
90-
delattr(mock_func, '__name__')
91+
delattr(mock_func, "__name__")
9192
mock_func.__code__ = MagicMock()
9293
mock_func.__code__.co_filename = "/test/file.py"
9394
mock_func.__code__.co_firstlineno = 10
@@ -115,7 +116,7 @@ def test_add_code_attributes_exception_handling(self):
115116
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, "test_func")
116117
self.mock_span.set_attribute.assert_any_call(CODE_FILE_PATH, "/test/file.py")
117118

118-
@patch('amazon.opentelemetry.distro.code_correlation.getattr')
119+
@patch("amazon.opentelemetry.distro.code_correlation.getattr")
119120
def test_add_code_attributes_getattr_exception(self, mock_getattr):
120121
"""Test exception handling when getattr fails."""
121122
mock_getattr.side_effect = Exception("Test exception")
@@ -204,7 +205,7 @@ def setUp(self):
204205
self.mock_span = MagicMock(spec=Span)
205206
self.mock_span.is_recording.return_value = True
206207

207-
@patch('amazon.opentelemetry.distro.code_correlation.trace.get_current_span')
208+
@patch("amazon.opentelemetry.distro.code_correlation.trace.get_current_span")
208209
def test_decorator_sync_function(self, mock_get_current_span):
209210
"""Test decorator with synchronous function."""
210211
mock_get_current_span.return_value = self.mock_span
@@ -222,7 +223,7 @@ def test_sync_function(arg1, arg2=None):
222223
# Verify span attributes were set
223224
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, "test_sync_function")
224225

225-
@patch('amazon.opentelemetry.distro.code_correlation.trace.get_current_span')
226+
@patch("amazon.opentelemetry.distro.code_correlation.trace.get_current_span")
226227
def test_decorator_async_function(self, mock_get_current_span):
227228
"""Test decorator with asynchronous function."""
228229
mock_get_current_span.return_value = self.mock_span
@@ -245,7 +246,7 @@ async def test_async_function(arg1, arg2=None):
245246
# Verify span attributes were set
246247
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, "test_async_function")
247248

248-
@patch('amazon.opentelemetry.distro.code_correlation.trace.get_current_span')
249+
@patch("amazon.opentelemetry.distro.code_correlation.trace.get_current_span")
249250
def test_decorator_no_current_span(self, mock_get_current_span):
250251
"""Test decorator when there's no current span."""
251252
mock_get_current_span.return_value = None
@@ -263,7 +264,7 @@ def test_function():
263264
# Verify no span attributes were set
264265
self.mock_span.set_attribute.assert_not_called()
265266

266-
@patch('amazon.opentelemetry.distro.code_correlation.trace.get_current_span')
267+
@patch("amazon.opentelemetry.distro.code_correlation.trace.get_current_span")
267268
def test_decorator_exception_handling(self, mock_get_current_span):
268269
"""Test decorator handles exceptions gracefully."""
269270
mock_get_current_span.side_effect = Exception("Test exception")
@@ -280,6 +281,7 @@ def test_function():
280281

281282
def test_decorator_preserves_function_metadata(self):
282283
"""Test that decorator preserves original function metadata."""
284+
283285
@add_code_attributes_to_span
284286
def test_function():
285287
"""Test function docstring."""
@@ -291,6 +293,7 @@ def test_function():
291293

292294
def test_async_function_detection(self):
293295
"""Test that async functions are properly detected."""
296+
294297
# Create a regular function
295298
def sync_func():
296299
pass
@@ -309,7 +312,7 @@ async def async_func():
309312
# Check that async function returns a coroutine function
310313
self.assertTrue(asyncio.iscoroutinefunction(decorated_async))
311314

312-
@patch('amazon.opentelemetry.distro.code_correlation.trace.get_current_span')
315+
@patch("amazon.opentelemetry.distro.code_correlation.trace.get_current_span")
313316
def test_decorator_with_function_that_raises_exception(self, mock_get_current_span):
314317
"""Test decorator with function that raises exception."""
315318
mock_get_current_span.return_value = self.mock_span
@@ -325,7 +328,7 @@ def test_function():
325328
# Verify span attributes were still set before exception
326329
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, "test_function")
327330

328-
@patch('amazon.opentelemetry.distro.code_correlation.trace.get_current_span')
331+
@patch("amazon.opentelemetry.distro.code_correlation.trace.get_current_span")
329332
def test_decorator_with_async_function_that_raises_exception(self, mock_get_current_span):
330333
"""Test decorator with async function that raises exception."""
331334
mock_get_current_span.return_value = self.mock_span
@@ -346,8 +349,8 @@ async def test_async_function():
346349
# Verify span attributes were still set before exception
347350
self.mock_span.set_attribute.assert_any_call(CODE_FUNCTION_NAME, "test_async_function")
348351

349-
@patch('amazon.opentelemetry.distro.code_correlation._add_code_attributes_to_span')
350-
@patch('amazon.opentelemetry.distro.code_correlation.trace.get_current_span')
352+
@patch("amazon.opentelemetry.distro.code_correlation._add_code_attributes_to_span")
353+
@patch("amazon.opentelemetry.distro.code_correlation.trace.get_current_span")
351354
def test_decorator_internal_exception_handling_sync(self, mock_get_current_span, mock_add_attributes):
352355
"""Test that decorator handles internal exceptions gracefully in sync function."""
353356
mock_get_current_span.return_value = self.mock_span
@@ -364,8 +367,8 @@ def test_function():
364367
# Verify the function still works correctly despite internal exception
365368
self.assertEqual(result, "test result")
366369

367-
@patch('amazon.opentelemetry.distro.code_correlation._add_code_attributes_to_span')
368-
@patch('amazon.opentelemetry.distro.code_correlation.trace.get_current_span')
370+
@patch("amazon.opentelemetry.distro.code_correlation._add_code_attributes_to_span")
371+
@patch("amazon.opentelemetry.distro.code_correlation.trace.get_current_span")
369372
def test_decorator_internal_exception_handling_async(self, mock_get_current_span, mock_add_attributes):
370373
"""Test that decorator handles internal exceptions gracefully in async function."""
371374
mock_get_current_span.return_value = self.mock_span

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelementry_configurator.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from amazon.opentelemetry.distro.aws_lambda_span_processor import AwsLambdaSpanProcessor
1818
from amazon.opentelemetry.distro.aws_metric_attributes_span_exporter import AwsMetricAttributesSpanExporter
1919
from amazon.opentelemetry.distro.aws_opentelemetry_configurator import (
20+
CODE_CORRELATION_ENABLED_CONFIG,
2021
LAMBDA_SPAN_EXPORT_BATCH_SIZE,
2122
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
2223
OTEL_EXPORTER_OTLP_LOGS_HEADERS,
@@ -1428,9 +1429,6 @@ def test_create_emf_exporter_cloudwatch_exporter_import_error(
14281429

14291430
def test_get_code_correlation_enabled_status(self):
14301431
"""Test _get_code_correlation_enabled_status function with various environment variable values"""
1431-
# Import the constant we need
1432-
from amazon.opentelemetry.distro.aws_opentelemetry_configurator import CODE_CORRELATION_ENABLED_CONFIG
1433-
14341432
# Test when environment variable is not set (default state)
14351433
os.environ.pop(CODE_CORRELATION_ENABLED_CONFIG, None)
14361434
result = _get_code_correlation_enabled_status()
@@ -1472,9 +1470,6 @@ def test_get_code_correlation_enabled_status(self):
14721470
self.assertFalse(result)
14731471

14741472
# Test invalid values (should return None and log warning)
1475-
# We'll use caplog to capture log messages instead of mocking
1476-
import logging
1477-
14781473
os.environ[CODE_CORRELATION_ENABLED_CONFIG] = "invalid"
14791474
result = _get_code_correlation_enabled_status()
14801475
self.assertIsNone(result)

0 commit comments

Comments
 (0)