View on npm
netra-sdk on npm registry
View on Yarn
netra-sdk on Yarn registry
Installation
Netra Class
TheNetra class is the main entry point for all SDK operations. All methods are static and can be called directly on the class.
Initialization & Lifecycle
init
Initialize the Netra SDK with configuration options. This method is async and waits for all instrumentations to be ready before returning. Call this once at the start of your application, before importing other libraries for best results.Always
await the init() call to ensure all instrumentations (like OpenAI, Anthropic, LangGraph) are fully patched before your application starts using them.
Returns:
Promise<void>
Tracing
startSpan
Create a new span for manual tracing. Returns aSpanWrapper that must be explicitly ended with end().
Returns:
SpanWrapper
startActiveSpan
Create a span that is automatically set as the active span in the current context for the duration of the callback. Child spans created inside the callback will be parented under this span. The span is ended automatically when the callback completes or throws.
Returns:
T (the return value of the callback)
If the callback returns a
Promise, the span is ended when the promise settles. If it throws, the span is marked as errored and ended before re-throwing.getTraceId
Get the trace ID of the currently active span. Returnsundefined if there is no active span.
string | undefined
runWithRootSpan
Run a function with the root span as the active parent context. All spans created within the function will be children of the root span. RequiresenableRootSpan: true in the init config.
This is necessary in JavaScript because OpenTelemetry JS has no persistent
context.attach(). Without this wrapper, spans created outside a callback-based context will not be parented under the root span.
Returns:
T (the return value of the function)
withBlockedSpansLocal
Suppress specific spans by name pattern within a scoped callback. Uses OpenTelemetry baggage for propagation, so the blocking applies to all descendant spans including those from auto-instrumentation. Patterns support wildcards.
Returns:
T (the return value of the function)
Context
setSessionId
Set the session ID for the current context. All subsequent spans will be associated with this session.
Returns:
void
setUserId
Set the user ID for the current context. All subsequent spans will be tagged with this user.
Returns:
void
setTenantId
Set the tenant ID for multi-tenant applications. All subsequent spans will be tagged with this tenant.
Returns:
void
setCustomAttributes
Add a custom key-value attribute to the current active span.
Returns:
void
setCustomEvent
Record a custom event on the current active span.
Returns:
void
Input / Output
setInput
Set the input value on the current active span. Useful for recording what was sent to an operation.
Returns:
void
setOutput
Set the output value on the current active span. Useful for recording what an operation returned.
Returns:
void
setRootInput
Set the input value on the root span. Useful for recording the top-level input to your application.
Returns:
void
setRootOutput
Set the output value on the root span. Useful for recording the top-level output of your application.
Returns:
void
Conversations
addConversation
Append a conversation entry to the current active span. Useful for tracking multi-turn LLM conversations.
Returns:
void
Sub-clients
After callingNetra.init(), the following sub-clients are available as static properties on the Netra class. Each provides domain-specific functionality.
SpanWrapper Class
TheSpanWrapper class provides methods for enriching spans with additional context. It’s returned by Netra.startSpan() and supports method chaining.
setAttribute
Add a custom attribute to the span.
Returns:
SpanWrapper (for chaining)
addEvent
Record a timestamped event within the span.
Returns:
SpanWrapper (for chaining)
setPrompt
Set the input prompt for LLM spans.
Returns:
SpanWrapper (for chaining)
setNegativePrompt
Set the negative prompt (commonly used for image generation).
Returns:
SpanWrapper (for chaining)
setModel
Set the model name used in the operation.
Returns:
SpanWrapper (for chaining)
setLlmSystem
Set the LLM provider/system name.
Returns:
SpanWrapper (for chaining)
setUsage
Record token usage and cost metrics.
Returns:
SpanWrapper (for chaining)
setAction
Track actions or tool calls within the span.
Returns:
SpanWrapper (for chaining)
setSuccess
Mark the span as successful.SpanWrapper (for chaining)
setError
Mark the span as failed with an error message.
Returns:
SpanWrapper (for chaining)
end
End the span. Required in TypeScript - spans won’t be exported untilend() is called.
void
Types and Interfaces
SpanType
Enum for categorizing spans.UsageModel
Interface for tracking token usage and costs.ActionModel
Interface for tracking actions and tool calls.Decorators
TypeScript decorators for easy function instrumentation.TypeScript decorators require
"experimentalDecorators": true in your tsconfig.json.@agent
Mark a function or class as an AI agent.@task
Mark a function as a task or tool.@workflow
Mark a function as a workflow.Instruments
Available instrumentations for auto-tracing.Default Root Instruments
WhenrootInstruments is not specified, Netra allows only the following curated subset (DEFAULT_INSTRUMENTS_FOR_ROOT) to produce root-level spans (top-level traces):
When a root span originates from an instrumentation outside this set (e.g.
EXPRESS, HTTP, a database driver), that span is dropped and its children are reparented onto its parent — so LLM and vector spans nested under an unwanted server root are kept while the server span itself is removed. The peel repeats recursively: if a promoted child is itself from a non-root instrumentation it is dropped too, until a surviving span is reached. A span whose parent link is remote (cross-process) is kept as a root so an upstream distributed trace is not severed.
Pass
new Set([NetraInstruments.ALL]) to rootInstruments to let every instrumentation produce root spans (legacy behavior). When enableRootSpan is true, Netra attaches its own root span and every auto-instrumentation span becomes its child, so no reparenting occurs and rootInstruments has no effect.Complete Example
Express.js Integration
Example of integrating Netra with Express.js:Key Differences from Python SDK
Next Steps
- Python SDK Reference - Python API reference
- Auto-Instrumentation - Automatic tracing setup
- Manual Tracing - Advanced tracing patterns
- Decorators - Using decorators effectively
