Skip to content

Conversation

shah-siddd
Copy link
Contributor

Pull Request

Summary

Add user and session context tracking to OpenLayer tracing functionality. This enhancement enables developers to associate traces with specific users and sessions, providing better observability and analytics for multi-user applications.

Changes

  • New context management module - Added src/openlayer/lib/tracing/context.py with user/session context functions
  • Public API functions - Exposed 5 new functions for managing user and session context:
    • set_user_session_context() - Set default user/session IDs (typically in middleware)
    • update_trace_user_session() - Override user/session IDs for specific traces
    • get_current_user_id() / get_current_session_id() - Retrieve current context
    • clear_user_session_context() - Clear all context
  • Automatic trace enrichment - Modified tracer to automatically include user_id and session_id in trace data
  • Column configuration - Added support for user_id_column_name and session_id_column_name in data stream configuration

Context

This addresses the need to track user sessions and individual users within OpenLayer traces.

  1. Give a way to set default user/session context once in middleware
  2. Override context anywhere in traced code when needed

@@ -0,0 +1,44 @@
"""OpenLayer tracing module."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, we don't use this file.

We expose all features in the tracing module in lib/__init__.py (e.g., here.) This way, the user only needs to import from lib. E.g.,

from openlayer.lib import trace # as opposed to from openlayer.lib.tracing.tracer import trace

In my opinion, we should maintain the pattern we're currently following. In that case, you should move the imports to https://github.com/openlayer-ai/openlayer-python/blob/main/src/openlayer/lib/__init__.py, to expose them there.

Open to hear your perspective as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense. updated.

@@ -0,0 +1,167 @@
"""
Streamlined user and session context management for OpenLayer tracing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: in this and other places, you have the "L" capitalized. We go by "Openlayer"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha. updated.

@@ -89,6 +89,12 @@ class ConfigLlmData(TypedDict, total=False):
used.
"""

user_id_column_name: Annotated[str, PropertyInfo(alias="userIdColumnName")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is auto-generated by Stainless and we shouldn't modify it directly. I'm going to comment in your Linear how to regenerate this file.

For this (and our other SDKs generated by Stainless), we should only edit files inside /lib and /examples. The rest is fully autogenerated from our OpenAPI spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it.

@shah-siddd shah-siddd force-pushed the siddhant/open-7297-add-user_id-and-session_id-in-traces-to-track-users-and branch from 6499738 to 03ed5a2 Compare September 9, 2025 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants