- Context manager:
startActiveObservation - Wrapper:
observe - Manual:
startObservation
Setup
To get started with the ABV TypeScript SDK you need to install the SDK and initialize the client.Instrumentation
You can add custom instrumentations to your application via- the
observewrapper startActiveObservationcontext managers- manually managing the observation lifecycle and its nesting with the
startObservationfunction
Context management with callbacks
To simplify nesting and context management, you can usestartActiveObservation. These functions take a callback and automatically manage the observation’s lifecycle and the OpenTelemetry context. Any observation created inside the callback will automatically be nested under the active observation, and the observation will be ended when the callback finishes.
This is the recommended approach for most use cases as it prevents context leakage and ensures observations are properly ended.
observe wrapper
Theobserve wrapper is a powerful tool for tracing existing functions without modifying their internal logic. It acts as a decorator that automatically creates a span or generation around the function call. You can use the updateActiveObservation function to add attributes to the observation from within the wrapped function.
observe wrapper by passing an options object as the second argument:
| Option | Description | Default |
|---|---|---|
name | The name of the observation. | The original function’s name. |
asType | The type of observation to create (e.g. span, generation). | "span" |
captureInput | Whether to capture the function’s arguments as the input of the observation. | true |
captureOutput | Whether to capture the function’s return value or thrown error as the output of the observation. | true |
Manual observations
The core tracing function (startObservation) gives you full control over creating observations. You can pass the asType option to specify the type of observation to create.
When you call one of these functions, the new observation is automatically linked as a child of the currently active operation in the OpenTelemetry context. However, it does not make this new observation the active one. This means any further operations you trace will still be linked to the original parent, not the one you just created.
To create nested observations manually, use the methods on the returned object (e.g., parentSpan.startObservation(...)).
Updating Traces
Often, you might not have all the information about a trace (like auserId or sessionId) when you start it. The SDK lets you add or update trace-level attributes at any point during its execution.
.updateTrace() on an observation
When you create an observation manually withstartObservation, the returned object has an .updateTrace() method. You can call this at any time before the root span ends to apply attributes to the entire trace.
updateActiveTrace()
When you’re inside a callback fromstartActiveObservation, or a function wrapped with observe, you might not have a direct reference to an observation object. In these cases, use the updateActiveTrace() function. It automatically finds the currently active trace in the context and applies the new attributes.