Skip to content

Commit

Permalink
feat(event): add session_id to Event class and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
teocns committed Jan 7, 2025
1 parent c100e1e commit 77e2d49
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions agentops/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Event:
end_timestamp(str): A timestamp indicating when the event ended. Defaults to the time when this Event was instantiated.
agent_id(UUID, optional): The unique identifier of the agent that triggered the event.
id(UUID): A unique identifier for the event. Defaults to a new UUID.
session_id(UUID, optional): The unique identifier of the session that the event belongs to.
foo(x=1) {
...
Expand All @@ -43,6 +44,7 @@ class Event:
end_timestamp: Optional[str] = None
agent_id: Optional[UUID] = field(default_factory=check_call_stack_for_agent_id)
id: UUID = field(default_factory=uuid4)
session_id: Optional[UUID] = None


@dataclass
Expand Down
8 changes: 8 additions & 0 deletions agentops/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ def record(self, event: Union[Event, ErrorEvent], flush_now=False):
if not self.is_running:
return

# Set session_id on the event itself
event.session_id = self.session_id

# Create session context
token = set_value("session.id", str(self.session_id))

Expand All @@ -355,6 +358,11 @@ def record(self, event: Union[Event, ErrorEvent], flush_now=False):
span_def.attributes.update({
AgentOpsAttributes.SESSION_ID: str(self.session_id),
AgentOpsAttributes.SESSION_TAGS: ",".join(self.tags) if self.tags else "",
# Add session_id to event data
AgentOpsAttributes.EVENT_DATA: json.dumps({
"session_id": str(self.session_id),
**span_def.attributes.get(AgentOpsAttributes.EVENT_DATA, {})
})
})

with self._otel_tracer.start_as_current_span(
Expand Down
1 change: 1 addition & 0 deletions agentops/telemetry/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def _get_span_attributes(event: Event) -> Dict[str, AttributeValue]:
AgentOpsAttributes.EVENT_START_TIME: event.init_timestamp if hasattr(event, 'init_timestamp') else event.timestamp,
AgentOpsAttributes.EVENT_END_TIME: getattr(event, 'end_timestamp', None),
AgentOpsAttributes.EVENT_ID: str(event.id),
AgentOpsAttributes.SESSION_ID: str(event.session_id) if event.session_id else None,
})

# Add agent ID if present
Expand Down

0 comments on commit 77e2d49

Please sign in to comment.