Skip to content

Commit

Permalink
ci: fix flaky es search query [backport 2.19] (#11879)
Browse files Browse the repository at this point in the history
Co-authored-by: Brett Langdon <[email protected]>
  • Loading branch information
github-actions[bot] and brettlangdon authored Jan 8, 2025
1 parent 2ba151b commit 3d0089c
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions tests/contrib/elasticsearch/test_elasticsearch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
from http.client import HTTPConnection
from importlib import import_module
import json
import time

import pytest
Expand Down Expand Up @@ -167,7 +168,12 @@ def test_elasticsearch(self):
es.index(id=10, body={"name": "ten", "created": datetime.date(2016, 1, 1)}, **args)
es.index(id=11, body={"name": "eleven", "created": datetime.date(2016, 2, 1)}, **args)
es.index(id=12, body={"name": "twelve", "created": datetime.date(2016, 3, 1)}, **args)
result = es.search(sort=["name:desc"], size=100, body={"query": {"match_all": {}}}, **args)
result = es.search(
sort={"name": {"order": "desc", "unmapped_type": "keyword"}},
size=100,
body={"query": {"match_all": {}}},
**args,
)

assert len(result["hits"]["hits"]) == 3, result
spans = self.get_spans()
Expand All @@ -183,13 +189,25 @@ def test_elasticsearch(self):
assert url.endswith("/_search")
assert url == span.get_tag("elasticsearch.url")
if elasticsearch.__version__ >= (8, 0, 0):
assert span.get_tag("elasticsearch.body").replace(" ", "") == '{"query":{"match_all":{}},"size":100}'
assert set(span.get_tag("elasticsearch.params").split("&")) == {"sort=name%3Adesc"}
assert set(span.get_tag(http.QUERY_STRING).split("&")) == {"sort=name%3Adesc"}
# Key order is not consistent, parse into dict to compare
body = json.loads(span.get_tag("elasticsearch.body"))
assert body == {
"query": {"match_all": {}},
"sort": {"name": {"order": "desc", "unmapped_type": "keyword"}},
"size": 100,
}
assert not span.get_tag("elasticsearch.params")
assert not span.get_tag(http.QUERY_STRING)
else:
assert span.get_tag("elasticsearch.body").replace(" ", "") == '{"query":{"match_all":{}}}'
assert set(span.get_tag("elasticsearch.params").split("&")) == {"sort=name%3Adesc", "size=100"}
assert set(span.get_tag(http.QUERY_STRING).split("&")) == {"sort=name%3Adesc", "size=100"}
assert set(span.get_tag("elasticsearch.params").split("&")) == {
"sort=%7B%27name%27%3A+%7B%27order%27%3A+%27desc%27%2C+%27unmapped_type%27%3A+%27keyword%27%7D%7D",
"size=100",
}
assert set(span.get_tag(http.QUERY_STRING).split("&")) == {
"sort=%7B%27name%27%3A+%7B%27order%27%3A+%27desc%27%2C+%27unmapped_type%27%3A+%27keyword%27%7D%7D",
"size=100",
}
assert span.get_tag("component") == "elasticsearch"
assert span.get_tag("span.kind") == "client"

Expand Down

0 comments on commit 3d0089c

Please sign in to comment.