feat: Add session ID support to tracing service (#5820)

* feat: Add session ID support to tracing service

This commit adds support for setting the session ID in the tracing service. Two methods have been modified:
- In the `Graph` class, the `set_session_id` method has been added to set the ID of the current session.
- In the `ArizePhoenixTracer` class, the `__init__` method has been modified to accept a `session_id` parameter and store it.

These changes enable the tracing service to associate traces with specific sessions, improving trace management and analysis.

* refactor: Improve tracing session ID handling in Graph class

- Rename `set_session_id()` method to `set_tracing_session_id()`
- Add null check for tracing service when setting session ID
- Enhance method to only set session ID when tracing service is available

---------

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
This commit is contained in:
anovazzi1 2025-02-17 11:50:14 -03:00 committed by GitHub
commit af59056604
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 2 deletions

View file

@ -635,6 +635,15 @@ class Graph:
raise ValueError(msg)
return self._run_id
def set_tracing_session_id(self) -> None:
"""Sets the ID of the current session.
Args:
session_id (str): The session ID.
"""
if self.tracing_service:
self.tracing_service.set_session_id(self._session_id)
def set_run_id(self, run_id: uuid.UUID | None = None) -> None:
"""Sets the ID of the current run.
@ -647,6 +656,8 @@ class Graph:
self._run_id = str(run_id)
if self.tracing_service:
self.tracing_service.set_run_id(run_id)
if self._session_id and self.tracing_service is not None:
self.tracing_service.set_session_id(self.session_id)
def set_run_name(self) -> None:
# Given a flow name, flow_id

View file

@ -39,7 +39,9 @@ class ArizePhoenixTracer(BaseTracer):
chat_input_value: str
chat_output_value: str
def __init__(self, trace_name: str, trace_type: str, project_name: str, trace_id: UUID):
def __init__(
self, trace_name: str, trace_type: str, project_name: str, trace_id: UUID, session_id: str | None = None
):
"""Initializes the ArizePhoenixTracer instance and sets up a root span."""
self.trace_name = trace_name
self.trace_type = trace_type
@ -49,6 +51,7 @@ class ArizePhoenixTracer(BaseTracer):
self.flow_id = trace_name.split(" - ")[-1]
self.chat_input_value = ""
self.chat_output_value = ""
self.session_id = session_id
try:
self._ready = self.setup_arize_phoenix()
@ -63,7 +66,7 @@ class ArizePhoenixTracer(BaseTracer):
name=self.flow_id,
start_time=self._get_current_timestamp(),
)
self.root_span.set_attribute(SpanAttributes.SESSION_ID, self.flow_id)
self.root_span.set_attribute(SpanAttributes.SESSION_ID, self.session_id or self.flow_id)
self.root_span.set_attribute(SpanAttributes.OPENINFERENCE_SPAN_KIND, self.trace_type)
self.root_span.set_attribute("langflow.project.name", self.project_name)
self.root_span.set_attribute("langflow.flow.name", self.flow_name)

View file

@ -65,6 +65,7 @@ class TracingService(Service):
self.worker_task: asyncio.Task | None = None
self.end_trace_tasks: set[asyncio.Task] = set()
self.deactivated = self.settings_service.settings.deactivate_tracing
self.session_id: str | None = None
async def log_worker(self) -> None:
while self.running or not self.logs_queue.empty():
@ -163,6 +164,7 @@ class TracingService(Service):
trace_type="chain",
project_name=self.project_name,
trace_id=self.run_id,
session_id=self.session_id,
)
def set_run_name(self, name: str) -> None:
@ -286,3 +288,7 @@ class TracingService(Service):
if langchain_callback:
callbacks.append(langchain_callback)
return callbacks
def set_session_id(self, session_id: str) -> None:
"""Set the session ID for tracing."""
self.session_id = session_id