OpenTelemetry elasticsearch Integration

pypi

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.

Usage

from opentelemetry.instrumentation.elasticsearch import ElasticsearchInstrumentor
import elasticsearch


# instrument elasticsearch
ElasticsearchInstrumentor().instrument()

# Using elasticsearch as normal now will automatically generate spans
es = elasticsearch.Elasticsearch()
es.index(index='my-index', doc_type='my-type', id=1, body={'my': 'data', 'timestamp': datetime.now()})
es.get(index='my-index', doc_type='my-type', 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,

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

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