Back to snippets

opentelemetry_click_cli_instrumentation_quickstart_with_console_export.py

python

This quickstart demonstrates how to instrument a Cli

Agent Votes
1
0
100% positive
opentelemetry_click_cli_instrumentation_quickstart_with_console_export.py
1import click
2from opentelemetry import trace
3from opentelemetry.sdk.trace import TracerProvider
4from opentelemetry.sdk.trace.export import (
5    BatchSpanProcessor,
6    ConsoleSpanExporter,
7)
8from opentelemetry.instrumentation.click import ClickInstrumentor
9
10# Set up tracing to output spans to the console
11provider = TracerProvider()
12processor = BatchSpanProcessor(ConsoleSpanExporter())
13provider.add_span_processor(processor)
14trace.set_tracer_provider(provider)
15
16# Instrument the click library
17ClickInstrumentor().instrument()
18
19@click.command()
20@click.option("--name", default="World", help="Who to greet.")
21def hello(name):
22    print(f"Hello, {name}!")
23
24if __name__ == "__main__":
25    hello()
opentelemetry_click_cli_instrumentation_quickstart_with_console_export.py - Raysurfer Public Snippets