Improve elasticsearch instrumentation examples (#3367)

* Improve elasticsearch instrumentation examples

* Fix doc_type for elasticsearch instrumentation examples
This commit is contained in:
Andre Murbach Maidl 2025-03-21 12:15:31 -03:00 committed by GitHub
parent db617eb3fd
commit 164259e149
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 5 deletions

View File

@ -32,15 +32,15 @@ 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='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
@ -49,6 +49,8 @@ environment variable or by passing the prefix as an argument to the instrumentor
.. code-block:: python
from opentelemetry.instrumentation.elasticsearch import ElasticsearchInstrumentor
ElasticsearchInstrumentor("my-custom-prefix").instrument()
The instrument() method accepts the following keyword args:
@ -67,6 +69,7 @@ for example:
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():
@ -82,8 +85,8 @@ for example:
# 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
---