You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add requestContext snapshot to tracing spans (#14020)
## Description
This PR adds automatic capture of `RequestContext` snapshots to tracing
spans. Each span now stores a serialized snapshot of the active
`RequestContext` at the time of span creation, making request-scoped
values (user IDs, tenant IDs, feature flags, etc.) available when
viewing traces.
### Key Changes
- **Span Enhancement**: Added `requestContext` field to `BaseSpan` that
captures a snapshot of the `RequestContext` when a span is created
- **Serialization**: Request context values are serialized using
`deepClean` to handle non-serializable values (functions are replaced
with `'[Function]'`)
- **Empty Context Handling**: Empty request contexts are not stored
(undefined if no data)
- **Child Span Support**: Child spans can optionally capture their own
request context snapshots, allowing different context states at
different span levels
- **Database Schema Updates**: Added `requestContext` column to spans
table across all storage backends (ClickHouse, LibSQL, MSSQL,
PostgreSQL) with backwards compatibility via `alterTable` with
`ifNotExists`
- **Exporter Updates**: Updated exporters (Cloud, Default) to include
`requestContext` in exported span records
- **Type Updates**: Updated TypeScript interfaces and Zod schemas to
include the new field
### Testing
Added comprehensive test coverage including:
- Root span request context capture
- Exported span serialization
- Child span context snapshots (with independent state capture)
- Child spans without explicit context
- Empty context handling
- Non-serializable value filtering
Integration tests verify request context propagation through agent
generation with multiple span types.
## Type of Change
- [x] New feature (non-breaking change that adds functionality)
- [x] Documentation update
- [x] Test update
## Checklist
- [x] I have made corresponding changes to the documentation
- [x] I have added tests that prove my feature works
- [x] All existing tests pass
https://claude.ai/code/session_01QaSLa999aBbRDenrGqtgic
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
## Release Notes
* **New Features**
* Request context snapshot is now captured in tracing spans, making
request-scoped values (user IDs, tenant IDs, feature flags) available
for enhanced observability.
* **Documentation**
* Updated tracing spans reference documentation.
* **Tests**
* Added comprehensive tests verifying request context propagation and
serialization across traces.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Claude <noreply@anthropic.com>
Added `requestContext` field to tracing spans. Each span now automatically captures a snapshot of the active `RequestContext`, making request-scoped values like user IDs, tenant IDs, and feature flags available when viewing traces.
0 commit comments