Skip to content

Add OpenTelemetry API middleware - #514

Merged
heyitsaamir merged 2 commits into
feature_agent365_supportfrom
heyitsaamir-python-otel-api
Jul 22, 2026
Merged

Add OpenTelemetry API middleware#514
heyitsaamir merged 2 commits into
feature_agent365_supportfrom
heyitsaamir-python-otel-api

Conversation

@heyitsaamir

@heyitsaamir heyitsaamir commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Adds the API-side plumbing for Teams SDK OpenTelemetry: common HTTP middleware, outbound API spans, and auth spans.

Why

This is the foundation layer. It lets us see what the SDK is doing when it calls Teams APIs: which operation ran, which auth flow was used, whether the call failed, and how it lines up under the app’s trace.

Keeping this in the API/common layer also means Apps can stay focused on inbound activity processing instead of knowing HTTP client internals. Nice little separation of concerns, for once.

Interesting bits

  • Adds public common HTTP middleware support.
  • Adds microsoft.teams.api.client for outbound Teams API calls.
  • Adds microsoft.teams.auth.outbound from the API auth token callback.
  • Uses Microsoft.Teams.Api as the instrumentation scope, with the package version attached as the scope version.
  • Keeps the outbound middleware mechanics-only: start/end span, record calls/errors, record exceptions, and run the optional response hook.
  • Client methods own semantic attributes and response-derived tags, including response activity.id.
  • Preserves public compatibility for per-call service URL / agentic identity paths, including the direct conversation activity client path.

Reviewer tips

Start with common HTTP client middleware, then the API outbound middleware, then conversation activity methods. The compatibility bits are worth a close look because they keep old scoping behavior working while routing through the new token callback path.

Testing

CI is green for this layer. The full stack was also live-smoked against Agent365/ACF and Echo/PokemonCatcher before splitting.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 0d367487-e88d-4423-8016-475637d47ed0

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds OpenTelemetry-based outbound diagnostics plumbing to the Teams API client stack by introducing a common HTTP middleware pipeline and attaching outbound/auth spans + metrics from API client operations.

Changes:

  • Added a public middleware mechanism to the common HTTP client and refactored request dispatch through a middleware pipeline.
  • Introduced Teams API diagnostics helpers/constants plus outbound telemetry middleware and auth-token span recording.
  • Updated conversation activity APIs to preserve existing service URL / agentic identity scoping while routing through the new telemetry + token callback paths.
Show a summary per file
File Description
uv.lock Adds opentelemetry-api to the workspace lock for the API package.
packages/common/tests/test_client.py Adds unit tests covering middleware behavior, ordering, mutation, and metadata stripping.
packages/common/src/microsoft_teams/common/http/client.py Implements middleware types, registration, cloning, and the _send() dispatch pipeline.
packages/common/src/microsoft_teams/common/http/init.py Exports new middleware types from the common HTTP module.
packages/api/tests/unit/test_diagnostics.py Adds tests for exported telemetry names and basic diagnostics helper behavior.
packages/api/tests/unit/test_conversation_client.py Extends tests for scoping compatibility and validates outbound/auth telemetry behavior.
packages/api/tests/unit/test_base_client.py Adds tests ensuring outbound telemetry middleware is registered once and auth spans are recorded.
packages/api/src/microsoft_teams/api/models/account.py Extends the Account model with additional optional fields (email, user_role).
packages/api/src/microsoft_teams/api/diagnostics/_telemetry.py Defines canonical tracer/meter names and instrumentation version for the API package.
packages/api/src/microsoft_teams/api/diagnostics/_outbound.py Adds outbound telemetry middleware and a metadata carrier for per-call attributes/hooks.
packages/api/src/microsoft_teams/api/diagnostics/_helpers.py Adds tracer/meter helpers plus outbound counters and exception recording utilities.
packages/api/src/microsoft_teams/api/diagnostics/_constants.py Defines internal constants for attribute keys, metric names, operation names, and span names.
packages/api/src/microsoft_teams/api/diagnostics/init.py Exposes public telemetry naming API via __all__.
packages/api/src/microsoft_teams/api/clients/user/client.py Ensures outbound telemetry middleware is installed when HTTP client is set.
packages/api/src/microsoft_teams/api/clients/conversation/client.py Adds service URL/agentic identity kwargs and scoped-client routing while ensuring telemetry middleware.
packages/api/src/microsoft_teams/api/clients/conversation/activity.py Adds per-operation telemetry metadata and response hooks for activity id tagging + scoped routing.
packages/api/src/microsoft_teams/api/clients/bot/client.py Ensures outbound telemetry middleware is installed when HTTP client is set.
packages/api/src/microsoft_teams/api/clients/base_client.py Ensures outbound telemetry middleware is installed at construction and on HTTP reassignment.
packages/api/src/microsoft_teams/api/clients/api_client.py Wraps auth provider token resolution in an auth outbound span and wires conversation scoping factory.
packages/api/src/microsoft_teams/api/init.py Re-exports diagnostics from the top-level API package.
packages/api/pyproject.toml Adds opentelemetry-api to the API package dependencies.

Review details

  • Files reviewed: 20/21 changed files
  • Comments generated: 2
  • Review effort level: Low

Comment thread packages/api/src/microsoft_teams/api/diagnostics/_outbound.py
Comment thread packages/api/tests/unit/test_diagnostics.py
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 0d367487-e88d-4423-8016-475637d47ed0
@heyitsaamir
heyitsaamir merged commit b620848 into feature_agent365_support Jul 22, 2026
5 checks passed
@heyitsaamir
heyitsaamir deleted the heyitsaamir-python-otel-api branch July 22, 2026 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants