Skip to content

Commit f5f5fd4

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

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ dev = [
6969
"numpy",
7070
"pandas",
7171
"mapbox-vector-tile",
72+
"opentelemetry-api",
73+
"opentelemetry-sdk",
7274
]
7375
docs = [
7476
"sphinx-rtd-theme>=1.2.2",
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
from opentelemetry import trace
19+
from opentelemetry.sdk.trace import TracerProvider, export
20+
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
21+
22+
import elasticsearch
23+
from test_elasticsearch.utils import CA_CERTS
24+
25+
26+
def test_otel_end_to_end(monkeypatch, elasticsearch_url: str):
27+
# Sets the global default tracer provider
28+
tracer_provider = TracerProvider()
29+
memory_exporter = InMemorySpanExporter()
30+
span_processor = export.SimpleSpanProcessor(memory_exporter)
31+
tracer_provider.add_span_processor(span_processor)
32+
trace.set_tracer_provider(tracer_provider)
33+
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": {}})
42+
assert resp.meta.status == 200
43+
44+
spans = memory_exporter.get_finished_spans()
45+
assert len(spans) == 1
46+
assert spans[0].name == "search"
47+
assert spans[0].attributes == {
48+
"http.request.method": "POST",
49+
"db.system": "elasticsearch",
50+
"db.elasticsearch.path_parts.index": "logs-*",
51+
}

0 commit comments

Comments
 (0)