Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ title: "Set Session ID and User ID"
description: "Adding SessionID and UserID as attributes to Spans for Tracing"
---

## What it is
## About

**Session ID and User ID** are span attributes that let you group and filter traces by conversation session or individual user. By attaching `session.id` and `user.id` to spans you can pinpoint where a conversation breaks, identify trace groups where your application underperforms, and build custom metrics based on session or user performance. Both values are added to the OpenTelemetry context and automatically picked up by traceAI auto-instrumentors as span attributes.
Traces are isolated by default. Without a session or user identifier, there is no way to connect multiple traces that belong to the same conversation or the same end user. Setting `session.id` and `user.id` on spans links them together so traces can be grouped by conversation and filtered by user in the dashboard. Both values are added to the OpenTelemetry context and automatically picked up by traceAI auto-instrumentors as span attributes.

## Use cases
---

## When to use

- **Conversation debugging**Group traces by session ID to view the full message history for a single conversation and pinpoint where it breaks.
- **User-level analysis** Filter spans by user ID to identify which users have the best or worst experiences.
- **Custom metrics** — Use `session.id` and `user.id` to aggregate evaluation results and compare performance across sessions and users.
- **Conversation debugging**: Group traces by session ID to view the full message history for a single conversation and find where it breaks.
- **User-level analysis**: Filter spans by user ID to identify which users have the best or worst experiences.
- **Session and user metrics**: Aggregate evaluation results by `session.id` or `user.id` to compare performance across sessions and users.

---

Expand All @@ -23,11 +25,11 @@ description: "Adding SessionID and UserID as attributes to Spans for Tracing"

<CodeGroup>

```bash Python
```python Python
pip install traceAI-openai
```

```bash JS/TS
```javascript JS/TS
npm install @opentelemetry/api # or yarn add @opentelemetry/api
# Assuming your traceAI or equivalent auto-instrumentation package is already installed.
```
Expand All @@ -36,7 +38,7 @@ description: "Adding SessionID and UserID as attributes to Spans for Tracing"
</Step>

<Step title="Set session ID and user ID">
Choose your approach`using_session`, `using_user`, or `using_attributes`.
Choose your approach:`using_session`, `using_user`, or `using_attributes`.

<Tabs>
<Tab title="using_session">
Expand Down Expand Up @@ -297,14 +299,14 @@ description: "Adding SessionID and UserID as attributes to Spans for Tracing"

## Key concepts

- **`using_session`**Context manager that adds `session.id` to the OpenTelemetry context. All spans from traceAI auto-instrumentors within the block will carry this attribute. Input must be a non-empty string.
- **`using_user`**Context manager that adds `user.id` to the OpenTelemetry context. All spans within the block will carry this attribute. Input must be a non-empty string.
- **`using_attributes`**General-purpose context manager that accepts both `session_id` and `user_id` (and other attributes). Useful when setting multiple context attributes in one call.
- **Baggage (JS/TS)**The JS/TS equivalent of Python context managers. Use `propagation.createBaggage()` and `context.with()` to propagate session and user IDs to child spans.
- **`using_session`**:Context manager that adds `session.id` to the OpenTelemetry context. All spans from traceAI auto-instrumentors within the block will carry this attribute. Input must be a non-empty string.
- **`using_user`**:Context manager that adds `user.id` to the OpenTelemetry context. All spans within the block will carry this attribute. Input must be a non-empty string.
- **`using_attributes`**:General-purpose context manager that accepts both `session_id` and `user_id` (and other attributes). Useful when setting multiple context attributes in one call.
- **Baggage (JS/TS)**:The JS/TS equivalent of Python context managers. Use `propagation.createBaggage()` and `context.with()` to propagate session and user IDs to child spans.

---

## What you can do next
## Next Steps

<CardGroup cols={2}>
<Card title="Set Up Tracing" icon="gear" href="/docs/observe/features/manual-tracing/set-up-tracing">
Expand Down