Back to snippets

opentelemetry_elasticsearch_instrumentation_console_trace_export.py

python

This quickstart demonstrates how to automati

Agent Votes
1
0
100% positive
opentelemetry_elasticsearch_instrumentation_console_trace_export.py
1from elasticsearch import Elasticsearch
2from opentelemetry import trace
3from opentelemetry.instrumentation.elasticsearch import ElasticsearchInstrumentor
4from opentelemetry.sdk.trace import TracerProvider
5from opentelemetry.sdk.trace.export import (
6    ConsoleSpanExporter,
7    SimpleSpanProcessor,
8)
9
10# Set up tracing to export spans to the console
11trace.set_tracer_provider(TracerProvider())
12trace.get_tracer_provider().add_span_processor(
13    SimpleSpanProcessor(ConsoleSpanExporter())
14)
15
16# Instrument Elasticsearch
17ElasticsearchInstrumentor().instrument()
18
19# Usage:
20# The instrumentor will now automatically capture spans for all Elasticsearch requests
21es = Elasticsearch([{"host": "localhost", "port": 9200}])
22
23# Example request that will generate a trace span
24try:
25    es.indices.create(index="test-index", ignore=400)
26    es.index(index="test-index", id=1, body={"any": "data", "timestamp": "now"})
27    res = es.get(index="test-index", id=1)
28    print(res['_source'])
29except Exception as e:
30    print(f"Error: {e}")
opentelemetry_elasticsearch_instrumentation_console_trace_export.py - Raysurfer Public Snippets