|
| 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