- How to configure a tracer with an OTLP exporter
- How to add span attributes and events
- How to inject trace context into outgoing HTTP requests
- How to record events on a server span (Flask example)
Avoid recording sensitive data (such as full account numbers, passwords, or PII) in span attributes or events. When needed, prefer identifiers, truncated values, or hashes.
Tracer setup (OTLP HTTP exporter)
Below is a minimal tracer configuration that exports traces to an OTLP HTTP endpoint. Adjustendpoint, headers, and resource attributes to match your environment.
Payment client: add attributes, events, and inject context
This client example demonstrates:- creating a span for the payment flow,
- adding attributes to make spans searchable,
- adding timestamped events before and after an HTTP call,
- injecting trace context into outgoing request headers so downstream services can correlate traces.
- “Sending request” (includes
http.methodandhttp.url) - “Request sent” (includes
http.status_codeandhttp.url)
Server-side (Flask) example: record an error event on the server span
On the server side you want to annotate the server span with request metadata and record events for application-level conditions such as errors, retries, or business rule violations.available_balance attribute. This makes it easier to correlate failures with the originating trace.
Expected combined console output (client + server example):
Quick reference: span attributes vs events
Use attributes to add searchable, indexed metadata to a span. Use events to attach timestamped, ordered observations within the span.| Use | API | Example |
|---|---|---|
| Add a single attribute | span.set_attribute(key, value) | span.set_attribute("http.status_code", resp.status_code) |
| Add multiple attributes | span.set_attributes(dict) | span.set_attributes({ "user": "john", "bank": "BOA" }) |
| Add a timestamped event | span.add_event(name, attributes=None) | span.add_event("Sending request", {"http.url": url}) |
Best practices
- Add events for meaningful, timestamped occurrences (request sent/received, error, retry).
- Use attributes for metadata you want to filter or search on (user ID, status code, service name).
- Inject trace context into outgoing HTTP requests so downstream services can join traces.
- Avoid recording sensitive or excessive data in attributes/events. Use truncated values, hashes, or identifiers where needed.
When instrumenting production systems, prefer structured keys for attributes and events (for example
error.type, http.method, http.status_code) to make querying and visualization easier in your tracing backend.Links and references
Summary
- Use
span.set_attribute/span.set_attributesto store searchable metadata on spans. - Use
span.add_event(name, attributes=None)to record timestamped events inside a span (useful for “request sent/received”, errors, retries). - Inject context for outgoing HTTP calls so downstream services can correlate traces.
- Avoid storing sensitive or excessive data in span attributes and events.