Skip to content

Commit 92b2699

Browse files
committed
elasticsearch: tests against elasticsearch 8
1 parent a889286 commit 92b2699

File tree

8 files changed

+189
-50
lines changed

8 files changed

+189
-50
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4949
([#2404](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2404))
5050
- Remove SDK dependency from opentelemetry-instrumentation-grpc
5151
([#2474](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2474))
52+
- `opentelemetry-instrumentation-elasticsearch` Improved support for version 8
53+
([#2420](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2420))
5254

5355
## Version 1.24.0/0.45b0 (2024-03-28)
5456

Diff for: instrumentation/opentelemetry-instrumentation-elasticsearch/src/opentelemetry/instrumentation/elasticsearch/__init__.py

+54-7
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def response_hook(span, response):
9494
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
9595
from opentelemetry.instrumentation.utils import unwrap
9696
from opentelemetry.semconv.trace import SpanAttributes
97-
from opentelemetry.trace import SpanKind, get_tracer
97+
from opentelemetry.trace import SpanKind, Status, StatusCode, get_tracer
9898

9999
from .utils import sanitize_body
100100

@@ -103,6 +103,7 @@ def response_hook(span, response):
103103
es_transport_split = elasticsearch.VERSION[0] > 7
104104
if es_transport_split:
105105
import elastic_transport
106+
from elastic_transport._models import DefaultType
106107

107108
logger = getLogger(__name__)
108109

@@ -173,7 +174,12 @@ def _instrument(self, **kwargs):
173174

174175
def _uninstrument(self, **kwargs):
175176
# pylint: disable=no-member
176-
unwrap(elasticsearch.Transport, "perform_request")
177+
transport_class = (
178+
elastic_transport.Transport
179+
if es_transport_split
180+
else elasticsearch.Transport
181+
)
182+
unwrap(transport_class, "perform_request")
177183

178184

179185
_regex_doc_url = re.compile(r"/_doc/([^/]+)")
@@ -182,6 +188,7 @@ def _uninstrument(self, **kwargs):
182188
_regex_search_url = re.compile(r"/([^/]+)/_search[/]?")
183189

184190

191+
# pylint: disable=too-many-statements
185192
def _wrap_perform_request(
186193
tracer,
187194
span_name_prefix,
@@ -234,7 +241,22 @@ def wrapper(wrapped, _, args, kwargs):
234241
kind=SpanKind.CLIENT,
235242
) as span:
236243
if callable(request_hook):
237-
request_hook(span, method, url, kwargs)
244+
# elasticsearch 8 changed the parameters quite a bit
245+
if es_transport_split:
246+
247+
def normalize_kwargs(k, v):
248+
if isinstance(v, DefaultType):
249+
v = str(v)
250+
elif isinstance(v, elastic_transport.HttpHeaders):
251+
v = dict(v)
252+
return (k, v)
253+
254+
hook_kwargs = dict(
255+
normalize_kwargs(k, v) for k, v in kwargs.items()
256+
)
257+
else:
258+
hook_kwargs = kwargs
259+
request_hook(span, method, url, hook_kwargs)
238260

239261
if span.is_recording():
240262
attributes = {
@@ -260,16 +282,41 @@ def wrapper(wrapped, _, args, kwargs):
260282
span.set_attribute(key, value)
261283

262284
rv = wrapped(*args, **kwargs)
263-
if isinstance(rv, dict) and span.is_recording():
285+
286+
body = rv.body if es_transport_split else rv
287+
if isinstance(body, dict) and span.is_recording():
264288
for member in _ATTRIBUTES_FROM_RESULT:
265-
if member in rv:
289+
if member in body:
266290
span.set_attribute(
267291
f"elasticsearch.{member}",
268-
str(rv[member]),
292+
str(body[member]),
293+
)
294+
295+
# since the transport split the raising of exceptions that set the error status
296+
# are called after this code so need to set error status manually
297+
if es_transport_split and span.is_recording():
298+
if not (method == "HEAD" and rv.meta.status == 404) and (
299+
not 200 <= rv.meta.status < 299
300+
):
301+
exception = elasticsearch.exceptions.HTTP_EXCEPTIONS.get(
302+
rv.meta.status, elasticsearch.exceptions.ApiError
303+
)
304+
message = str(body)
305+
if isinstance(body, dict):
306+
error = body.get("error", message)
307+
if isinstance(error, dict) and "type" in error:
308+
error = error["type"]
309+
message = error
310+
311+
span.set_status(
312+
Status(
313+
status_code=StatusCode.ERROR,
314+
description=f"{exception.__name__}: {message}",
269315
)
316+
)
270317

271318
if callable(response_hook):
272-
response_hook(span, rv)
319+
response_hook(span, body)
273320
return rv
274321

275322
return wrapper
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
asgiref==3.7.2
2+
attrs==23.2.0
3+
Deprecated==1.2.14
4+
elasticsearch==8.12.1
5+
elasticsearch-dsl==8.12.0
6+
elastic-transport==8.12.0
7+
importlib-metadata==7.1.0
8+
iniconfig==2.0.0
9+
packaging==23.2
10+
pluggy==1.4.0
11+
py==1.11.0
12+
py-cpuinfo==9.0.0
13+
pytest==7.1.3
14+
pytest-benchmark==4.0.0
15+
python-dateutil==2.8.2
16+
six==1.16.0
17+
tomli==2.0.1
18+
typing_extensions==4.10.0
19+
urllib3==2.2.1
20+
wrapt==1.16.0
21+
zipp==3.17.0
22+
-e opentelemetry-instrumentation
23+
-e instrumentation/opentelemetry-instrumentation-elasticsearch

Diff for: instrumentation/opentelemetry-instrumentation-elasticsearch/tests/helpers_es6.py

+6
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ class Index:
3131
dsl_index_span_name = "Elasticsearch/test-index/doc/2"
3232
dsl_index_url = "/test-index/doc/2"
3333
dsl_search_method = "GET"
34+
35+
perform_request_mock_path = "elasticsearch.connection.http_urllib3.Urllib3HttpConnection.perform_request"
36+
37+
38+
def mock_response(body: str, status_code: int = 200):
39+
return (status_code, {}, body)

Diff for: instrumentation/opentelemetry-instrumentation-elasticsearch/tests/helpers_es7.py

+6
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ class Index:
2929
dsl_index_span_name = "Elasticsearch/test-index/_doc/:id"
3030
dsl_index_url = "/test-index/_doc/2"
3131
dsl_search_method = "POST"
32+
33+
perform_request_mock_path = "elasticsearch.connection.http_urllib3.Urllib3HttpConnection.perform_request"
34+
35+
36+
def mock_response(body: str, status_code: int = 200):
37+
return (status_code, {}, body)

Diff for: instrumentation/opentelemetry-instrumentation-elasticsearch/tests/helpers_es8.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from elastic_transport import ApiResponseMeta, HttpHeaders
16+
from elastic_transport._node import NodeApiResponse
1517
from elasticsearch_dsl import Document, Keyword, Text
1618

1719

@@ -36,6 +38,23 @@ class Index:
3638
}
3739
}
3840
dsl_index_result = (1, {}, '{"result": "created"}')
39-
dsl_index_span_name = "Elasticsearch/test-index/_doc/2"
41+
dsl_index_span_name = "Elasticsearch/test-index/_doc/:id"
4042
dsl_index_url = "/test-index/_doc/2"
4143
dsl_search_method = "POST"
44+
45+
perform_request_mock_path = (
46+
"elastic_transport._node._http_urllib3.Urllib3HttpNode.perform_request"
47+
)
48+
49+
50+
def mock_response(body: str, status_code: int = 200):
51+
return NodeApiResponse(
52+
ApiResponseMeta(
53+
status=status_code,
54+
headers=HttpHeaders({}),
55+
duration=100,
56+
http_version="1.1",
57+
node="node",
58+
),
59+
body.encode(),
60+
)

0 commit comments

Comments
 (0)