Skip to content

Commit 84706d6

Browse files
lint
1 parent c67429c commit 84706d6

File tree

7 files changed

+133
-127
lines changed

7 files changed

+133
-127
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ def _set_remote_type_and_identifier(span: ReadableSpan, attributes: BoundedAttri
530530

531531

532532
def _set_remote_account_id_and_region(span: ReadableSpan, attributes: BoundedAttributes) -> bool:
533-
ARN_ATTRIBUTES = [
533+
arn_attributes = [
534534
AWS_DYNAMODB_TABLE_ARN,
535535
AWS_KINESIS_STREAM_ARN,
536536
AWS_SNS_TOPIC_ARN,
@@ -548,7 +548,7 @@ def _set_remote_account_id_and_region(span: ReadableSpan, attributes: BoundedAtt
548548
remote_account_id = SqsUrlParser.get_account_id(queue_url)
549549
remote_region = SqsUrlParser.get_region(queue_url)
550550
else:
551-
for arn_attribute in ARN_ATTRIBUTES:
551+
for arn_attribute in arn_attributes:
552552
if is_key_present(span, arn_attribute):
553553
arn = span.attributes.get(arn_attribute)
554554
remote_account_id = RegionalResourceArnParser.get_account_id(arn)

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/patches/_botocore_patches.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@
4444
_BotoResultT,
4545
)
4646
from opentelemetry.instrumentation.botocore.utils import get_server_attributes
47-
from opentelemetry.instrumentation.utils import (
48-
is_instrumentation_enabled,
49-
suppress_http_instrumentation,
50-
)
47+
from opentelemetry.instrumentation.utils import is_instrumentation_enabled, suppress_http_instrumentation
5148
from opentelemetry.semconv.trace import SpanAttributes
5249
from opentelemetry.trace.span import Span
5350

@@ -250,6 +247,7 @@ def patch_on_success(self, span: Span, result: _BotoResultT, instrumentor_contex
250247

251248

252249
def _apply_botocore_api_call_patch() -> None:
250+
# pylint: disable=too-many-locals
253251
def patched_api_call(self, original_func, instance, args, kwargs):
254252
"""Botocore instrumentation patch to capture AWS authentication details
255253
@@ -262,7 +260,9 @@ def patched_api_call(self, original_func, instance, args, kwargs):
262260
1. Propose refactoring upstream _patched_api_call into smaller components
263261
2. Apply targeted patches to these components to reduce code duplication
264262
265-
Reference: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/release/v1.33.x-0.54bx/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py#L263
263+
Reference: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/
264+
release/v1.33.x-0.54bx/instrumentation/opentelemetry-instrumentation-botocore/src/
265+
opentelemetry/instrumentation/botocore/__init__.py#L263
266266
"""
267267
if not is_instrumentation_enabled():
268268
return original_func(*args, **kwargs)
@@ -284,8 +284,8 @@ def patched_api_call(self, original_func, instance, args, kwargs):
284284
**get_server_attributes(call_context.endpoint_url),
285285
AWS_AUTH_REGION: call_context.region,
286286
}
287-
credentials = instance._get_credentials()
288287

288+
credentials = instance._get_credentials()
289289
if credentials is not None:
290290
access_key = credentials.access_key
291291
if access_key is not None:

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def get_queue_name(url: str) -> Optional[str]:
1919
"""
2020
if url is None:
2121
return None
22-
urlWithoutProtocol = url.replace(_HTTP_SCHEMA, "").replace(_HTTPS_SCHEMA, "")
23-
split_url: List[Optional[str]] = urlWithoutProtocol.split("/")
22+
url_without_protocol = url.replace(_HTTP_SCHEMA, "").replace(_HTTPS_SCHEMA, "")
23+
split_url: List[Optional[str]] = url_without_protocol.split("/")
2424
if len(split_url) == 3 and is_account_id(split_url[1]) and _is_valid_queue_name(split_url[2]):
2525
return split_url[2]
2626
return None
@@ -48,8 +48,8 @@ def parse_url(url: str) -> Tuple[Optional[str], Optional[str], Optional[str]]:
4848
if url is None:
4949
return None, None, None
5050

51-
urlWithoutProtocol = url.replace(_HTTP_SCHEMA, "").replace(_HTTPS_SCHEMA, "")
52-
split_url: List[Optional[str]] = urlWithoutProtocol.split("/")
51+
url_without_protocol = url.replace(_HTTP_SCHEMA, "").replace(_HTTPS_SCHEMA, "")
52+
split_url: List[Optional[str]] = url_without_protocol.split("/")
5353
if (
5454
len(split_url) != 3
5555
or not is_account_id(split_url[1])

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

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ def _test_patched_api_call_with_credentials(self):
275275
args = ("operation_name",)
276276
kwargs = {}
277277
initial_attributes = {}
278-
mock_extension = self._get_mock_extension()
279-
mock_call_context = self._get_mock_call_context()
278+
mock_extension = _get_mock_extension()
279+
mock_call_context = _get_mock_call_context()
280280

281281
def mock_start_span(*args, **kwargs):
282282
attributes = kwargs.get("attributes", {})
@@ -326,8 +326,8 @@ def _test_patched_api_call_with_no_credentials(self):
326326
args = ("operation_name",)
327327
kwargs = {}
328328
initial_attributes = {}
329-
mock_extension = self._get_mock_extension()
330-
mock_call_context = self._get_mock_call_context()
329+
mock_extension = _get_mock_extension()
330+
mock_call_context = _get_mock_call_context()
331331

332332
def mock_start_span(*args, **kwargs):
333333
attributes = kwargs.get("attributes", {})
@@ -372,8 +372,8 @@ def _test_patched_api_call_with_no_access_key(self):
372372
args = ("operation_name",)
373373
kwargs = {}
374374
initial_attributes = {}
375-
mock_extension = self._get_mock_extension()
376-
mock_call_context = self._get_mock_call_context()
375+
mock_extension = _get_mock_extension()
376+
mock_call_context = _get_mock_call_context()
377377

378378
def mock_start_span(*args, **kwargs):
379379
attributes = kwargs.get("attributes", {})
@@ -411,34 +411,6 @@ def mock_start_span(*args, **kwargs):
411411
self.assertTrue("aws.region" in initial_attributes)
412412
instrumentor.uninstrument()
413413

414-
def _get_mock_extension(self):
415-
# Mock extension
416-
mock_extension = MagicMock()
417-
mock_extension.should_trace_service_call.return_value = True
418-
mock_extension.tracer_schema_version.return_value = "1.0.0"
419-
mock_extension.event_logger_schema_version.return_value = "1.0.0"
420-
mock_extension.meter_schema_version.return_value = "1.0.0"
421-
mock_extension.should_end_span_on_exit.return_value = True
422-
mock_extension.extract_attributes = lambda x: None
423-
mock_extension.before_service_call = lambda *args, **kwargs: None
424-
mock_extension.after_service_call = lambda *args, **kwargs: None
425-
mock_extension.on_success = lambda *args, **kwargs: None
426-
mock_extension.on_error = lambda *args, **kwargs: None
427-
mock_extension.setup_metrics = lambda meter, metrics: None
428-
return mock_extension
429-
430-
def _get_mock_call_context(self):
431-
# Mock call context
432-
mock_call_context = MagicMock()
433-
mock_call_context.service = "test-service"
434-
mock_call_context.service_id = "test-service"
435-
mock_call_context.operation = "test-operation"
436-
mock_call_context.region = "us-west-2"
437-
mock_call_context.span_name = "test-span"
438-
mock_call_context.span_kind = "CLIENT"
439-
mock_call_context.endpoint_url = "https://www.awsmocktest.com"
440-
return mock_call_context
441-
442414
def _test_patched_gevent_os_ssl_instrumentation(self):
443415
# Only ssl and os module should have been patched since the environment variable was set to 'os, ssl'
444416
self.assertTrue(gevent.monkey.is_module_patched("ssl"), "gevent ssl module has not been patched")
@@ -711,3 +683,33 @@ def set_side_effect(set_key, set_value):
711683
extension.on_success(span_mock, result, mock_instrumentor_context)
712684

713685
return span_attributes
686+
687+
688+
def _get_mock_extension():
689+
# Mock extension
690+
mock_extension = MagicMock()
691+
mock_extension.should_trace_service_call.return_value = True
692+
mock_extension.tracer_schema_version.return_value = "1.0.0"
693+
mock_extension.event_logger_schema_version.return_value = "1.0.0"
694+
mock_extension.meter_schema_version.return_value = "1.0.0"
695+
mock_extension.should_end_span_on_exit.return_value = True
696+
mock_extension.extract_attributes = lambda x: None
697+
mock_extension.before_service_call = lambda *args, **kwargs: None
698+
mock_extension.after_service_call = lambda *args, **kwargs: None
699+
mock_extension.on_success = lambda *args, **kwargs: None
700+
mock_extension.on_error = lambda *args, **kwargs: None
701+
mock_extension.setup_metrics = lambda meter, metrics: None
702+
return mock_extension
703+
704+
705+
def _get_mock_call_context():
706+
# Mock call context
707+
mock_call_context = MagicMock()
708+
mock_call_context.service = "test-service"
709+
mock_call_context.service_id = "test-service"
710+
mock_call_context.operation = "test-operation"
711+
mock_call_context.region = "us-west-2"
712+
mock_call_context.span_name = "test-span"
713+
mock_call_context.span_kind = "CLIENT"
714+
mock_call_context.endpoint_url = "https://www.awsmocktest.com"
715+
return mock_call_context

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
14
from unittest import TestCase
25

36
from amazon.opentelemetry.distro.regional_resource_arn_parser import RegionalResourceArnParser
@@ -6,35 +9,35 @@
69
class TestRegionalResourceArnParser(TestCase):
710
def test_get_account_id(self):
811
# Test invalid ARN formats
9-
self.validateGetAccountId(None, None)
10-
self.validateGetAccountId("", None)
11-
self.validateGetAccountId(" ", None)
12-
self.validateGetAccountId(":", None)
13-
self.validateGetAccountId("::::::", None)
14-
self.validateGetAccountId("not:an:arn:string", None)
15-
self.validateGetAccountId("arn:aws:ec2:us-west-2:123456", None)
16-
self.validateGetAccountId("arn:aws:ec2:us-west-2:1234567xxxxx", None)
17-
self.validateGetAccountId("arn:aws:ec2:us-west-2:123456789012", None)
12+
self.validate_get_account_id(None, None)
13+
self.validate_get_account_id("", None)
14+
self.validate_get_account_id(" ", None)
15+
self.validate_get_account_id(":", None)
16+
self.validate_get_account_id("::::::", None)
17+
self.validate_get_account_id("not:an:arn:string", None)
18+
self.validate_get_account_id("arn:aws:ec2:us-west-2:123456", None)
19+
self.validate_get_account_id("arn:aws:ec2:us-west-2:1234567xxxxx", None)
20+
self.validate_get_account_id("arn:aws:ec2:us-west-2:123456789012", None)
1821

1922
# Test valid ARN formats
20-
self.validateGetAccountId("arn:aws:dynamodb:us-west-2:123456789012:table/test_table", "123456789012")
21-
self.validateGetAccountId("arn:aws:acm:us-east-1:123456789012:certificate:abc-123", "123456789012")
23+
self.validate_get_account_id("arn:aws:dynamodb:us-west-2:123456789012:table/test_table", "123456789012")
24+
self.validate_get_account_id("arn:aws:acm:us-east-1:123456789012:certificate:abc-123", "123456789012")
2225

2326
def test_get_region(self):
2427
# Test invalid ARN formats
25-
self.validateGetRegion(None, None)
26-
self.validateGetRegion("", None)
27-
self.validateGetRegion(" ", None)
28-
self.validateGetRegion(":", None)
29-
self.validateGetRegion("::::::", None)
30-
self.validateGetRegion("not:an:arn:string", None)
31-
self.validateGetRegion("arn:aws:ec2:us-west-2:123456", None)
32-
self.validateGetRegion("arn:aws:ec2:us-west-2:1234567xxxxx", None)
33-
self.validateGetRegion("arn:aws:ec2:us-west-2:123456789012", None)
28+
self.validate_get_region(None, None)
29+
self.validate_get_region("", None)
30+
self.validate_get_region(" ", None)
31+
self.validate_get_region(":", None)
32+
self.validate_get_region("::::::", None)
33+
self.validate_get_region("not:an:arn:string", None)
34+
self.validate_get_region("arn:aws:ec2:us-west-2:123456", None)
35+
self.validate_get_region("arn:aws:ec2:us-west-2:1234567xxxxx", None)
36+
self.validate_get_region("arn:aws:ec2:us-west-2:123456789012", None)
3437

3538
# Test valid ARN formats
36-
self.validateGetRegion("arn:aws:dynamodb:us-west-2:123456789012:table/test_table", "us-west-2")
37-
self.validateGetRegion("arn:aws:acm:us-east-1:123456789012:certificate:abc-123", "us-east-1")
39+
self.validate_get_region("arn:aws:dynamodb:us-west-2:123456789012:table/test_table", "us-west-2")
40+
self.validate_get_region("arn:aws:acm:us-east-1:123456789012:certificate:abc-123", "us-east-1")
3841

3942
def test_extract_dynamodb_table_name_from_arn(self):
4043
# Test invalid ARN formats
@@ -89,8 +92,8 @@ def validate_kinesis_stream_name(self, arn, expected_name):
8992
def validate_resource_name(self, arn, expected_name):
9093
self.assertEqual(RegionalResourceArnParser.extract_resource_name_from_arn(arn), expected_name)
9194

92-
def validateGetRegion(self, arn, expected_region):
95+
def validate_get_region(self, arn, expected_region):
9396
self.assertEqual(RegionalResourceArnParser.get_region(arn), expected_region)
9497

95-
def validateGetAccountId(self, arn, expected_account_id):
98+
def validate_get_account_id(self, arn, expected_account_id):
9699
self.assertEqual(RegionalResourceArnParser.get_account_id(arn), expected_account_id)

0 commit comments

Comments
 (0)