OpenTelemetry Span Attribute Not Visible Fix
In this tutorial, you'll learn about OpenTelemetry Span Attribute Not Visible Fix. We cover key concepts, practical examples, and best practices.
Your OpenTelemetry span attributes do not appear in Jaeger or Grafana Tempo — the span exists but has no custom data. Attributes are set on the wrong span, set after the span ends, or use invalid value types.
The Problem
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def process_order(order_id):
with tracer.start_as_current_span("process-order") as span:
# Do some work
pass
# WRONG: attribute set after span ends
span.set_attribute("order.id", order_id)
The attribute is set outside the with block, after the span has already ended. Attributes must be set while the span is active.
Step-by-Step Fix
1. Set attributes inside the span context
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def process_order(order_id):
with tracer.start_as_current_span("process-order") as span:
span.set_attribute("order.id", order_id)
span.set_attribute("order.type", "standard")
# Do work while span is active
2. Use semantic convention attribute names
from opentelemetry import trace
from opentelemetry.semconv.trace import SpanAttributes
tracer = trace.get_tracer(__name__)
def handle_http_request(method, path, status_code):
with tracer.start_as_current_span("http-request") as span:
span.set_attribute(SpanAttributes.HTTP_METHOD, method)
span.set_attribute(SpanAttributes.HTTP_TARGET, path)
span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
3. Set attributes at span creation
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def process_payment(amount, currency):
with tracer.start_as_current_span(
"process-payment",
attributes={
"payment.amount": amount,
"payment.currency": currency,
},
) as span:
# Work happens here — attributes already set
pass
4. Use arrays and complex attribute values
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def process_items(items):
with tracer.start_as_current_span("process-items") as span:
# Array attribute
span.set_attribute("item.ids", [item.id for item in items])
# Boolean attribute
span.set_attribute("process.urgent", True)
# Integer attribute
span.set_attribute("item.count", len(items))
# Float attribute
span.set_attribute("total.value", sum(item.price for item in items))
5. Add attributes with manual span management
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def long_running_task(task_id):
span = tracer.start_span("long-task")
span.set_attribute("task.id", task_id)
try:
# Long-running work
span.set_attribute("task.status", "running")
except Exception as e:
span.set_attribute("task.status", "failed")
span.set_attribute("task.error", str(e))
span.record_exception(e)
raise
finally:
span.end()
Expected output in tracing backend:
process-order
Attributes:
order.id: "ord-12345"
order.type: "standard"
Duration: 234ms
Prevention Tips
- Set attributes inside the
withblock, never after it ends - Use OpenTelemetry semantic conventions for consistent attribute naming
- Set critical attributes at span creation time via
start_as_current_span(attributes=...) - Use typed values (string, int, float, bool, array) — avoid nested objects
- Add attributes before any operation that might throw
Common Mistakes with span attribute
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
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