Skip to content

Commit 196dc00

Browse files
authored
Merge branch 'main' into allow-the-user-to-cancel-stream
2 parents 5b26e33 + d0500c2 commit 196dc00

15 files changed

Lines changed: 252 additions & 100 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5353
([#2474](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2474))
5454
- `opentelemetry-instrumentation-grpc` User should be able to cancel grpc stream
5555
([#2093](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2093))
56+
- `opentelemetry-instrumentation-elasticsearch` Improved support for version 8
57+
([#2420](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2420))
5658

5759
## Version 1.24.0/0.45b0 (2024-03-28)
5860

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ You can run `tox` with the following arguments:
6767
`black` and `isort` are executed when `tox -e lint` is run. The reported errors can be tedious to fix manually.
6868
An easier way to do so is:
6969

70-
1. Run `.tox/lint-some-package/bin/black .`
71-
2. Run `.tox/lint-some-package/bin/isort .`
70+
1. Run `.tox/lint/bin/black .`
71+
2. Run `.tox/lint/bin/isort .`
7272

7373
Or you can call formatting and linting in one command by [pre-commit](https://pre-commit.com/):
7474

instrumentation/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
| [opentelemetry-instrumentation-confluent-kafka](./opentelemetry-instrumentation-confluent-kafka) | confluent-kafka >= 1.8.2, <= 2.3.0 | No | experimental
1818
| [opentelemetry-instrumentation-dbapi](./opentelemetry-instrumentation-dbapi) | dbapi | No | experimental
1919
| [opentelemetry-instrumentation-django](./opentelemetry-instrumentation-django) | django >= 1.10 | Yes | experimental
20-
| [opentelemetry-instrumentation-elasticsearch](./opentelemetry-instrumentation-elasticsearch) | elasticsearch >= 2.0 | No | experimental
20+
| [opentelemetry-instrumentation-elasticsearch](./opentelemetry-instrumentation-elasticsearch) | elasticsearch >= 6.0 | No | experimental
2121
| [opentelemetry-instrumentation-falcon](./opentelemetry-instrumentation-falcon) | falcon >= 1.4.1, < 4.0.0 | Yes | experimental
2222
| [opentelemetry-instrumentation-fastapi](./opentelemetry-instrumentation-fastapi) | fastapi ~= 0.58 | Yes | experimental
2323
| [opentelemetry-instrumentation-flask](./opentelemetry-instrumentation-flask) | flask >= 1.0 | Yes | migration

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
request_hook (Callable) - a function with extra user-defined logic to be performed before performing the request
5252
this function signature is: def request_hook(span: Span, service_name: str, operation_name: str, api_params: dict) -> None
5353
response_hook (Callable) - a function with extra user-defined logic to be performed after performing the request
54-
this function signature is: def request_hook(span: Span, service_name: str, operation_name: str, result: dict) -> None
54+
this function signature is: def response_hook(span: Span, service_name: str, operation_name: str, result: dict) -> None
5555
5656
for example:
5757

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

Lines changed: 54 additions & 7 deletions
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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# limitations under the License.
1414

1515

16-
_instruments = ("elasticsearch >= 2.0",)
16+
_instruments = ("elasticsearch >= 6.0",)
Lines changed: 23 additions & 0 deletions
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==6.11.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

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

Lines changed: 6 additions & 0 deletions
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)

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

Lines changed: 6 additions & 0 deletions
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)

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

Lines changed: 20 additions & 1 deletion
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)