Skip to content

Commit 1e107f8

Browse files
committed
fix an issue in boto3 instrumentation with nonstandard endpoint URLs (#178)
Specifically, this handles the case where the endpoint has no dots closes #176 closes #178
1 parent e0b8c5f commit 1e107f8

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

elasticapm/instrumentation/packages/botocore.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ def call(self, module, method, wrapped, instance, args, kwargs):
1818

1919
target_endpoint = instance._endpoint.host
2020
parsed_url = urlparse.urlparse(target_endpoint)
21-
service, region, _ = parsed_url.hostname.split('.', 2)
21+
if '.' in parsed_url.hostname:
22+
service, region = parsed_url.hostname.split('.', 2)[:2]
23+
else:
24+
service, region = parsed_url.hostname, None
2225

2326
signature = '{}:{}'.format(service, operation_name)
2427
extra_data = {
Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import boto3
22
import mock
33

4-
from elasticapm.traces import capture_span
4+
from elasticapm.instrumentation.packages.botocore import BotocoreInstrumentation
55

66

77
@mock.patch("botocore.endpoint.Endpoint.make_request")
@@ -11,14 +11,32 @@ def test_botocore_instrumentation(mock_make_request, elasticapm_client):
1111
mock_make_request.return_value = (mock_response, {})
1212

1313
elasticapm_client.begin_transaction("transaction.test")
14-
with capture_span("test_pipeline", "test"):
15-
session = boto3.Session(aws_access_key_id='foo',
16-
aws_secret_access_key='bar',
17-
region_name='us-west-2')
18-
ec2 = session.client('ec2')
19-
ec2.describe_instances()
20-
elasticapm_client.end_transaction("MyView")
21-
22-
transactions = elasticapm_client.instrumentation_store.get_all()
23-
spans = transactions[0]['spans']
24-
assert 'ec2:DescribeInstances' in map(lambda x: x['name'], spans)
14+
session = boto3.Session(aws_access_key_id='foo',
15+
aws_secret_access_key='bar',
16+
region_name='us-west-2')
17+
ec2 = session.client('ec2')
18+
ec2.describe_instances()
19+
transaction = elasticapm_client.end_transaction("MyView")
20+
span = transaction.spans[0]
21+
22+
assert span.name == 'ec2:DescribeInstances'
23+
assert span.context['service'] == 'ec2'
24+
assert span.context['region'] == 'us-west-2'
25+
assert span.context['operation'] == 'DescribeInstances'
26+
27+
28+
29+
def test_nonstandard_endpoint_url(elasticapm_client):
30+
instrument = BotocoreInstrumentation()
31+
elasticapm_client.begin_transaction('test')
32+
module, method = BotocoreInstrumentation.instrument_list[0]
33+
instance = mock.Mock(_endpoint=mock.Mock(host='https://example'))
34+
instrument.call(module, method, lambda *args, **kwargs: None, instance,
35+
('DescribeInstances',), {})
36+
transaction = elasticapm_client.end_transaction('test', 'test')
37+
span = transaction.spans[0]
38+
39+
assert span.name == 'example:DescribeInstances'
40+
assert span.context['service'] == 'example'
41+
assert span.context['region'] is None
42+
assert span.context['operation'] == 'DescribeInstances'

0 commit comments

Comments
 (0)