Add OutputScope, BaggageMiddleware, OutputLoggingMiddleware, and ObservabilityHostingManager#210
Conversation
… tracing Introduces message-level tracing via InputScope and OutputScope, registered through MessageLoggingMiddleware and ObservabilityMiddlewareRegistrar. Both scopes lazily read A365_PARENT_SPAN_KEY from turnState so the agent handler can link them as children of an InvokeAgentScope. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This pull request adds comprehensive tracing support for input and output messages in the observability pipeline by introducing InputScope and OutputScope for message tracing, along with MessageLoggingMiddleware for automatic integration into the adapter pipeline. The PR also standardizes message recording across all scopes to use JSON array format instead of comma-separated strings.
Changes:
- Added
InputScopeandOutputScopeclasses with lazy parent span linking viaturnState, supporting caller details, conversation IDs, execution types, and source metadata - Added
MessageLoggingMiddlewareto automatically create input/output spans with baggage propagation and optional parent span linking throughA365_PARENT_SPAN_KEY - Changed message recording format from comma-separated to JSON arrays across
InvokeAgentScope,InferenceScope, and new scopes - Added
parseExecutionType()helper function for safe string-to-enum conversion with validation
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/agents-a365-observability/src/tracing/scopes/InputScope.ts | New scope class for tracing input messages with caller details, conversation ID, execution type, and lazy flush optimization |
| packages/agents-a365-observability/src/tracing/scopes/OutputScope.ts | Enhanced to accept caller details, execution type, source metadata, and conversation ID; implements lazy flush for recordOutputMessages |
| packages/agents-a365-observability-hosting/src/middleware/MessageLoggingMiddleware.ts | New middleware for automatic input/output tracing with baggage propagation and parent span linking support |
| packages/agents-a365-observability-hosting/src/middleware/ObservabilityMiddlewareRegistrar.ts | Fluent builder for registering observability middleware on adapters |
| packages/agents-a365-observability/src/tracing/scopes/InvokeAgentScope.ts | Updated recordInputMessages and recordOutputMessages to use JSON.stringify instead of join |
| packages/agents-a365-observability/src/tracing/scopes/InferenceScope.ts | Updated recordInputMessages and recordOutputMessages to use JSON.stringify instead of join |
| packages/agents-a365-observability/src/tracing/contracts.ts | Added parseExecutionType helper for safe string-to-enum conversion with Set-based validation |
| packages/agents-a365-observability/src/tracing/constants.ts | Added INPUT_MESSAGES_OPERATION_NAME constant |
| packages/agents-a365-observability-hosting/src/utils/ScopeUtils.ts | Updated to use parseExecutionType with fallback to base execution type |
| packages/agents-a365-observability/src/index.ts | Exported InputScope and parseExecutionType |
| packages/agents-a365-observability-hosting/src/index.ts | Exported MessageLoggingMiddleware, A365_PARENT_SPAN_KEY, MessageLoggingMiddlewareOptions, and ObservabilityMiddlewareRegistrar |
| tests/observability/core/input-scope.test.ts | Comprehensive test coverage for InputScope including parent linking, caller details, and message recording |
| tests/observability/core/output-scope.test.ts | Refactored and consolidated tests for OutputScope |
| tests/observability/extension/hosting/message-logging-middleware.test.ts | Extensive test coverage including error handling, parent linking, baggage propagation, and async replies |
| tests/observability/extension/hosting/observability-middleware-registrar.test.ts | Tests for fluent builder pattern and middleware registration |
| tests/observability/extension/hosting/scope-utils.test.ts | Updated assertions to expect JSON.stringify format for input messages |
packages/agents-a365-observability-hosting/src/middleware/MessageLoggingMiddleware.ts
Outdated
Show resolved
Hide resolved
packages/agents-a365-observability-hosting/src/middleware/MessageLoggingMiddleware.ts
Outdated
Show resolved
Hide resolved
packages/agents-a365-observability/src/tracing/scopes/InputScope.ts
Outdated
Show resolved
Hide resolved
packages/agents-a365-observability-hosting/src/middleware/MessageLoggingMiddleware.ts
Outdated
Show resolved
Hide resolved
packages/agents-a365-observability-hosting/src/middleware/ObservabilityMiddlewareRegistrar.ts
Outdated
Show resolved
Hide resolved
packages/agents-a365-observability-hosting/src/middleware/ObservabilityHostingManager.ts
Outdated
Show resolved
Hide resolved
packages/agents-a365-observability-hosting/src/middleware/ObservabilityHostingManager.ts
Outdated
Show resolved
Hide resolved
packages/agents-a365-observability-hosting/src/middleware/ObservabilityHostingManager.ts
Outdated
Show resolved
Hide resolved
packages/agents-a365-observability-hosting/src/middleware/ObservabilityHostingManager.ts
Outdated
Show resolved
Hide resolved
- Remove parseExecutionType (execution type removed in new schema) - Make ObservabilityHostingManager.configure() an instance method with required params - Remove static singleton pattern and getInstance() from ObservabilityHostingManager - Update CHANGELOG with unreleased changes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
packages/agents-a365-observability-hosting/src/middleware/OutputLoggingMiddleware.ts
Show resolved
Hide resolved
packages/agents-a365-observability-hosting/src/middleware/OutputLoggingMiddleware.ts
Show resolved
Hide resolved
packages/agents-a365-observability-hosting/src/middleware/ObservabilityHostingManager.ts
Show resolved
Hide resolved
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| * TurnState key for the parent span reference. | ||
| * Set this in `turnState` to link OutputScope spans as children of an InvokeAgentScope. | ||
| */ | ||
| export const A365_PARENT_SPAN_KEY = 'A365ParentSpanId'; |
There was a problem hiding this comment.
A365_PARENT_SPAN_KEY stores a full ParentSpanRef (traceId/spanId/traceFlags), but the underlying string value 'A365ParentSpanId' implies it contains only a spanId. This is likely to confuse consumers and can lead to incorrect usage. Consider renaming the key string to reflect that it stores a parent span reference/context (and keep the exported constant name/value aligned).
Summary
OutputScopeto@microsoft/agents-a365-observabilityfor tracing output messages with parent span linking, caller details, and custom start/end time supportMessageLoggingMiddlewareintoBaggageMiddleware(baggage propagation) andOutputLoggingMiddleware(output span tracing)ObservabilityHostingManageras an entry point for configuring hosting-layer middlewareInputScope(not supported in current schema)parseExecutionType(execution type removed in new telemetry schema)Middleware setup
BaggageMiddlewareis registered by default and propagates caller, agent, tenant, channel, conversation, and execution type as OpenTelemetry baggage. SetenableBaggage: falseto disable.OutputLoggingMiddlewarecreatesOutputScopespans for outgoing messages. To link output spans as children of anInvokeAgentScope, setA365_PARENT_SPAN_KEYinturnState:Span hierarchy
Test plan
BaggageMiddleware(baggage propagation, async reply skip, pass-through)OutputLoggingMiddleware(span creation, caller details, parent linking, async reply, error re-throw, skip conditions)ObservabilityHostingManager(default middleware, options, idempotency)OutputScope(attributes, parent ref, message recording)🤖 Generated with Claude Code