From cbee5935bf4e354a79dbdf9c607f65f04b25493b Mon Sep 17 00:00:00 2001 From: Quentin Pradet Date: Thu, 7 Mar 2024 20:46:46 +0100 Subject: [PATCH] Add OpenTelemetry end-to-end test (#2466) (cherry picked from commit f02b7faf5b100baa90ce95e170a67abf8428fa0b) --- dev-requirements.txt | 3 ++ test_elasticsearch/test_server/test_otel.py | 51 +++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 test_elasticsearch/test_server/test_otel.py diff --git a/dev-requirements.txt b/dev-requirements.txt index b5b92c283..0167b2e1d 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -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 diff --git a/test_elasticsearch/test_server/test_otel.py b/test_elasticsearch/test_server/test_otel.py new file mode 100644 index 000000000..0ae7f8721 --- /dev/null +++ b/test_elasticsearch/test_server/test_otel.py @@ -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-*", + }