Registry /
observability / opentelemetry-instrumentation-elasticsearch
Install & Compatibility
Where this runs
tested against v0.64b0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 1.436s · 66.1MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 6.4s · import 1.344s · 65MB
65MB installed
● package 65MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
ElasticsearchInstrumentor
✓ from opentelemetry.instrumentation.elasticsearch import ElasticsearchInstrumentor
TracerProvider
✓ from opentelemetry.sdk.trace import TracerProvider
✗ from opentelemetry.trace import TracerProvider
TracerProvider is an SDK concept; the API provides `trace.get_tracer_provider()` but not `TracerProvider` directly for instantiation.
set_global_tracer_provider
✓ from opentelemetry import trace; trace.set_global_tracer_provider(...)
This quickstart configures a basic OpenTelemetry SDK with a `ConsoleSpanExporter` to print traces to the console. It then instruments the Elasticsearch client and attempts a `ping()` and `index()` operation. Spans representing these operations will be generated and printed to stdout. It includes error handling for cases where an Elasticsearch instance is not running.
import os
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.instrumentation.elasticsearch import ElasticsearchInstrumentor
from elasticsearch import Elasticsearch, ConnectionError # Import ConnectionError for graceful handling
# 1. Configure OpenTelemetry Tracer Provider
# For demonstration, use ConsoleSpanExporter to print traces to stdout
resource = Resource.create({"service.name": "my-elasticsearch-app"})
tracer_provider = TracerProvider(resource=resource)
span_processor = SimpleSpanProcessor(ConsoleSpanExporter())
tracer_provider.add_span_processor(span_processor)
trace.set_global_tracer_provider(tracer_provider)
# 2. Instrument Elasticsearch
ElasticsearchInstrumentor().instrument()
# 3. Use the Elasticsearch client
# Ensure an Elasticsearch instance is running at this address.
# For a quick runnable example without a real ES instance, we'll catch ConnectionError.
try:
# Replace with your Elasticsearch host if different. This will attempt connection.
client = Elasticsearch("http://localhost:9200")
print("Attempting to ping Elasticsearch...")
if client.ping():
print("Elasticsearch connection successful! Spans should be visible.")
else:
print("Could not ping Elasticsearch. Check its status.")
# Example of an index operation that will also be traced
print("Indexing a document...")
client.index(index="my-test-index", id=1, document={"message": "hello opentelemetry"})
print("Document indexed. Check for additional spans.")
except ConnectionError as e:
print(f"Could not connect to Elasticsearch: {e}")
print("Spans for the connection attempt should still be generated, even if it failed.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
finally:
# Ensure all spans are exported before exiting
tracer_provider.shutdown()
Upgrade
Version history
0.64b0latest on PyPI · released Jun 24, 2026
Audit
Dependencies
elasticsearchrequiredThe library being instrumented by opentelemetry-instrumentation-elasticsearch.
opentelemetry-sdkrequiredRequired for OpenTelemetry SDK functionalities, including TracerProvider and SpanProcessors.
opentelemetry-exporter-otlpoptionalA common exporter for sending telemetry data to an OTLP collector. Other exporters can be used.