Skip to content

[Backport 8.13] Add OpenTelemetry end-to-end test #2469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ nox
numpy
pandas

opentelemetry-api
opentelemetry-sdk

# Testing the 'search_mvt' API response
mapbox-vector-tile
# Python 3.7 gets an old version of mapbox-vector-tile, requiring an
Expand Down
51 changes: 51 additions & 0 deletions test_elasticsearch/test_server/test_otel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider, export
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter

import elasticsearch
from test_elasticsearch.utils import CA_CERTS


def test_otel_end_to_end(monkeypatch, elasticsearch_url: str):
# Sets the global default tracer provider
tracer_provider = TracerProvider()
memory_exporter = InMemorySpanExporter()
span_processor = export.SimpleSpanProcessor(memory_exporter)
tracer_provider.add_span_processor(span_processor)
trace.set_tracer_provider(tracer_provider)

# Once OpenTelemetry is enabled by default, we can use the sync_client fixture instead
monkeypatch.setenv("OTEL_PYTHON_INSTRUMENTATION_ELASTICSEARCH_ENABLED", "true")
kw = {}
if elasticsearch_url.startswith("https://"):
kw["ca_certs"] = CA_CERTS
client = elasticsearch.Elasticsearch(elasticsearch_url, **kw)

resp = client.search(index="logs-*", query={"match_all": {}})
assert resp.meta.status == 200

spans = memory_exporter.get_finished_spans()
assert len(spans) == 1
assert spans[0].name == "search"
assert spans[0].attributes == {
"http.request.method": "POST",
"db.system": "elasticsearch",
"db.elasticsearch.path_parts.index": "logs-*",
}