Typescript / Javascript
Learn how to use, upload, and debug your LLM Application traces with our Javascript library
Last updated
Was this helpful?
Learn how to use, upload, and debug your LLM Application traces with our Javascript library
Last updated
Was this helpful?
Acquiring a tracer
Anywhere in your application where you write manual tracing code should call getTracer
to acquire a tracer. For example:
The values of instrumentation-scope-name
and instrumentation-scope-version
should uniquely identify the , such as the package, module or class name. While the name is required, the version is still recommended despite being optional.
It’s generally recommended to call getTracer
in your app when you need it rather than exporting the tracer
instance to the rest of your app. This helps avoid trickier application load issues when other required dependencies are involved.
In the case of the , there are two places where a tracer may be acquired with an appropriate Instrumentation Scope:
First, in the application file app.ts
(or app.js
):
The API of OpenTelemetry JavaScript exposes two methods that allow you to create spans:
In most cases you want to use the latter (tracer.startActiveSpan
), as it takes care of setting the span and its context active.
The code below illustrates how to create an active span.
The above instrumented code can now be pasted in the /chat
handler. You should now be able to see spans emitted from your app.
Start your app as follows, and then send it requests by visiting http://localhost:8080/chat?message="how long is a pencil"
with your browser or curl
.
After a while, you should see the spans printed in the console by the ConsoleSpanExporter
, something like this:
Get Active Context
Example to grab the active context:
You can also add attributes to a span as it’s created:
First, add both semantic conventions as a dependency to your application:
Add the following to the top of your application file:
Finally, you can update your file to include semantic attributes:
Span events
Span Status
The status can be set at any time before the span is finished.
Using sdk-trace-base
and manually propagating span context
In some cases, you may not be able to use either the Node.js SDK nor the Web SDK. The biggest difference, aside from the initialization code, is that you’ll have to manually set spans as active in the current context to be able to create nested spans.
Initializing tracing with sdk-trace-base
Initializing tracing is similar to how you’d do it with Node.js or the Web SDK.
Like the other examples in this document, this exports a tracer you can use throughout the app.
Creating nested spans with sdk-trace-base
To create nested spans, you need to set whatever the currently-created span is as the active span in the current context. Don’t bother using startActiveSpan
because it won’t do this for you.
All other APIs behave the same when you use sdk-trace-base
compared with the Node.js SDKs.
Create spans Now that you have initialized, you can create .
: Starts a new span without setting it on context.
: Starts a new span and calls the given callback function passing it the created span as the first argument. The new span gets set in context and this context is activated for the duration of the function call.
Get the current span Sometimes it’s helpful to do something with the current/active at a particular point in program execution.
Get a span from context It can also be helpful to get the from a given context that isn’t necessarily the active span.
Attributes
let you attach key/value pairs to a so it carries more information about the current operation that it’s tracking. For OpenInference related attributes, use the @arizeai/openinference-semantic-conventions
keys. However, you are free to add any attributes you'd like!
Semantic Attributes There are semantic conventions for spans representing operations in well-known protocols like HTTP or database calls. OpenInference also publishes its own set of semantic conventions related to LLM applications. Semantic conventions for these spans are defined in the specification under . In the simple example of this guide, the source code attributes can be used.
A is a human-readable message on an that represents a discrete event with no duration that can be tracked by a single timestamp. You can think of it like a primitive log.
You can also create Span Events with additional
A can be set on a , typically used to specify that a Span has not completed successfully - Error
. By default, all spans are Unset
, which means a span completed without error. The Ok
status is reserved for when you need to explicitly mark a span as successful rather than stick with the default of Unset
(i.e., “without error”).
Recording exceptions It can be a good idea to record exceptions when they happen. It’s recommended to do this in conjunction with setting .
This documentation includes material adapted from the OpenTelemetry JS "Instrumentation" documentation, originally authored by the OpenTelemetry Authors, available . It is used under the Create Commons Attribution 4.0 license (). It has been modified here to provide relevant examples for Arize.