- How to add OpenTelemetry packages to a Python project
- How to configure a TracerProvider and ConsoleSpanExporter
- How to create spans that represent operations and nested child operations
- Where to go next to export traces to a backend (OTLP, Jaeger, Zipkin)
- Python 3.7+
- pip
process_payment()starts the flow.validate_card()simulates credit-card validation.charge_bank()simulates charging the user’s bank.
OpenTelemetry is split into API and SDK: the API provides the interfaces you call from application code, while the SDK provides concrete implementations (exporters, processors, and providers) that produce and export telemetry.
ConsoleSpanExporter. The ConsoleSpanExporter prints spans to stdout — this is useful for local development and debugging.
configure_tracer() to create spans. The recommended pattern is to use the context manager start_as_current_span so spans are properly nested and automatically ended.
Complete instrumented example:
ConsoleSpanExporter, you will also see formatted span output printed to the console. A simplified example of what ConsoleSpanExporter might print (exact formatting may vary by OpenTelemetry version):
ConsoleSpanExporter with an exporter for your backend. Common choices:
| Exporter | Use case | Docs |
|---|---|---|
| OTLP | Send traces to the OpenTelemetry Collector or supported backends | https://opentelemetry.io/docs/instrumentation/python/exporters/otlp/ |
| Jaeger | Send traces directly to a Jaeger backend | https://www.jaegertracing.io/ |
| Zipkin | Send traces directly to Zipkin | https://zipkin.io/ |
- Add meaningful span names, attributes, and events to capture the context that helps debugging (e.g., masked card identifier, user id, HTTP status).
- Use automatic instrumentation libraries for frameworks and popular libraries when available; complement them with manual spans for business logic.
- Keep spans short-lived and focused on logical units of work.
- Avoid logging sensitive data to spans or attributes. Use masking or hashing when necessary.
Do not store unmasked sensitive data (full card numbers, personal identifiers, secrets) in span attributes. Use masking or tokenization to protect user data in telemetry.
- OpenTelemetry: https://opentelemetry.io/
- OpenTelemetry Python instrumentation: https://opentelemetry.io/docs/instrumentation/python/
- OTLP exporter docs: https://opentelemetry.io/docs/instrumentation/python/exporters/otlp/
- OpenTelemetry Collector: https://opentelemetry.io/docs/collector/
- Jaeger: https://www.jaegertracing.io/
- Zipkin: https://zipkin.io/