Skip to content

Commit 1bcc94c

Browse files
committed
add some None checks for the params argument in the ES instrumentation (#257)
fixes #251 closes #257 Signed-off-by: Benjamin Wohlwend <[email protected]>
1 parent 7909d9c commit 1bcc94c

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

elasticapm/instrumentation/packages/elasticsearch.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def call(self, module, method, wrapped, instance, args, kwargs):
3232
http_method = args[0] if args_len else kwargs.get('method')
3333
http_path = args[1] if args_len > 1 else kwargs.get('url')
3434
params = args[2] if args_len > 2 else kwargs.get('params')
35-
body = params.pop(BODY_REF_NAME, None)
35+
body = params.pop(BODY_REF_NAME, None) if params else None
3636

37-
api_method = params.pop(API_METHOD_KEY_NAME, None)
37+
api_method = params.pop(API_METHOD_KEY_NAME, None) if params else None
3838

3939
signature = 'ES %s %s' % (http_method, http_path)
4040
context = {'db': {'type': 'elasticsearch'}}
@@ -43,7 +43,7 @@ def call(self, module, method, wrapped, instance, args, kwargs):
4343
# using both q AND body is allowed in some API endpoints / ES versions,
4444
# but not in others. We simply capture both if they are there so the
4545
# user can see it.
46-
if 'q' in params:
46+
if params and 'q' in params:
4747
# 'q' is already encoded to a byte string at this point
4848
# we assume utf8, which is the default
4949
query.append('q=' + params['q'].decode('utf-8', errors='replace'))
@@ -132,8 +132,11 @@ def instrument(self):
132132
super(ElasticsearchInstrumentation, self).instrument()
133133

134134
def call(self, module, method, wrapped, instance, args, kwargs):
135+
params = kwargs.pop('params', {})
136+
135137
# make a copy of params in case the caller reuses them for some reason
136-
params = kwargs.pop('params', {}).copy()
138+
params = params.copy() if params is not None else {}
139+
137140
cls_name, method_name = method.split('.', 1)
138141
body_pos = (self.body_positions['all'].get(method_name) or
139142
self.body_positions[self.version].get(method_name) or None)

tests/instrumentation/elasticsearch_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def test_search_body(instrument, elasticapm_client, elasticsearch):
194194
elasticsearch.create(index='tweets', doc_type='doc', id=1, body={'user': 'kimchy', 'text': 'hola'}, refresh=True)
195195
elasticapm_client.begin_transaction('test')
196196
search_query = {"query": {"term": {"user": "kimchy"}}}
197-
result = elasticsearch.search(body=search_query)
197+
result = elasticsearch.search(body=search_query, params=None)
198198
transaction_obj = elasticapm_client.end_transaction('test', 'OK')
199199
assert result['hits']['hits'][0]['_source'] == {'user': 'kimchy', 'text': 'hola'}
200200
assert len(transaction_obj.spans) == 1

0 commit comments

Comments
 (0)