Getting Started
To start manual tracing, you’ll need to:- Import the required classes from Netra
- Create a new span using
start_span() - Track your operations within the span
- Add relevant attributes and events
Creating Spans
Usestart_span() to create a span that wraps a block of code. In Python, use it as a context manager. In TypeScript, explicitly call end() when done.
Span Parameters
Span Types
Use theas_type parameter to categorize spans. This helps Netra display them correctly and enables type-specific features.
See Spans for detailed guidance on when to use each type.
Local Span Blocking
You can block specific spans locally within a particular span scope. This is useful when you want to filter out noisy child spans (like HTTP requests) within a specific operation.blocked_spans in Netra.init() which blocks spans across the entire application. Local blocking only affects spans created within the specific parent span’s scope.
SpanWrapper Methods
Thestart_span() function returns a SpanWrapper object with methods for adding context to your spans.
Setting Span Attributes
Add custom key-value pairs to provide context about the operation:Renaming a Span
Sometimes a span has to start before you know its readable name. For example, an agent span opens under a ticket ID, and the department that handles the ticket is only decided inside the span. Callupdate_span_name() once the name is known:
Python
SpanType.AGENT and SpanType.TOOL spans, the rename also updates the span’s netra.agent.name or netra.task.name attribute. Child spans started after the rename inherit the new name, including those in asyncio tasks and worker threads that share the span’s context. Child spans that already started keep the old name, because entity names are stamped when a span starts.
LLM-Specific Attributes
For LLM operations, use dedicated methods to set prompts, models, and system information:Recording Events
Track significant moments within a span’s lifecycle:Tracking Usage Data
UseUsageModel to track token usage and costs for LLM operations:
UsageModel Fields
Adding Action Tracking
UseActionModel to track discrete actions, tool calls, or database operations within a span:
ActionModel Fields
Error Handling
Mark spans as errors when operations fail:set_error() for custom error messages.
Nested Spans
Create hierarchical traces by nesting spans. Child spans automatically inherit the parent context:Accessing the Current Span
Get the currently active span to add attributes from anywhere in your code:Example: RAG Pipeline
This example demonstrates nested spans with multiple span types - a common pattern for AI pipelines.Best Practices
- Use context managers in Python - They ensure spans are properly closed even when exceptions occur.
- End spans in TypeScript - Always call
span.end()in both success and error paths, preferably in afinallyblock. - Add meaningful attributes - Include information that will help you debug and analyze traces later.
- Track usage for LLM calls - Use
setUsage()to monitor token consumption and costs. - Use appropriate span types - Set
as_typeto categorize spans correctly (GENERATION for LLM calls, TOOL for function calls, etc.). - Handle errors explicitly - Call
setError()with descriptive messages to make debugging easier. - Use local span blocking - Filter noisy child spans when you only care about the parent operation.
- Add events for milestones - Use
addEvent()to mark important points in long-running operations.
Learn More
- Decorators - Simpler instrumentation with decorators
- Auto Instrumentation - Zero-code tracing
- Initialization - Configure tracing behavior
