Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: fix flaky es search query [backport 2.19] #11879

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading