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
| Parameter | Type | Description |
|---|---|---|
name | string | Name of the span (required) |
attributes | dict/object | Initial attributes to set on the span |
module_name / moduleName | string | Module or component name for organization |
as_type / asType | SpanType | Type of span (SPAN, GENERATION, TOOL, etc.) |
Span Types
Use theas_type parameter to categorize spans. This helps Netra display them correctly and enables type-specific features.
| Type | Use For |
|---|---|
SpanType.GENERATION | LLM completions, image generation |
SpanType.EMBEDDING | Vector embedding operations |
SpanType.TOOL | Function calls, API requests, DB queries |
SpanType.AGENT | AI agent reasoning and decisions |
SpanType.SPAN | General operations (default) |
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: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
| Field | Type | Description |
|---|---|---|
model | string | Model name used |
cost_in_usd / costInUsd | float | Calculated cost in USD |
usage_type / usageType | string | Type of usage (e.g., “chat”, “image_generation”) |
units_used / unitsUsed | int | Number of units consumed |
Adding Action Tracking
UseActionModel to track discrete actions, tool calls, or database operations within a span:
ActionModel Fields
| Field | Type | Description |
|---|---|---|
action | string | Action category (e.g., “DB”, “API”, “CACHE”) |
action_type / actionType | string | Action subtype (e.g., “INSERT”, “SELECT”, “CALL”) |
affected_records / affectedRecords | array | List of affected records with record_id and record_type |
metadata | dict/object | Additional metadata as key-value pairs |
success | boolean | Whether the action succeeded |
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