You can create custom spans using OpenTelemetry’s tracer:
Copy
Ask AI
from opentelemetry import tracefrom opentelemetry.trace import SpanKindtracer = trace.get_tracer("my-tracer")async def my_function(): with tracer.start_as_current_span("my_operation", kind=SpanKind.INTERNAL) as span: try: # Your code here span.set_attribute("result", "success") except Exception as e: span.record_exception(e) span.set_status(trace.Status(trace.StatusCode.ERROR)) raise
Create nested spans to represent complex operations:
Copy
Ask AI
async def complex_operation(): with tracer.start_as_current_span("parent_operation") as parent_span: parent_span.set_attribute("parent_data", "value") with tracer.start_as_current_span("child_operation") as child_span: child_span.set_attribute("child_data", "value") # Child operation code