.NET: Python: Add OpenTelemetry spans for agent, chat, and function middleware

Author: droideronlineCreated Sep 20, 2026Updated Sep 27, 2026
Labelspython.NETtriage

Summary

Add first-class OpenTelemetry telemetry for middleware execution in the Python Agent Framework.

Current behavior

Agent, chat, and function middleware execute inside the existing middleware pipelines, but MAF does not emit dedicated middleware spans or middleware-specific span attributes. Their execution is therefore only visible indirectly within the enclosing agent, chat, or function/tool span.

Requested behavior

Emit a dedicated span for each middleware invocation, while preserving the existing parent span relationships:

  • Agent middleware spans under the invoke_agent span.
  • Chat middleware spans under the chat / inference span.
  • Function middleware spans under the function/tool execution span.

Suggested span attributes include:

  • Middleware category: agent, chat, or function.
  • Middleware implementation name.
  • Execution outcome: completed, terminated, or failed.
  • Duration.
  • Exception/error type when applicable.

Middleware telemetry should follow the existing observability configuration, exporter setup, sensitive-data controls, and OpenTelemetry semantic-convention guidance. Middleware arguments, messages, and results must not be captured unless sensitive telemetry is explicitly enabled and the data follows the existing redaction policy.

Acceptance criteria

  • Dedicated middleware spans are emitted for agent, chat, and function middleware.
  • Span names and attributes are documented and covered by unit tests.
  • Middleware spans preserve the correct parent-child relationships for streamed and non-streamed execution.
  • Normal middleware termination is distinguishable from middleware failure.
  • Middleware telemetry does not change execution behavior or break async stream cleanup.
  • Exceptions and cancellation are recorded according to existing MAF telemetry conventions.
  • The implementation works for class-based and callable middleware where the middleware name can be determined.

Related issues

  • #3075 requests customizable workflow telemetry in .NET.
  • #6626 tracks broader native GenAI semantic-convention coverage.
  • #8397 tracked a Python workflow stream OpenTelemetry cleanup problem and is now closed.

Source: microsoft/agent-framework