OpenTelemetry elasticsearch Integration
This library allows tracing elasticsearch made by the elasticsearch library.
Installation
pip install opentelemetry-instrumentation-elasticsearch
References
This library allows tracing HTTP elasticsearch made by the elasticsearch library.
Warning
The elasticsearch package got native OpenTelemetry support since version 8.13. To avoid duplicated tracing this instrumentation disables itself if it finds an elasticsearch client that has OpenTelemetry support enabled.
Please be aware that the two libraries may use a different semantic convention, see elasticsearch documentation.
Usage
from opentelemetry.instrumentation.elasticsearch import ElasticsearchInstrumentor
import elasticsearch
from datetime import datetime
# instrument elasticsearch
ElasticsearchInstrumentor().instrument()
# Using elasticsearch as normal now will automatically generate spans
es = elasticsearch.Elasticsearch()
es.index(index='my-index', doc_type='_doc', id=1, body={'my': 'data', 'timestamp': datetime.now()})
es.get(index='my-index', doc_type='_doc', id=1)
Elasticsearch instrumentation prefixes operation names with the string “Elasticsearch”. This can be changed to a different string by either setting the OTEL_PYTHON_ELASTICSEARCH_NAME_PREFIX environment variable or by passing the prefix as an argument to the instrumentor. For example,
from opentelemetry.instrumentation.elasticsearch import ElasticsearchInstrumentor
ElasticsearchInstrumentor("my-custom-prefix").instrument()
The instrument() method accepts the following keyword args: tracer_provider (TracerProvider) - an optional tracer provider request_hook (Callable) - a function with extra user-defined logic to be performed before performing the request this function signature is: def request_hook(span: Span, method: str, url: str, kwargs)
response_hook (Callable) - a function with extra user-defined logic to be performed after performing the request this function signature is: def response_hook(span: Span, response: dict)
for example:
API
- class opentelemetry.instrumentation.elasticsearch.ElasticsearchInstrumentor(*args, **kwargs)[source]
Bases:
BaseInstrumentorAn instrumentor for elasticsearch See BaseInstrumentor
- instrumentation_dependencies()[source]
Return a list of python packages with versions that the will be instrumented.
The format should be the same as used in requirements.txt or pyproject.toml.
For example, if an instrumentation instruments requests 1.x, this method should look like: :rtype:
Collection[str]- def instrumentation_dependencies(self) -> Collection[str]:
return [‘requests ~= 1.0’]
This will ensure that the instrumentation will only be used when the specified library is present in the environment.