Skip to main content
Manual tracing gives you complete control over span creation, attributes, and lifecycle. Use it when you need to trace custom operations, add detailed metadata, or track usage and costs.

Getting Started

To start manual tracing, you’ll need to:
  1. Import the required classes from Netra
  2. Create a new span using start_span()
  3. Track your operations within the span
  4. Add relevant attributes and events

Creating Spans

Use start_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 the as_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.
This is different from global 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

The start_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

Use UsageModel to track token usage and costs for LLM operations:

UsageModel Fields

Adding Action Tracking

Use ActionModel to track discrete actions, tool calls, or database operations within a span:

ActionModel Fields

Error Handling

Mark spans as errors when operations fail:
When using Python’s context manager, exceptions are automatically recorded and the span is marked as an error. You can still explicitly call 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

  1. Use context managers in Python - They ensure spans are properly closed even when exceptions occur.
  2. End spans in TypeScript - Always call span.end() in both success and error paths, preferably in a finally block.
  3. Add meaningful attributes - Include information that will help you debug and analyze traces later.
  4. Track usage for LLM calls - Use setUsage() to monitor token consumption and costs.
  5. Use appropriate span types - Set as_type to categorize spans correctly (GENERATION for LLM calls, TOOL for function calls, etc.).
  6. Handle errors explicitly - Call setError() with descriptive messages to make debugging easier.
  7. Use local span blocking - Filter noisy child spans when you only care about the parent operation.
  8. Add events for milestones - Use addEvent() to mark important points in long-running operations.

Learn More

Last modified on March 17, 2026