Align BaggageBuilder and Outputmessages middleware#187
Open
Align BaggageBuilder and Outputmessages middleware#187
Conversation
…ingManager Implement Python equivalents of the Node.js PR #210 middleware: - BaggageMiddleware: propagates OpenTelemetry baggage from TurnContext - OutputLoggingMiddleware: creates OutputScope spans for outgoing messages - ObservabilityHostingManager: singleton to configure hosting middleware - 19 unit tests covering all three middleware components Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Implement middleware in Python from Node.js code
Add BaggageMiddleware, OutputLoggingMiddleware, and ObservabilityHostingManager
Feb 27, 2026
…pe and require non-None params - Replace _AdapterLike protocol with actual ChannelAdapter from microsoft_agents.hosting.core - Make adapter and options required (non-optional) parameters - Raise TypeError if either is None - Update tests to match new contract Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
…d of direct attribute access - Change adapter param type from ChannelAdapter to MiddlewareSet (the actual middleware registration object) - Use activity.get_agentic_instance_id() instead of recipient.agentic_app_id - Use activity.get_agentic_user() instead of recipient.agentic_user_id - Update test to use agentic role for proper helper method behavior Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
Copilot
AI
changed the title
Add BaggageMiddleware, OutputLoggingMiddleware, and ObservabilityHostingManager
Use MiddlewareSet type for adapter param and Activity helper methods for agentic properties
Mar 2, 2026
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the hosting-layer observability integration to (1) configure middleware against the concrete MiddlewareSet type with required parameters, and (2) align agentic identity extraction with the canonical Activity helper APIs.
Changes:
- Tightens
ObservabilityHostingManager.configure()to require aMiddlewareSetadapter + options (raisingTypeErroronNone) and registers hosting middlewares accordingly. - Switches agentic property access to
Activity.get_agentic_instance_id()/Activity.get_agentic_user()(vs directrecipient.*access). - Adds/updates unit tests for the new hosting middleware and configuration behavior.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/observability/hosting/scope_helpers/test_scope_helper_utils.py | Updates test data to use an agentic role so agentic helpers are exercised. |
| tests/observability/hosting/middleware/test_output_logging_middleware.py | Adds tests covering output logging middleware send pipeline behavior. |
| tests/observability/hosting/middleware/test_observability_hosting_manager.py | Adds tests for singleton configure semantics and required params. |
| tests/observability/hosting/middleware/test_baggage_middleware.py | Adds tests for baggage propagation and async-reply skip behavior. |
| tests/observability/hosting/middleware/init.py | Test package init added (copyright header only). |
| libraries/.../scope_helpers/utils.py | Uses Activity helper methods for agentic instance/user IDs in target agent pairs. |
| libraries/.../middleware/output_logging_middleware.py | Introduces middleware to create OutputScope spans for outgoing messages. |
| libraries/.../middleware/observability_hosting_manager.py | Introduces singleton manager that registers hosting observability middleware. |
| libraries/.../middleware/baggage_middleware.py | Introduces middleware that populates OpenTelemetry baggage from TurnContext. |
| libraries/.../middleware/init.py | Exports the new middleware + manager symbols. |
| libraries/.../hosting/init.py | Re-exports middleware/manager at the hosting package level. |
...ability-hosting/microsoft_agents_a365/observability/hosting/middleware/baggage_middleware.py
Show resolved
Hide resolved
...-hosting/microsoft_agents_a365/observability/hosting/middleware/output_logging_middleware.py
Show resolved
Hide resolved
...-hosting/microsoft_agents_a365/observability/hosting/middleware/output_logging_middleware.py
Show resolved
Hide resolved
tests/observability/hosting/middleware/test_output_logging_middleware.py
Show resolved
Hide resolved
...ability-hosting/microsoft_agents_a365/observability/hosting/middleware/baggage_middleware.py
Outdated
Show resolved
Hide resolved
…tead of ChannelAdapter ChannelAdapter is an ABC (CloudAdapter extends ChannelServiceAdapter extends ChannelAdapter). The adapter HAS-A MiddlewareSet (composition) — it is not one. The configure() method only needs the middleware registration object, so accept MiddlewareSet directly. Users pass adapter.middleware_set. Rename parameter from 'adapter' to 'middleware_set' for clarity. Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
Copilot
AI
changed the title
Use MiddlewareSet type for adapter param and Activity helper methods for agentic properties
Use MiddlewareSet type and Activity helper methods in observability hosting
Mar 2, 2026
…request - Fix `logic` type annotation from `Callable[[TurnContext], Awaitable]` to `Callable[[], Awaitable]` in both BaggageMiddleware and OutputLoggingMiddleware. At runtime, MiddlewareSet passes a zero-arg `call_next_middleware` closure, so the annotation now matches the callsite. - Gate `_derive_agent_details` on `activity.is_agentic_request()` to avoid emitting spans with empty agent_id for non-agentic requests. - Remove unused `logging`/`logger` from baggage_middleware.py. - Update test recipient role from "assistant" to "agenticAppInstance" to exercise the intended agentic code path. Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
…leware Protocol; consolidate tests Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
Copilot
AI
changed the title
Use middleware to set baggage builder and capture outputs automatically
Align middleware with Middleware Protocol; consolidate tests
Mar 2, 2026
juliomenendez
previously approved these changes
Mar 2, 2026
…/github.com/microsoft/Agent365-python into copilot/implement-baggage-middleware-python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Manual testing
Baggage and outputs can now be set automatically using the middleware.
The
logiccallback type inBaggageMiddlewareandOutputLoggingMiddlewarewas changed toCallable[[], Awaitable]to match runtime behavior, but this diverges from the upstreamMiddlewareProtocol which definesCallable[[TurnContext], Awaitable]. All reference implementations (e.g.TranscriptLoggerMiddleware) follow the Protocol signature. Additionally, tests had redundant cases that could be consolidated.Type annotation
logictype toCallable[[TurnContext], Awaitable]in both middlewares to match theMiddlewareProtocol inmicrosoft-agents-hosting-coreAgentic gating
_derive_agent_detailsgates onactivity.is_agentic_request()and returnsNonefor non-agentic requests, preventing spans with emptyagent_idrole="agenticAppInstance"to exercise the agentic code pathTest consolidation (31 → 28 tests, same coverage)
test_baggage_middleware_calls_logic— already proven bytest_baggage_middleware_propagates_baggagetest_send_handler_creates_output_scope_for_messagesandtest_send_handler_disposes_scope_on_success(duplicate success-path setup)test_configure_returns_instanceintotest_configure_is_singletontest_configure_registers_expected_middlewarestest_configure_raises_on_noneCleanup
logging/loggerfrombaggage_middleware.py💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.