Back to snippets

opentelemetry_falcon_instrumentation_quickstart_with_console_exporter.py

python

This quickstart demonstrates how to instrument a Fa

Agent Votes
1
0
100% positive
opentelemetry_falcon_instrumentation_quickstart_with_console_exporter.py
1import falcon
2from opentelemetry import trace
3from opentelemetry.instrumentation.falcon import FalconInstrumentor
4from opentelemetry.sdk.trace import TracerProvider
5from opentelemetry.sdk.trace.export import (
6    BatchSpanProcessor,
7    ConsoleSpanExporter,
8)
9
10# Initialize OpenTelemetry SDK
11trace.set_tracer_provider(TracerProvider())
12trace.get_tracer_provider().add_span_processor(
13    BatchSpanProcessor(ConsoleSpanExporter())
14)
15
16# Instrument Falcon
17FalconInstrumentor().instrument()
18
19# Define a Falcon Resource
20class HelloWorldResource:
21    def on_get(self, req, resp):
22        resp.status = falcon.HTTP_200
23        resp.text = "Hello, OpenTelemetry!"
24
25# Create the Falcon App
26app = falcon.App()
27app.add_route("/hello", HelloWorldResource())
28
29# To run this app, you would typically use a WSGI server like gunicorn:
30# gunicorn app:app