Skip to content

Commit 8785889

Browse files
committed
Add OpenTelemetry end-to-end test
1 parent f98a4d3 commit 8785889

File tree

5 files changed

+88
-11
lines changed

5 files changed

+88
-11
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: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,11 @@
3131
INSTALL_ENV = {"AIOHTTP_NO_EXTENSIONS": "1"}
3232

3333

34-
@nox.session(python=["3.9", "3.10", "3.11", "3.12"])
35-
def test(session):
36-
session.install(".[dev]", env=INSTALL_ENV)
37-
34+
def pytest_argv():
3835
junit_xml = os.path.join(
3936
SOURCE_DIR, "junit", "elasticsearch-serverless-python-junit.xml"
4037
)
41-
pytest_argv = [
38+
return [
4239
"pytest",
4340
"--cov-report=term-missing",
4441
"--cov=elasticsearch_serverless",
@@ -47,9 +44,23 @@ def test(session):
4744
"--log-level=debug",
4845
"-vv",
4946
"--cache-clear",
50-
*(session.posargs),
5147
]
52-
session.run(*pytest_argv)
48+
49+
50+
@nox.session(python=["3.9", "3.10", "3.11", "3.12"])
51+
def test(session):
52+
session.install(".[dev]", env=INSTALL_ENV)
53+
54+
session.run(*pytest_argv(), *(session.posargs))
55+
56+
57+
@nox.session(python=["3.9", "3.12"])
58+
def test_otel(session):
59+
session.install(".[dev]", env=INSTALL_ENV)
60+
session.install("opentelemetry-api", "opentelemetry-sdk")
61+
62+
argv = pytest_argv() + ["-m", "otel"]
63+
session.run(*argv, *(session.posargs), env={"TEST_WITH_OTEL": "1"})
5364

5465

5566
@nox.session()

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ packages = ["elasticsearch_serverless"]
9999

100100
[tool.pytest.ini_options]
101101
junit_family = "legacy"
102-
xfail_strict=true
102+
xfail_strict = true
103+
markers = "otel"
103104

104105
[tool.isort]
105106
profile = "black"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import os
19+
20+
import pytest
21+
22+
try:
23+
from opentelemetry import trace
24+
from opentelemetry.sdk.trace import TracerProvider, export
25+
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
26+
InMemorySpanExporter,
27+
)
28+
except ModuleNotFoundError:
29+
pass
30+
31+
pytestmark = [
32+
pytest.mark.skipif(
33+
"TEST_WITH_OTEL" not in os.environ, reason="TEST_WITH_OTEL is not set"
34+
),
35+
pytest.mark.otel,
36+
]
37+
38+
39+
def test_otel_end_to_end(monkeypatch, sync_client):
40+
# Sets the global default tracer provider
41+
tracer_provider = TracerProvider()
42+
memory_exporter = InMemorySpanExporter()
43+
span_processor = export.SimpleSpanProcessor(memory_exporter)
44+
tracer_provider.add_span_processor(span_processor)
45+
trace.set_tracer_provider(tracer_provider)
46+
47+
resp = sync_client.search(index="logs-*", query={"match_all": {}})
48+
assert resp.meta.status == 200
49+
50+
spans = memory_exporter.get_finished_spans()
51+
assert len(spans) == 1
52+
assert spans[0].name == "search"
53+
expected_attributes = {
54+
"http.request.method": "POST",
55+
"db.system": "elasticsearch",
56+
"db.operation": "search",
57+
"db.elasticsearch.path_parts.index": "logs-*",
58+
}
59+
# Assert expected atttributes are here, but allow other attributes too
60+
# to make this test robust to elastic-transport changes
61+
assert expected_attributes.items() <= spans[0].attributes.items()

0 commit comments

Comments
 (0)