Skip to content

Commit 98db646

Browse files
authored
Handle track_total_hits=False in elasticsearch instrumentation (#1687)
* Handle track_total_hits=False * CHANGELOG * Fix test * Fix test (again)
1 parent 8b22d6a commit 98db646

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

CHANGELOG.asciidoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ endif::[]
2929
//===== Bug fixes
3030
//
3131
32+
=== Unreleased
33+
34+
// Unreleased changes go here
35+
// When the next release happens, nest these changes under the "Python Agent version 6.x" heading
36+
//[float]
37+
//===== Features
38+
//
39+
[float]
40+
===== Bug fixes
41+
42+
* Fix elasticsearch for track_total_hits=False {pull}1687[#1687]
43+
3244
[[release-notes-6.x]]
3345
=== Python Agent version 6.x
3446

elasticapm/instrumentation/packages/elasticsearch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ def _get_signature(self, args, kwargs):
177177

178178
def _get_hits(self, result) -> Optional[int]:
179179
if getattr(result, "body", None) and "hits" in result.body: # ES >= 8
180-
return result.body["hits"]["total"]["value"]
181-
elif isinstance(result, dict) and "hits" in result:
180+
return result.body["hits"].get("total", {}).get("value")
181+
elif isinstance(result, dict) and "hits" in result and "total" in result["hits"]:
182182
return (
183183
result["hits"]["total"]["value"]
184184
if isinstance(result["hits"]["total"], dict)

tests/instrumentation/elasticsearch_tests.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,32 @@ def test_search_body(instrument, elasticapm_client, elasticsearch):
399399
assert span["context"]["http"]["status_code"] == 200
400400

401401

402+
@pytest.mark.skipif(ES_VERSION[0] < 7, reason="track_total_hits unsupported")
403+
@pytest.mark.integrationtest
404+
def test_search_track_total_hits_false(instrument, elasticapm_client, elasticsearch):
405+
elasticsearch.create(
406+
index="tweets", id="1", refresh=True, **get_kwargs({"user": "kimchy", "text": "hola", "userid": 1})
407+
)
408+
elasticapm_client.begin_transaction("test")
409+
search_query = {"query": {"term": {"user": "kimchy"}}, "sort": ["userid"]}
410+
result = elasticsearch.search(body=search_query, track_total_hits=False)
411+
elasticapm_client.end_transaction("test", "OK")
412+
413+
transaction = elasticapm_client.events[TRANSACTION][0]
414+
assert result["hits"]["hits"][0]["_source"] == {"user": "kimchy", "text": "hola", "userid": 1}
415+
spans = elasticapm_client.spans_for_transaction(transaction)
416+
assert len(spans) == 1
417+
span = spans[0]
418+
# Depending on ES_VERSION, could be /_all/_search or /_search, and GET or POST
419+
assert span["name"] in ("ES GET /_search", "ES GET /_all/_search", "ES POST /_search")
420+
assert span["type"] == "db"
421+
assert span["subtype"] == "elasticsearch"
422+
assert span["action"] == "query"
423+
assert span["context"]["db"]["type"] == "elasticsearch"
424+
assert "rows_affected" not in span["context"]["db"]
425+
assert span["context"]["http"]["status_code"] == 200
426+
427+
402428
@pytest.mark.integrationtest
403429
def test_search_querystring(instrument, elasticapm_client, elasticsearch):
404430
elasticsearch.create(index="tweets", id="1", refresh=True, **get_kwargs({"user": "kimchy", "text": "hola"}))

0 commit comments

Comments
 (0)