Skip to content
This repository has been archived by the owner on Jun 11, 2018. It is now read-only.

Commit

Permalink
fix issue with handling nonstandard s3 endpoint URLs
Browse files Browse the repository at this point in the history
closes #198
  • Loading branch information
beniwohli committed Feb 22, 2018
1 parent 4bdfe49 commit c4bc1d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
5 changes: 4 additions & 1 deletion opbeat/instrumentation/packages/botocore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
31 changes: 25 additions & 6 deletions tests/instrumentation/botocore_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'])

0 comments on commit c4bc1d3

Please sign in to comment.