Skip to content

Add experimental TraceTimingMiddleware and refine ASGI header typing in IMiddleware#5979

Draft
cobycloud wants to merge 2 commits into
masterfrom
bbdx/create-swarmauri_middleware-plugin/2026-03-16
Draft

Add experimental TraceTimingMiddleware and refine ASGI header typing in IMiddleware#5979
cobycloud wants to merge 2 commits into
masterfrom
bbdx/create-swarmauri_middleware-plugin/2026-03-16

Conversation

@cobycloud

Copy link
Copy Markdown
Contributor

Motivation

  • Provide an experimental ASGI middleware that propagates W3C trace-context and emits compact Server-Timing metrics for latency observability.
  • Improve typing for ASGI headers in the core middleware interface to allow clearer, reusable header types across middleware implementations.

Description

  • Introduces a new experimental package swarmauri_middleware_tracetiming with TraceTimingMiddleware that registers via ComponentBase and implements MiddlewareBase hooks on_scope and on_send.
  • TraceTimingMiddleware extracts or generates traceparent/tracestate, stores timing information on the request scope, and writes server-timing, timing-allow-origin, and trace headers on http.response.start; an edge_ms_getter can be injected to include edge duration.
  • Adds a small HeaderCollection helper for case-insensitive ASGI header read/write and a _gen_trace_ids() helper to create compliant traceparent values.
  • Updates pkgs/pyproject.toml to include the new experimental package and refactors the middleware interface IMiddleware typing by introducing Header: TypeAlias, Headers: TypeAlias, and updating Message.headers to use Headers.

Testing

  • Ran package tests for the new middleware with pytest pkgs/experimental/swarmauri_middleware_tracetiming -q, executing unit tests in tests/unit and integration tests in tests/i9n, and all tests passed.
  • Verified importability via the integration import test test_import_package which succeeded.

Codex Task

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant