fromopentelemetryimporttracefromopentelemetry.sdk.traceimportTracerProviderfromopentelemetry.sdk.resourcesimportResourcefromopentelemetry.sdk.trace.exportimport(BatchSpanProcessor,ConsoleSpanExporter,)# Creates a resource and adds it to the tracer providerresource=Resource.create({"service.name":"hello-world-console"})provider=TracerProvider(resource=resource)trace.set_tracer_provider(provider)# Adds span processor with the OTLP exporter to the tracer providerprovider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))tracer=trace.get_tracer(__name__)# Starts and sets an attribute to a spanwithtracer.start_as_current_span("HelloWorldSpan")asspan:span.set_attribute("foo","bar")print("Hello world Example")
Run this Python snippet
uv run https://emdneto.github.io/opentelemetry-by-example/python/hello-world/snippet_console.py
fromopentelemetryimporttracefromopentelemetry.sdk.traceimportTracerProviderfromopentelemetry.sdk.resourcesimportResourcefromopentelemetry.sdk.trace.exportimportBatchSpanProcessorfromopentelemetry.exporter.otlp.proto.grpc.trace_exporterimport(OTLPSpanExporter,)# Creates a resource and adds it to the tracer providerresource=Resource.create({"service.name":"hello-world-otlp-grpc"})provider=TracerProvider(resource=resource)trace.set_tracer_provider(provider)# Adds span processor with the OTLP exporter to the tracer providerprovider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4317",insecure=True)))tracer=trace.get_tracer(__name__,attributes={"domain":"foo"})# Starts and sets an attribute to a spanwithtracer.start_as_current_span("HelloWorldSpanGrpc")asspan:span.set_attribute("foo","grpc")span.add_event("event in span")print("Hello world")
Run this Python snippet
uv run https://emdneto.github.io/opentelemetry-by-example/python/hello-world/snippet_grpc.py
fromopentelemetryimporttracefromopentelemetry.sdk.traceimportTracerProviderfromopentelemetry.sdk.resourcesimportResourcefromopentelemetry.sdk.trace.exportimportBatchSpanProcessorfromopentelemetry.exporter.otlp.proto.http.trace_exporterimport(OTLPSpanExporter,)# Creates a resource and adds it to the tracer providerresource=Resource.create({"service.name":"hello-world-otlp-http"})provider=TracerProvider(resource=resource)trace.set_tracer_provider(provider)# Adds span processor with the OTLP exporter to the tracer providerprovider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces")))tracer=trace.get_tracer(__name__,attributes={"domain":"foo"})# Starts and sets an attribute to a spanwithtracer.start_as_current_span("HelloWorldSpanHttp")asspan:span.set_attribute("foo","http")span.add_event("event in span")print("Hello world")
Run this Python snippet
uv run https://emdneto.github.io/opentelemetry-by-example/python/hello-world/snippet_http.py
fromopentelemetryimportmetricsfromopentelemetry.sdk.metricsimportMeterProviderfromopentelemetry.sdk.resourcesimportResourcefromopentelemetry.sdk.metrics.exportimport(ConsoleMetricExporter,PeriodicExportingMetricReader,)# Creates a resource and adds it to the meter providerresource=Resource.create({"service.name":"hello-world-metrics-console"})metric_reader=PeriodicExportingMetricReader(exporter=ConsoleMetricExporter(),export_interval_millis=1000,# Export every 1 second for demo)provider=MeterProvider(resource=resource,metric_readers=[metric_reader])metrics.set_meter_provider(provider)meter=metrics.get_meter(__name__)# Create different metric instrumentsrequest_counter=meter.create_counter(name="hello_requests_total",description="Total number of hello requests",unit="1",)response_time_histogram=meter.create_histogram(name="hello_request_duration_seconds",description="Hello request duration in seconds",unit="s",)active_users_gauge=meter.create_up_down_counter(name="hello_active_users",description="Number of active users",unit="1",)request_counter.add(1,{"method":"GET","endpoint":"/hello"})response_time_histogram.record(0.1,{"method":"GET","endpoint":"/hello"})active_users_gauge.add(1,{"region":"us-west"})
Run this Python snippet
uv run https://emdneto.github.io/opentelemetry-by-example/python/hello-world/snippet_metrics_console.py
fromopentelemetryimportmetricsfromopentelemetry.sdk.metricsimportMeterProviderfromopentelemetry.sdk.resourcesimportResourcefromopentelemetry.sdk.metrics.exportimportPeriodicExportingMetricReaderfromopentelemetry.exporter.otlp.proto.grpc.metric_exporterimport(OTLPMetricExporter,)# Creates a resource and adds it to the meter providerresource=Resource.create({"service.name":"hello-world-otlp-grpc"})metric_reader=PeriodicExportingMetricReader(exporter=OTLPMetricExporter(endpoint="http://localhost:4317",insecure=True),export_interval_millis=1000,# Export every 1 second for demo)provider=MeterProvider(resource=resource,metric_readers=[metric_reader])metrics.set_meter_provider(provider)meter=metrics.get_meter(__name__)# Create different metric instrumentsrequest_counter=meter.create_counter(name="hello_requests_total",description="Total number of hello requests",unit="1",)response_time_histogram=meter.create_histogram(name="hello_request_duration_seconds",description="Hello request duration in seconds",unit="s",)active_users_gauge=meter.create_up_down_counter(name="hello_active_users",description="Number of active users",unit="1",)request_counter.add(1,{"method":"POST","endpoint":"/hello","transport":"grpc"})response_time_histogram.record(0.1,{"method":"POST","endpoint":"/hello","transport":"grpc"})active_users_gauge.add(1,{"region":"us-west","transport":"grpc"})
Run this Python snippet
uv run https://emdneto.github.io/opentelemetry-by-example/python/hello-world/snippet_metrics_grpc.py
fromopentelemetryimportmetricsfromopentelemetry.sdk.metricsimportMeterProviderfromopentelemetry.sdk.resourcesimportResourcefromopentelemetry.sdk.metrics.exportimportPeriodicExportingMetricReaderfromopentelemetry.exporter.otlp.proto.http.metric_exporterimport(OTLPMetricExporter,)# Creates a resource and adds it to the meter providerresource=Resource.create({"service.name":"hello-world-otlp-http"})metric_reader=PeriodicExportingMetricReader(exporter=OTLPMetricExporter(endpoint="http://localhost:4318/v1/metrics"),export_interval_millis=1000,# Export every 1 second for demo)provider=MeterProvider(resource=resource,metric_readers=[metric_reader])metrics.set_meter_provider(provider)meter=metrics.get_meter(__name__)# Create different metric instrumentsrequest_counter=meter.create_counter(name="hello_requests_total",description="Total number of hello requests",unit="1",)response_time_histogram=meter.create_histogram(name="hello_request_duration_seconds",description="Hello request duration in seconds",unit="s",)active_users_gauge=meter.create_up_down_counter(name="hello_active_users",description="Number of active users",unit="1",)request_counter.add(1,{"method":"POST","endpoint":"/hello","transport":"http"})response_time_histogram.record(0.1,{"method":"POST","endpoint":"/hello","transport":"http"})active_users_gauge.add(1,{"region":"us-west","transport":"http"})
Run this Python snippet
uv run https://emdneto.github.io/opentelemetry-by-example/python/hello-world/snippet_metrics_http.py