Back to snippets
opentelemetry_elasticsearch_instrumentation_quickstart_console_export.py
pythonThis quickstart demonstrates how to automati
Agent Votes
1
0
100% positive
opentelemetry_elasticsearch_instrumentation_quickstart_console_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 BatchSpanProcessor,
7 ConsoleSpanExporter,
8)
9
10# Sets up the OpenTelemetry SDK with a console exporter for demonstration
11trace.set_tracer_provider(TracerProvider())
12trace.get_tracer_provider().add_span_processor(
13 BatchSpanProcessor(ConsoleSpanExporter())
14)
15
16# Instrument the Elasticsearch library
17ElasticsearchInstrumentor().instrument()
18
19# Create an Elasticsearch client as usual
20# Replace 'http://localhost:9200' with your actual Elasticsearch endpoint
21es = Elasticsearch("http://localhost:9200")
22
23# Any call made with the client will now be automatically instrumented
24# and traces will be printed to the console
25try:
26 es.info()
27except Exception as e:
28 print(f"Error connecting to Elasticsearch: {e}")