Skip to content

otel: Session Lifecycle Instrumentation #2133

@schloerke

Description

@schloerke

Goal: Add OTel spans for session start/end and HTTP/WebSocket connections.

Tasks

  • Modify AppSession._run() method to wrap server initialization in session.start span
    • Updated in Phase 3: Moved span to wrap only app.server() call, not entire session loop
  • Modify _run_session_ended_tasks() to wrap session-ended callbacks in session.end span
  • Create shiny/otel/_attributes.py with:
    • extract_http_attributes(scope) - Extract HTTP attributes from ASGI scope
    • Attributes: server.address, server.port, url.path, url.scheme
  • Apply HTTP attributes to session start span
  • Include session ID in all session spans

Acceptance Criteria

  • Session lifecycle creates spans when SHINY_OTEL_COLLECT >= session
  • Spans include session.id attribute
  • Spans include HTTP metadata (address, port, path, scheme)
  • No spans created when SHINY_OTEL_COLLECT=none
  • Async context propagates correctly through session lifecycle
  • session.start span only wraps server initialization (fixed in Phase 3)

Implementation

Files Created

  • shiny/otel/_attributes.py - HTTP attribute extraction utilities

Files Modified

  • shiny/session/_session.py - Session lifecycle instrumentation
    • session.start span wraps app.server() call (server initialization)
    • session.end span wraps session cleanup callbacks
  • tests/pytest/test_otel_session.py - 9 comprehensive tests

Status

COMPLETED - Phase 2 complete with Phase 3 refinement

Note: The session.start span scope was refined in Phase 3 (commit 8e0420f8) to only wrap the server function initialization instead of the entire session event loop. This provides a more accurate measurement of server initialization time.


Closed by PR #2144

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions