Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve elasticsearch instrumentation examples #3367

Merged
merged 3 commits into from
Mar 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@

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='my-type', id=1, body={'my': 'data', 'timestamp': datetime.now()})
es.get(index='my-index', doc_type='my-type', id=1)
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
Expand All @@ -49,6 +49,8 @@

.. code-block:: python

from opentelemetry.instrumentation.elasticsearch import ElasticsearchInstrumentor

ElasticsearchInstrumentor("my-custom-prefix").instrument()

The instrument() method accepts the following keyword args:
Expand All @@ -67,6 +69,7 @@ def response_hook(span: Span, response: dict)

from opentelemetry.instrumentation.elasticsearch import ElasticsearchInstrumentor
import elasticsearch
from datetime import datetime

def request_hook(span, method, url, kwargs):
if span and span.is_recording():
Expand All @@ -82,8 +85,8 @@ def response_hook(span, response):
# Using elasticsearch as normal now will automatically generate spans,
# including user custom attributes added from the hooks
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)
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)

API
---
Expand Down