diff --git a/tests/contrib/elasticsearch/test_elasticsearch.py b/tests/contrib/elasticsearch/test_elasticsearch.py index b80b4486e71..4a480c550c8 100644 --- a/tests/contrib/elasticsearch/test_elasticsearch.py +++ b/tests/contrib/elasticsearch/test_elasticsearch.py @@ -1,6 +1,7 @@ import datetime from http.client import HTTPConnection from importlib import import_module +import json import time import pytest @@ -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() @@ -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"