Skip to main content

View on npm

netra-sdk on npm registry

View on Yarn

netra-sdk on Yarn registry

Installation

Netra Class

The Netra 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.
Parameters: Returns: Promise<void>

Tracing

startSpan

Create a new span for manual tracing. Returns a SpanWrapper that must be explicitly ended with end().
Parameters: 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.
Overloads:
Parameters: 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. Returns undefined if there is no active span.
Returns: 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. Requires enableRootSpan: 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.
Parameters: 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.
Overloads:
Parameters: 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.
Parameters: Returns: void

setUserId

Set the user ID for the current context. All subsequent spans will be tagged with this user.
Parameters: Returns: void

setTenantId

Set the tenant ID for multi-tenant applications. All subsequent spans will be tagged with this tenant.
Parameters: Returns: void

setCustomAttributes

Add a custom key-value attribute to the current active span.
Parameters: Returns: void

setCustomEvent

Record a custom event on the current active span.
Parameters: Returns: void

Input / Output

setInput

Set the input value on the current active span. Useful for recording what was sent to an operation.
Parameters: Returns: void

setOutput

Set the output value on the current active span. Useful for recording what an operation returned.
Parameters: Returns: void

setRootInput

Set the input value on the root span. Useful for recording the top-level input to your application.
Parameters: Returns: void

setRootOutput

Set the output value on the root span. Useful for recording the top-level output of your application.
Parameters: Returns: void

Conversations

addConversation

Append a conversation entry to the current active span. Useful for tracking multi-turn LLM conversations.
Parameters: Returns: void

Sub-clients

After calling Netra.init(), the following sub-clients are available as static properties on the Netra class. Each provides domain-specific functionality.

SpanWrapper Class

The SpanWrapper 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.
Parameters: Returns: SpanWrapper (for chaining)

addEvent

Record a timestamped event within the span.
Parameters: Returns: SpanWrapper (for chaining)

setPrompt

Set the input prompt for LLM spans.
Parameters: Returns: SpanWrapper (for chaining)

setNegativePrompt

Set the negative prompt (commonly used for image generation).
Parameters: Returns: SpanWrapper (for chaining)

setModel

Set the model name used in the operation.
Parameters: Returns: SpanWrapper (for chaining)

setLlmSystem

Set the LLM provider/system name.
Parameters: Returns: SpanWrapper (for chaining)

setUsage

Record token usage and cost metrics.
Parameters: Returns: SpanWrapper (for chaining)

setAction

Track actions or tool calls within the span.
Parameters: Returns: SpanWrapper (for chaining)

setSuccess

Mark the span as successful.
Returns: SpanWrapper (for chaining)

setError

Mark the span as failed with an error message.
Parameters: Returns: SpanWrapper (for chaining)

end

End the span. Required in TypeScript - spans won’t be exported until end() is called.
Returns: void
Always call span.end() in TypeScript, preferably in a finally block to ensure spans are closed even when errors occur.

Types and Interfaces

SpanType

Enum for categorizing spans.

UsageModel

Interface for tracking token usage and costs.
Example:

ActionModel

Interface for tracking actions and tool calls.
Example:

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

When rootInstruments 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

Last modified on July 31, 2026