Skip to content

Commit d5419bb

Browse files
fix(elasticsearch): elasticsearch8 circular import error [backport #5753 to 1.12] (#5791)
Backport of #5753 to 1.12 Currently if using >= elasticsearch 8 the tracer will throw an error as it tries to patch, since we haven't added support for this version. This fix version guards patching, so users don't have to disable the integration manually. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) are followed. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] PR description includes explicit acknowledgement/acceptance of the performance implications of this PR as reported in the benchmarks PR comment. ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. Co-authored-by: Brett Langdon <[email protected]>
1 parent 0eaec8c commit d5419bb

File tree

10 files changed

+185
-1
lines changed

10 files changed

+185
-1
lines changed

.riot/requirements/131d266.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.7
3+
# by the following command:
4+
#
5+
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/131d266.in
6+
#
7+
attrs==23.1.0
8+
certifi==2022.12.7
9+
coverage[toml]==7.2.5
10+
elastic-transport==8.4.0
11+
elasticsearch8==8.7.0
12+
exceptiongroup==1.1.1
13+
hypothesis==6.45.0
14+
importlib-metadata==6.6.0
15+
iniconfig==2.0.0
16+
mock==5.0.2
17+
opentracing==2.4.0
18+
packaging==23.1
19+
pluggy==1.0.0
20+
pytest==7.3.1
21+
pytest-cov==4.0.0
22+
pytest-mock==3.10.0
23+
sortedcontainers==2.4.0
24+
tomli==2.0.1
25+
typing-extensions==4.5.0
26+
urllib3==1.26.15
27+
zipp==3.15.0

.riot/requirements/15b846e.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.10
3+
# by the following command:
4+
#
5+
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/15b846e.in
6+
#
7+
attrs==23.1.0
8+
certifi==2022.12.7
9+
coverage[toml]==7.2.5
10+
elastic-transport==8.4.0
11+
elasticsearch8==8.7.0
12+
exceptiongroup==1.1.1
13+
hypothesis==6.45.0
14+
iniconfig==2.0.0
15+
mock==5.0.2
16+
opentracing==2.4.0
17+
packaging==23.1
18+
pluggy==1.0.0
19+
pytest==7.3.1
20+
pytest-cov==4.0.0
21+
pytest-mock==3.10.0
22+
sortedcontainers==2.4.0
23+
tomli==2.0.1
24+
urllib3==1.26.15

.riot/requirements/165f0ba.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.8
3+
# by the following command:
4+
#
5+
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/165f0ba.in
6+
#
7+
attrs==23.1.0
8+
certifi==2022.12.7
9+
coverage[toml]==7.2.5
10+
elastic-transport==8.4.0
11+
elasticsearch8==8.7.0
12+
exceptiongroup==1.1.1
13+
hypothesis==6.45.0
14+
iniconfig==2.0.0
15+
mock==5.0.2
16+
opentracing==2.4.0
17+
packaging==23.1
18+
pluggy==1.0.0
19+
pytest==7.3.1
20+
pytest-cov==4.0.0
21+
pytest-mock==3.10.0
22+
sortedcontainers==2.4.0
23+
tomli==2.0.1
24+
urllib3==1.26.15

.riot/requirements/1c63df9.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# This file is autogenerated by pip-compile with python 3.6
3+
# To update, run:
4+
#
5+
# pip-compile --no-annotate .riot/requirements/1c63df9.in
6+
#
7+
attrs==22.2.0
8+
certifi==2022.12.7
9+
coverage[toml]==6.2
10+
dataclasses==0.8
11+
elastic-transport==8.4.0
12+
elasticsearch8==8.7.0
13+
hypothesis==6.31.6
14+
importlib-metadata==4.8.3
15+
iniconfig==1.1.1
16+
mock==5.0.2
17+
opentracing==2.4.0
18+
packaging==21.3
19+
pluggy==1.0.0
20+
py==1.11.0
21+
pyparsing==3.0.9
22+
pytest==7.0.1
23+
pytest-cov==4.0.0
24+
pytest-mock==3.6.1
25+
sortedcontainers==2.4.0
26+
tomli==1.2.3
27+
typing-extensions==4.1.1
28+
urllib3==1.26.15
29+
zipp==3.6.0

.riot/requirements/84c2760.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.11
3+
# by the following command:
4+
#
5+
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/84c2760.in
6+
#
7+
attrs==23.1.0
8+
certifi==2022.12.7
9+
coverage[toml]==7.2.5
10+
elastic-transport==8.4.0
11+
elasticsearch8==8.7.0
12+
hypothesis==6.45.0
13+
iniconfig==2.0.0
14+
mock==5.0.2
15+
opentracing==2.4.0
16+
packaging==23.1
17+
pluggy==1.0.0
18+
pytest==7.3.1
19+
pytest-cov==4.0.0
20+
pytest-mock==3.10.0
21+
sortedcontainers==2.4.0
22+
urllib3==1.26.15

.riot/requirements/a362729.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.9
3+
# by the following command:
4+
#
5+
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/a362729.in
6+
#
7+
attrs==23.1.0
8+
certifi==2022.12.7
9+
coverage[toml]==7.2.5
10+
elastic-transport==8.4.0
11+
elasticsearch8==8.7.0
12+
exceptiongroup==1.1.1
13+
hypothesis==6.45.0
14+
iniconfig==2.0.0
15+
mock==5.0.2
16+
opentracing==2.4.0
17+
packaging==23.1
18+
pluggy==1.0.0
19+
pytest==7.3.1
20+
pytest-cov==4.0.0
21+
pytest-mock==3.10.0
22+
sortedcontainers==2.4.0
23+
tomli==2.0.1
24+
urllib3==1.26.15

ddtrace/contrib/elasticsearch/patch.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ddtrace._tracing import _limits
55
from ddtrace.contrib.trace_utils import ext_service
66
from ddtrace.internal.constants import COMPONENT
7+
from ddtrace.internal.logger import get_logger
78
from ddtrace.vendor.wrapt import wrap_function_wrapper as _w
89

910
from ...constants import ANALYTICS_SAMPLE_RATE_KEY
@@ -19,6 +20,8 @@
1920
from .quantize import quantize
2021

2122

23+
log = get_logger(__name__)
24+
2225
config._add(
2326
"elasticsearch",
2427
{
@@ -44,14 +47,19 @@ def _es_modules():
4447
pass
4548

4649

50+
def get_version(elasticsearch):
51+
return getattr(elasticsearch, "__version__", (0, 0, 0))
52+
53+
4754
# NB: We are patching the default elasticsearch.transport module
4855
def patch():
4956
for elasticsearch in _es_modules():
5057
_patch(elasticsearch)
5158

5259

5360
def _patch(elasticsearch):
54-
if getattr(elasticsearch, "_datadog_patch", False):
61+
# elasticsearch 8 is not yet supported
62+
if getattr(elasticsearch, "_datadog_patch", False) or get_version(elasticsearch) >= (8, 0, 0):
5563
return
5664
setattr(elasticsearch, "_datadog_patch", True)
5765
_w(elasticsearch.transport, "Transport.perform_request", _get_perform_request(elasticsearch))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
elasticsearch: This fix resolves an issue where the tracer would throw an error when patching
5+
unsupported versions of elasticsearch (> 8.0). Patching is now skipped if an unsupported version is detected.

riotfile.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,18 @@ def select_pys(min_version=MIN_PYTHON_VERSION, max_version=MAX_PYTHON_VERSION):
909909
),
910910
],
911911
),
912+
Venv(
913+
name="elasticsearch8-patch",
914+
command="pytest {cmdargs} tests/contrib/elasticsearch/test_es8_patch.py",
915+
venvs=[
916+
Venv(
917+
pys=select_pys(min_version="3.6"),
918+
pkgs={
919+
"elasticsearch8": [latest],
920+
},
921+
),
922+
],
923+
),
912924
Venv(
913925
name="elasticsearch-opensearch",
914926
# avoid running tests in ElasticsearchPatchTest, only run tests with OpenSearchPatchTest configurations
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from ddtrace.contrib.elasticsearch.patch import patch
2+
from tests.utils import TracerTestCase
3+
4+
5+
class ElasticsearchPatchTest(TracerTestCase):
6+
def test_patch(self):
7+
# This is to stop crashing applications if ES8 is used
8+
# If it doesn't error, we are good.
9+
patch()

0 commit comments

Comments
 (0)