Set Session ID and User ID
Adding SessionID and UserID as attributes to Spans for Tracing
Understanding Sessions
A session groups traces using a session ID
attribute. This is particularly useful when developing or debugging a chatbot application, as it allows you to view collections of messages or traces that belong to a series of interactions between a human and the AI. By incorporating session.id
and user.id
as span attributes, you can:
- Pinpoint where a conversation “breaks” or deviates. This helps in identifying if a user becomes increasingly frustrated or if a chatbot is ineffective.
- Identify trace groups where your application underperforms. Adding
session.id
and/oruser.id
from an application allows for grouping and further filtering of interactions. - Develop custom metrics based on evaluations using
session.id
oruser.id
to identify the best and worst performing sessions and users.
Adding SessionID and UserID
Session and user IDs can be added to a span through auto instrumentation or manual instrumentation of traceAI. Any LLM call within the context (the with
block in the example below) will include the corresponding session.id
or user.id
as a span attribute. Both session.id
and user.id
must be non-empty strings.
When setting up your instrumentation, you can pass the sessionID
attribute as demonstrated below.
using_session
This context manager adds a session ID to the current OpenTelemetry Context. traceAI auto instrumentators will read this Context and pass the session ID as a span attribute, adhering to the traceAI semantic conventions. The session ID input must be a non-empty string.
It can also be applied as a decorator:
using_user
This context manager adds a user ID to the current OpenTelemetry Context. traceAI auto instrumentators will read this Context and pass the user ID as a span attribute, following the traceAI semantic conventions. The user ID input must be a non-empty string.
It can also be applied as a decorator:
Additional Examples
Install the required package:
Once your OpenAI client is defined, any call inside our context managers will attach the corresponding attributes to the spans.
Alternatively, if you wrap your calls inside functions, you can use them as decorators: