From 13c89e3c7f96594c533dc035972cc455384c12e3 Mon Sep 17 00:00:00 2001 From: Benjamin Wohlwend Date: Thu, 22 Feb 2018 15:05:22 +0100 Subject: [PATCH] fix issue with handling nonstandard s3 endpoint URLs closes #198 --- opbeat/instrumentation/packages/botocore.py | 5 +++- tests/instrumentation/botocore_tests.py | 32 ++++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/opbeat/instrumentation/packages/botocore.py b/opbeat/instrumentation/packages/botocore.py index 4038c171..5a38c0c6 100644 --- a/opbeat/instrumentation/packages/botocore.py +++ b/opbeat/instrumentation/packages/botocore.py @@ -18,7 +18,10 @@ def call(self, module, method, wrapped, instance, args, kwargs): target_endpoint = instance._endpoint.host parsed_url = urlparse.urlparse(target_endpoint) - service, region, _ = parsed_url.hostname.split('.', 2) + if '.' in parsed_url.hostname: + service, region = parsed_url.hostname.split('.', 2)[:2] + else: + service, region = parsed_url.hostname, None signature = '{}:{}'.format(service, operation_name) extra_data = { diff --git a/tests/instrumentation/botocore_tests.py b/tests/instrumentation/botocore_tests.py index 16e4a5f5..d14400d5 100644 --- a/tests/instrumentation/botocore_tests.py +++ b/tests/instrumentation/botocore_tests.py @@ -3,7 +3,7 @@ import opbeat import opbeat.instrumentation.control -from opbeat.traces import trace +from opbeat.instrumentation.packages.botocore import BotocoreInstrumentation from tests.helpers import get_tempstoreclient from tests.utils.compat import TestCase @@ -20,13 +20,31 @@ def test_botocore_instrumentation(self, mock_make_request): mock_make_request.return_value = (mock_response, {}) self.client.begin_transaction("transaction.test") - with trace("test_pipeline", "test"): - session = boto3.Session(aws_access_key_id='foo', - aws_secret_access_key='bar', - region_name='us-west-2') - ec2 = session.client('ec2') - ec2.describe_instances() + session = boto3.Session(aws_access_key_id='foo', + aws_secret_access_key='bar', + region_name='us-west-2') + ec2 = session.client('ec2') + ec2.describe_instances() self.client.end_transaction("MyView") _, traces = self.client.instrumentation_store.get_all() + trace = [t for t in traces if t['kind'] == 'ext.http.aws'][0] self.assertIn('ec2:DescribeInstances', map(lambda x: x['signature'], traces)) + self.assertEqual(trace['signature'], 'ec2:DescribeInstances') + self.assertEqual(trace['extra']['service'], 'ec2') + self.assertEqual(trace['extra']['region'], 'us-west-2') + + def test_nonstandard_endpoint_url(self): + instrument = BotocoreInstrumentation() + self.client.begin_transaction('test') + module, method = BotocoreInstrumentation.instrument_list[0] + instance = mock.Mock(_endpoint=mock.Mock(host='https://example')) + instrument.call(module, method, lambda *args, **kwargs: None, instance, + ('DescribeInstances',), {}) + self.client.end_transaction('test', 'test') + _, traces = self.client.instrumentation_store.get_all() + + trace = [t for t in traces if t['kind'] == 'ext.http.aws'][0] + self.assertEqual(trace['signature'], 'example:DescribeInstances') + self.assertEqual(trace['extra']['service'], 'example') + self.assertIsNone(trace['extra']['region'])