Skip to content

feat(events): add span events and real-time streaming infrastructure#21

Merged
vilsonrodrigues merged 2 commits intomainfrom
feat/span-events
Jan 28, 2026
Merged

feat(events): add span events and real-time streaming infrastructure#21
vilsonrodrigues merged 2 commits intomainfrom
feat/span-events

Conversation

@vilsonrodrigues
Copy link
Contributor

Summary

Add new events.py module with span events and real-time streaming infrastructure.

Changes

  • EventType: 15 constants following OpenTelemetry GenAI semantic conventions (gen_ai.agent.start, gen_ai.tool.call, etc.)
  • StreamEvent: Frozen dataclass with name, attributes, timestamp_ns, span_name, span_id, trace_id
  • add_event(): Dual emission to span.add_event() (persistence) AND streaming queue (real-time)
  • Convenience functions: add_tool_call_event(), add_model_response_chunk_event(), etc.
  • EventStream: Context manager for astream() consumers

How it works

from msgtrace.sdk import add_event, EventType, EventStream

# Dual emission: OTel span + streaming queue
add_event(EventType.TOOL_CALL, {
    "tool_name": "search",
    "tool_id": "call_123",
})

# Real-time streaming
async with EventStream() as stream:
    task = asyncio.create_task(agent.acall("Hello"))
    async for event in stream:
        print(event.name, event.attributes)
    await task

Benefits

Aspect Benefit
Single source of truth Events go to OTel spans (persisted) AND streaming queue
Zero overhead When not streaming, only span.add_event() is called
Semantic conventions Uses gen_ai.* namespace (OpenTelemetry standard)
Hierarchy preserved trace_id/span_id in StreamEvent for correlation

Test plan

  • All 103 tests pass (69 existing + 34 new)
  • EventType constants
  • StreamEvent dataclass (creation, immutability)
  • add_event() with/without span, with complex attributes
  • All convenience event functions
  • EventStream sync/async context
  • EventStream captures events, callbacks, ordering
  • Integration tests (nested spans, streaming simulation)

🤖 Generated with Claude Code

@github-actions github-actions bot added documentation Improvements or additions to documentation sdk tests size/XL Extra large PR enhancement New feature or request and removed size/XL Extra large PR labels Jan 28, 2026
vilsonrodrigues and others added 2 commits January 28, 2026 16:06
Add new events.py module with:
- EventType constants following OpenTelemetry GenAI conventions
- StreamEvent dataclass for real-time event streaming
- add_event() dual emission (span.add_event + streaming queue)
- Convenience functions for agent, model, tool, and flow events
- EventStream context manager for astream() consumers
- Comprehensive test suite (34 tests)

This enables real-time event streaming while persisting events
to OTel backends (Jaeger, Zipkin, etc.) via span.add_event().

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@github-actions github-actions bot added size/XL Extra large PR and removed size/XL Extra large PR labels Jan 28, 2026
@vilsonrodrigues vilsonrodrigues merged commit 14ab89a into main Jan 28, 2026
10 checks passed
@vilsonrodrigues vilsonrodrigues deleted the feat/span-events branch January 28, 2026 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request sdk size/XL Extra large PR tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant