Elasticsearch
See every request your application makes to Elasticsearch, including its operation, duration, outcome, and target, as a span (one unit of work: a single operation, with a name, a start, and a duration) in Logfire.
Logfire is built on OpenTelemetry (OTel), the open industry standard for collecting traces,
metrics, and logs. From version 8.15, the Elasticsearch Python client uses OTel directly to create
spans through the active global tracer provider. You do not need a Logfire wrapper or the older
opentelemetry-instrumentation-elasticsearch package.
You’ll need a Logfire project. Open Add data in your project (top navigation) and follow the
setup for your language: it signs your machine in with logfire auth (a browser sign-in, no token
to copy) and, for production or other languages, creates a write token (the credential your app
uses to send data). New to Logfire? Start with Getting Started.
Install Logfire and Elasticsearch 8.15 or later:
pip install 'logfire' 'elasticsearch>=8.15'
Call logfire.configure() before making an Elasticsearch request. The client looks up the active
OTel tracer provider when it starts a request, so you can create the client before or after this call.
Requests made before configuration are not sent to Logfire, but requests made after configuration
are traced normally.
from elasticsearch import Elasticsearch
import logfire
client = Elasticsearch('http://localhost:9200')
logfire.configure()
client.search(index='products', query={'match': {'name': 'coffee'}})
By default, the client does not attach the search query body to spans because it can contain sensitive or personally identifiable information (PII). To record query bodies, set this environment variable before starting your application:
export OTEL_PYTHON_INSTRUMENTATION_ELASTICSEARCH_CAPTURE_SEARCH_QUERY=raw
The Elasticsearch client creates spans by default whenever an OTel tracer provider is active. To turn off its native tracing, set this environment variable before creating the client:
export OTEL_PYTHON_INSTRUMENTATION_ELASTICSEARCH_ENABLED=false
- No Elasticsearch spans appear: check that you use
elasticsearch8.15 or later, calllogfire.configure()before the request you want to trace, and have not setOTEL_PYTHON_INSTRUMENTATION_ELASTICSEARCH_ENABLED=false. - The query body is missing: restart the application after setting
OTEL_PYTHON_INSTRUMENTATION_ELASTICSEARCH_CAPTURE_SEARCH_QUERY=raw. Query capture is disabled by default. - Duplicate spans appear: remove
opentelemetry-instrumentation-elasticsearchand its setup. The client already creates spans natively.