-
Notifications
You must be signed in to change notification settings - Fork 10
Align BaggageBuilder and Outputmessages middleware #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a36531
Initial plan
Copilot c858b8b
Add BaggageMiddleware, OutputLoggingMiddleware, and ObservabilityHost…
Copilot 64ce06e
Update ObservabilityHostingManager.configure to use ChannelAdapter ty…
Copilot deb01ed
Use MiddlewareSet type for adapter and Activity helper methods instea…
Copilot a297348
Merge branch 'main' into copilot/implement-baggage-middleware-python
nikhilNava 87c1c12
Fix ObservabilityHostingManager.configure to accept MiddlewareSet ins…
Copilot e88a6dd
Address review comments: fix logic callback type, gate on is_agentic_…
Copilot 480e469
Revert logic type to Callable[[TurnContext], Awaitable] to match Midd…
Copilot 734ec1a
set defaults to false
nikhilc-microsoft 233a0fb
Merge branch 'copilot/implement-baggage-middleware-python' of https:/…
nikhilc-microsoft 0491bb9
address PR comment
nikhilc-microsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
14 changes: 14 additions & 0 deletions
14
...-observability-hosting/microsoft_agents_a365/observability/hosting/middleware/__init__.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| from .baggage_middleware import BaggageMiddleware | ||
| from .observability_hosting_manager import ObservabilityHostingManager, ObservabilityHostingOptions | ||
| from .output_logging_middleware import A365_PARENT_SPAN_KEY, OutputLoggingMiddleware | ||
|
|
||
| __all__ = [ | ||
| "BaggageMiddleware", | ||
| "OutputLoggingMiddleware", | ||
| "A365_PARENT_SPAN_KEY", | ||
| "ObservabilityHostingManager", | ||
| "ObservabilityHostingOptions", | ||
| ] |
44 changes: 44 additions & 0 deletions
44
...lity-hosting/microsoft_agents_a365/observability/hosting/middleware/baggage_middleware.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Middleware that propagates OpenTelemetry baggage context derived from TurnContext.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Awaitable, Callable | ||
|
|
||
| from microsoft_agents.activity import ActivityEventNames, ActivityTypes | ||
| from microsoft_agents.hosting.core.turn_context import TurnContext | ||
| from microsoft_agents_a365.observability.core.middleware.baggage_builder import BaggageBuilder | ||
|
|
||
| from ..scope_helpers.populate_baggage import populate | ||
|
|
||
|
|
||
| class BaggageMiddleware: | ||
| """Middleware that propagates OpenTelemetry baggage context derived from TurnContext. | ||
|
|
||
| Async replies (ContinueConversation) are passed through without baggage setup. | ||
| """ | ||
|
|
||
| async def on_turn( | ||
| self, | ||
| context: TurnContext, | ||
| logic: Callable[[TurnContext], Awaitable], | ||
| ) -> None: | ||
| activity = context.activity | ||
| is_async_reply = ( | ||
| activity is not None | ||
| and activity.type == ActivityTypes.event | ||
| and activity.name == ActivityEventNames.continue_conversation | ||
| ) | ||
|
|
||
| if is_async_reply: | ||
| await logic() | ||
| return | ||
|
|
||
| builder = BaggageBuilder() | ||
| populate(builder, context) | ||
| baggage_scope = builder.build() | ||
|
|
||
| with baggage_scope: | ||
| await logic() | ||
101 changes: 101 additions & 0 deletions
101
...g/microsoft_agents_a365/observability/hosting/middleware/observability_hosting_manager.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Singleton manager for configuring hosting-layer observability middleware.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from dataclasses import dataclass | ||
|
|
||
| from microsoft_agents.hosting.core.middleware_set import MiddlewareSet | ||
|
|
||
| from .baggage_middleware import BaggageMiddleware | ||
| from .output_logging_middleware import OutputLoggingMiddleware | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @dataclass | ||
| class ObservabilityHostingOptions: | ||
| """Configuration options for the hosting observability layer.""" | ||
|
|
||
| enable_baggage: bool = False | ||
nikhilNava marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Enable baggage propagation middleware. Defaults to ``False``.""" | ||
|
|
||
| enable_output_logging: bool = False | ||
| """Enable output logging middleware for tracing outgoing messages. Defaults to ``False``.""" | ||
|
|
||
|
|
||
| class ObservabilityHostingManager: | ||
| """Singleton manager for configuring hosting-layer observability middleware. | ||
|
|
||
| Example: | ||
| .. code-block:: python | ||
|
|
||
| ObservabilityHostingManager.configure(adapter.middleware_set, ObservabilityHostingOptions( | ||
| enable_output_logging=True, | ||
| )) | ||
| """ | ||
|
|
||
| _instance: ObservabilityHostingManager | None = None | ||
|
|
||
| def __init__(self) -> None: | ||
| """Private constructor — use :meth:`configure` instead.""" | ||
|
|
||
| @classmethod | ||
| def configure( | ||
| cls, | ||
| middleware_set: MiddlewareSet, | ||
| options: ObservabilityHostingOptions, | ||
| ) -> ObservabilityHostingManager: | ||
| """Configure the singleton instance and register middleware. | ||
|
|
||
| Subsequent calls after the first are no-ops and return the existing instance. | ||
|
|
||
| Args: | ||
| middleware_set: The middleware set to register middleware on | ||
| (e.g., ``adapter.middleware_set``). | ||
| options: Configuration options controlling which middleware to enable. | ||
|
|
||
| Returns: | ||
| The singleton :class:`ObservabilityHostingManager` instance. | ||
|
|
||
| Raises: | ||
| TypeError: If *middleware_set* or *options* is ``None``. | ||
| """ | ||
| if middleware_set is None: | ||
| raise TypeError("middleware_set must not be None") | ||
| if options is None: | ||
| raise TypeError("options must not be None") | ||
|
|
||
| if cls._instance is not None: | ||
| logger.warning( | ||
| "[ObservabilityHostingManager] Already configured. " | ||
| "Subsequent configure() calls are ignored." | ||
| ) | ||
| return cls._instance | ||
|
|
||
| instance = cls() | ||
|
|
||
| if options.enable_baggage: | ||
| middleware_set.use(BaggageMiddleware()) | ||
| logger.info("[ObservabilityHostingManager] BaggageMiddleware registered.") | ||
|
|
||
| if options.enable_output_logging: | ||
| middleware_set.use(OutputLoggingMiddleware()) | ||
| logger.info("[ObservabilityHostingManager] OutputLoggingMiddleware registered.") | ||
|
|
||
| logger.info( | ||
| "[ObservabilityHostingManager] Configured. Baggage: %s, OutputLogging: %s.", | ||
| options.enable_baggage, | ||
| options.enable_output_logging, | ||
| ) | ||
|
|
||
| cls._instance = instance | ||
| return instance | ||
|
|
||
| @classmethod | ||
| def reset(cls) -> None: | ||
| """Reset the singleton instance. Intended for testing only.""" | ||
| cls._instance = None | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.