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..469f6c17 100644 --- a/tests/instrumentation/botocore_tests.py +++ b/tests/instrumentation/botocore_tests.py @@ -3,6 +3,7 @@ import opbeat import opbeat.instrumentation.control +from opbeat.instrumentation.packages.botocore import BotocoreInstrumentation from opbeat.traces import trace from tests.helpers import get_tempstoreclient from tests.utils.compat import TestCase @@ -20,13 +21,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 = traces[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 = traces[0] + self.assertEqual(trace['signature'], 'example:DescribeInstances') + self.assertEqual(trace['extra']['service'], 'example') + self.assertIsNone(trace['extra']['region'])