Skip to content

Commit e70437a

Browse files
tammy-baylis-swiocelotlshalevr
authored
Add conditional elastic_transport import (#1810)
* Add conditional elastic_transport import * Update changelog * Add future es8 tests * Update CHANGELOG.md Co-authored-by: Diego Hurtado <[email protected]> * Add license, rm pylint disable * Consistent elastic version check * lint import * Update CHANGELOG.md --------- Co-authored-by: Diego Hurtado <[email protected]> Co-authored-by: Shalev Roda <[email protected]>
1 parent c9004bd commit e70437a

File tree

5 files changed

+76
-11
lines changed

5 files changed

+76
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3131
([#1789](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1789))
3232
- `opentelemetry-instrumentation-grpc` Allow gRPC connections via Unix socket
3333
([#1833](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1833))
34+
- Fix elasticsearch `Transport.perform_request` instrument wrap for elasticsearch >= 8
35+
([#1810](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1810))
3436

3537
## Version 1.18.0/0.39b0 (2023-05-10)
3638

instrumentation/opentelemetry-instrumentation-elasticsearch/src/opentelemetry/instrumentation/elasticsearch/__init__.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ def response_hook(span, response):
9898

9999
from .utils import sanitize_body
100100

101+
# Split of elasticsearch and elastic_transport in 8.0.0+
102+
# https://www.elastic.co/guide/en/elasticsearch/client/python-api/master/release-notes.html#rn-8-0-0
103+
es_transport_split = elasticsearch.VERSION[0] > 7
104+
if es_transport_split:
105+
import elastic_transport
106+
101107
logger = getLogger(__name__)
102108

103109

@@ -137,16 +143,28 @@ def _instrument(self, **kwargs):
137143
tracer = get_tracer(__name__, __version__, tracer_provider)
138144
request_hook = kwargs.get("request_hook")
139145
response_hook = kwargs.get("response_hook")
140-
_wrap(
141-
elasticsearch,
142-
"Transport.perform_request",
143-
_wrap_perform_request(
144-
tracer,
145-
self._span_name_prefix,
146-
request_hook,
147-
response_hook,
148-
),
149-
)
146+
if es_transport_split:
147+
_wrap(
148+
elastic_transport,
149+
"Transport.perform_request",
150+
_wrap_perform_request(
151+
tracer,
152+
self._span_name_prefix,
153+
request_hook,
154+
response_hook,
155+
),
156+
)
157+
else:
158+
_wrap(
159+
elasticsearch,
160+
"Transport.perform_request",
161+
_wrap_perform_request(
162+
tracer,
163+
self._span_name_prefix,
164+
request_hook,
165+
response_hook,
166+
),
167+
)
150168

151169
def _uninstrument(self, **kwargs):
152170
unwrap(elasticsearch.Transport, "perform_request")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from elasticsearch_dsl import Document, Keyword, Text
16+
17+
18+
class Article(Document):
19+
title = Text(analyzer="snowball", fields={"raw": Keyword()})
20+
body = Text(analyzer="snowball")
21+
22+
class Index:
23+
name = "test-index"
24+
25+
26+
dsl_create_statement = {
27+
"mappings": {
28+
"properties": {
29+
"title": {
30+
"analyzer": "snowball",
31+
"fields": {"raw": {"type": "keyword"}},
32+
"type": "text",
33+
},
34+
"body": {"analyzer": "snowball", "type": "text"},
35+
}
36+
}
37+
}
38+
dsl_index_result = (1, {}, '{"result": "created"}')
39+
dsl_index_span_name = "Elasticsearch/test-index/_doc/2"
40+
dsl_index_url = "/test-index/_doc/2"
41+
dsl_search_method = "POST"

instrumentation/opentelemetry-instrumentation-elasticsearch/tests/test_elasticsearch.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737

3838
major_version = elasticsearch.VERSION[0]
3939

40-
if major_version == 7:
40+
if major_version == 8:
41+
from . import helpers_es8 as helpers # pylint: disable=no-name-in-module
42+
elif major_version == 7:
4143
from . import helpers_es7 as helpers # pylint: disable=no-name-in-module
4244
elif major_version == 6:
4345
from . import helpers_es6 as helpers # pylint: disable=no-name-in-module

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ deps =
255255
; FIXME: Elasticsearch >=7 causes CI workflow tests to hang, see open-telemetry/opentelemetry-python-contrib#620
256256
; elasticsearch7: elasticsearch-dsl>=7.0,<8.0
257257
; elasticsearch7: elasticsearch>=7.0,<8.0
258+
; elasticsearch8: elasticsearch-dsl>=8.0,<9.0
259+
; elasticsearch8: elasticsearch>=8.0,<9.0
258260
falcon1: falcon ==1.4.1
259261
falcon2: falcon >=2.0.0,<3.0.0
260262
falcon3: falcon >=3.0.0,<4.0.0

0 commit comments

Comments
 (0)