Skip to content

Commit 297ce60

Browse files
committed
Run OpenTelemetry integration tests separately
1 parent f5f5fd4 commit 297ce60

File tree

5 files changed

+51
-29
lines changed

5 files changed

+51
-29
lines changed

.buildkite/generatesteps.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import yaml
44

55

6-
def benchmark_to_steps(python, connection_class):
6+
def benchmark_to_steps(python, connection_class, nox_session):
77
return [
88
{
99
"group": f":elasticsearch: :python: ES Serverless ({python}/{connection_class})",
@@ -14,6 +14,7 @@ def benchmark_to_steps(python, connection_class):
1414
"env": {
1515
"PYTHON_VERSION": f"{python}",
1616
"PYTHON_CONNECTION_CLASS": f"{connection_class}",
17+
"NOX_SESSION": f"{nox_session}",
1718
# For development versions
1819
# https://github.com/aio-libs/aiohttp/issues/6600
1920
"AIOHTTP_NO_EXTENSIONS": 1,
@@ -55,5 +56,7 @@ def benchmark_to_steps(python, connection_class):
5556
steps = []
5657
for python in ["3.9", "3.10", "3.11", "3.12"]:
5758
for connection_class in ["urllib3", "requests"]:
58-
steps.extend(benchmark_to_steps(python, connection_class))
59+
steps.extend(benchmark_to_steps(python, connection_class, "test"))
60+
steps.extend(benchmark_to_steps("3.9", "urllib3", "test_otel"))
61+
steps.extend(benchmark_to_steps("3.12", "urllib3", "test_otel"))
5962
print(yaml.dump({"steps": steps}, Dumper=yaml.Dumper, sort_keys=False))

.buildkite/run-tests

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ echo -e "--- :computer: Environment variables"
5151
echo -e "ELASTICSEARCH_URL $ELASTICSEARCH_URL"
5252
echo -e "PYTHON_VERSION $PYTHON_VERSION"
5353
echo -e "PYTHON_CONNECTION_CLASS $PYTHON_CONNECTION_CLASS"
54+
echo -e "NOX_SESSION $NOX_SESSION"
5455

5556
echo -e "--- :docker: Build elasticsearch-serverless-python container"
5657

@@ -74,4 +75,4 @@ docker run \
7475
--volume "$(pwd)/junit:/code/elasticsearch-serverless-python/junit" \
7576
--rm \
7677
elasticsearch-serverless-python \
77-
nox -s "test-$PYTHON_VERSION"
78+
nox -s ${NOX_SESSION}-${PYTHON_VERSION}

noxfile.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,11 @@
3030
# Allow building aiohttp when no wheels are available (eg. for recent Python versions)
3131
INSTALL_ENV = {"AIOHTTP_NO_EXTENSIONS": "1"}
3232

33-
34-
@nox.session(python=["3.9", "3.10", "3.11", "3.12"])
35-
def test(session):
36-
session.install(".[dev]", env=INSTALL_ENV)
37-
33+
def pytest_argv():
3834
junit_xml = os.path.join(
3935
SOURCE_DIR, "junit", "elasticsearch-serverless-python-junit.xml"
4036
)
41-
pytest_argv = [
37+
return [
4238
"pytest",
4339
"--cov-report=term-missing",
4440
"--cov=elasticsearch_serverless",
@@ -47,10 +43,21 @@ def test(session):
4743
"--log-level=debug",
4844
"-vv",
4945
"--cache-clear",
50-
*(session.posargs),
5146
]
52-
session.run(*pytest_argv)
5347

48+
@nox.session(python=["3.9", "3.10", "3.11", "3.12"])
49+
def test(session):
50+
session.install(".[dev]", env=INSTALL_ENV)
51+
52+
session.run(*pytest_argv(), *(session.posargs))
53+
54+
@nox.session(python=["3.9", "3.12"])
55+
def test_otel(session):
56+
session.install(".[dev]", env=INSTALL_ENV)
57+
session.install("opentelemetry-api", "opentelemetry-sdk")
58+
59+
argv = pytest_argv() + ["-m", "otel"]
60+
session.run(*argv, *(session.posargs), env={"TEST_WITH_OTEL": "1"})
5461

5562
@nox.session()
5663
def format(session):

pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ dev = [
6969
"numpy",
7070
"pandas",
7171
"mapbox-vector-tile",
72-
"opentelemetry-api",
73-
"opentelemetry-sdk",
7472
]
7573
docs = [
7674
"sphinx-rtd-theme>=1.2.2",
@@ -101,7 +99,8 @@ packages = ["elasticsearch_serverless"]
10199

102100
[tool.pytest.ini_options]
103101
junit_family = "legacy"
104-
xfail_strict=true
102+
xfail_strict = true
103+
markers = "otel"
105104

106105
[tool.isort]
107106
profile = "black"

test_elasticsearch_serverless/test_server/test_otel.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,49 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from opentelemetry import trace
19-
from opentelemetry.sdk.trace import TracerProvider, export
20-
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
18+
import os
2119

22-
import elasticsearch
23-
from test_elasticsearch.utils import CA_CERTS
20+
import pytest
2421

22+
import elasticsearch_serverless
2523

26-
def test_otel_end_to_end(monkeypatch, elasticsearch_url: str):
24+
try:
25+
from opentelemetry import trace
26+
from opentelemetry.sdk.trace import TracerProvider, export
27+
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
28+
InMemorySpanExporter,
29+
)
30+
except ModuleNotFoundError:
31+
pass
32+
33+
pytestmark = [
34+
pytest.mark.skipif(
35+
"TEST_WITH_OTEL" not in os.environ, reason="TEST_WITH_OTEL is not set"
36+
),
37+
pytest.mark.otel,
38+
]
39+
40+
41+
def test_otel_end_to_end(monkeypatch, sync_client):
2742
# Sets the global default tracer provider
2843
tracer_provider = TracerProvider()
2944
memory_exporter = InMemorySpanExporter()
3045
span_processor = export.SimpleSpanProcessor(memory_exporter)
3146
tracer_provider.add_span_processor(span_processor)
3247
trace.set_tracer_provider(tracer_provider)
3348

34-
# Once OpenTelemetry is enabled by default, we can use the sync_client fixture instead
35-
monkeypatch.setenv("OTEL_PYTHON_INSTRUMENTATION_ELASTICSEARCH_ENABLED", "true")
36-
kw = {}
37-
if elasticsearch_url.startswith("https://"):
38-
kw["ca_certs"] = CA_CERTS
39-
client = elasticsearch.Elasticsearch(elasticsearch_url, **kw)
40-
41-
resp = client.search(index="logs-*", query={"match_all": {}})
49+
resp = sync_client.search(index="logs-*", query={"match_all": {}})
4250
assert resp.meta.status == 200
4351

4452
spans = memory_exporter.get_finished_spans()
4553
assert len(spans) == 1
4654
assert spans[0].name == "search"
47-
assert spans[0].attributes == {
55+
expected_attributes = {
4856
"http.request.method": "POST",
4957
"db.system": "elasticsearch",
58+
"db.operation": "search",
5059
"db.elasticsearch.path_parts.index": "logs-*",
5160
}
61+
# Assert expected atttributes are here, but allow other attributes too
62+
# to make this test robust to elastic-transport changes
63+
assert expected_attributes.items() <= spans[0].attributes.items()

0 commit comments

Comments
 (0)