OpenTelemetry Span Event Not Recording Fix
In this tutorial, you'll learn about OpenTelemetry Span Event Not Recording Fix. We cover key concepts, practical examples, and best practices.
Your OpenTelemetry span events do not appear in the tracing backend — span.add_event() calls produce no visible output, or the events lack timestamps and attributes. Events must be added to active spans and exported correctly.
The Problem
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def process_file(filename):
with tracer.start_as_current_span("process-file") as span:
# WRONG: event name as keyword argument
span.add_event(name="file.opened")
The event is created but may not appear in the backend because name is a positional parameter, not a keyword argument. Also, events without attributes provide limited debugging value.
Step-by-Step Fix
1. Add events with attributes
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def process_file(filename):
with tracer.start_as_current_span("process-file") as span:
span.add_event("file.opened", {
"filename": filename,
"size_bytes": 1024,
})
# Process file...
span.add_event("file.processed", {
"filename": filename,
"records": 500,
})
2. Record exceptions as events
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def process_data(data):
with tracer.start_as_current_span("process-data") as span:
try:
result = transform(data)
span.set_attribute("data.valid", True)
except ValueError as e:
span.set_attribute("data.valid", False)
span.record_exception(e, {
"data.size": len(data),
"data.preview": str(data)[:100],
})
3. Use semantic convention event names
from opentelemetry import trace
from opentelemetry.semconv.trace import SpanAttributes
tracer = trace.get_tracer(__name__)
def cache_lookup(key):
with tracer.start_as_current_span("cache-lookup") as span:
span.add_event("cache.miss", {
"cache.key": key,
})
# Fetch from database...
span.add_event("db.query", {
"db.system": "postgresql",
"db.statement": "SELECT * FROM users WHERE id = ?",
})
4. Add timed events
import time
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def deploy_service():
with tracer.start_as_current_span("deploy") as span:
span.add_event("deploy.started")
time.sleep(2)
span.add_event("deploy.built", {
"image": "myapp:latest",
"size_mb": 245,
})
time.sleep(3)
span.add_event("deploy.deployed", {
"environment": "staging",
"replicas": 3,
})
time.sleep(1)
span.add_event("deploy.completed")
5. Query events with TraceQL
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def payment_flow():
with tracer.start_as_current_span("payment-flow") as span:
span.add_event("payment.authorized", {
"amount": 99.99,
"currency": "USD",
})
span.add_event("payment.captured", {
"transaction_id": "txn-123",
})
Expected output:
process-file
Events:
file.opened @ 10:00:00.000
filename: "data.csv"
size_bytes: 1024
file.processed @ 10:00:00.234
filename: "data.csv"
records: 500
Prevention Tips
- Always add attributes to events for debugging value
- Use
span.record_exception(e)instead of manual exception events - Use semantic convention names for consistency
- Add events at meaningful points in the operation lifecycle
- Keep event names concise and namespaced (e.g.,
cache.miss,db.query)
Common Mistakes with span event
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
These mistakes appear frequently in real-world OPENTELEMETRY code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro