Skip to content

Add a365 hosting middleware - #29

Merged
Hector Hernandez (hectorhdzg) merged 9 commits into
microsoft:mainfrom
hectorhdzg:hectorhdzg/a365-hosting-middleware
Apr 22, 2026
Merged

Hector Hernandez (hectorhdzg) merged 9 commits into
microsoft:mainfrom
hectorhdzg:hectorhdzg/a365-hosting-middleware

Conversation

@hectorhdzg

Copy link
Copy Markdown
Member

No description provided.

Add hosting-layer middleware adapted from Agent365-nodejs that enables
automatic OpenTelemetry instrumentation for agents-hosting compatible
adapters using structural typing (no direct dependency on
@microsoft/agents-hosting).

Hosting middleware:
- BaggageMiddleware: propagates OTel baggage (tenant, agent, caller,
  channel, conversation) from TurnContext; skips ContinueConversation
- OutputLoggingMiddleware: creates OutputScope spans for outgoing
  messages with parent span linking
- ObservabilityHostingManager: one-call middleware registration

Hosting utilities:
- TurnContextUtils: extract baggage pairs from TurnContext activity
- ScopeUtils: create InferenceScope/InvokeAgentScope/ExecuteToolScope
  pre-populated from TurnContext
- BaggageBuilderUtils: populate BaggageBuilder from TurnContext

Bug fixes (vendored A365 code vs upstream):
- Fix S2S exporter endpoint path (add /otlp/ segment)
- Fix SDK version constant to read from package version
- Add ~15 missing OpenTelemetryConstants from upstream

Also includes:
- Comprehensive unit tests (6 test files, 62 new tests)
- Hosting middleware sample app (a365HostingMiddleware.ts)
- Updated samples README

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

This PR introduces A365 “hosting-layer” observability helpers (TurnContext-derived baggage extraction, scope population utilities, and adapter middleware) and wires them into the public exports/samples, along with an Agent365 exporter endpoint update.

Changes:

  • Added A365 hosting utilities (TurnContextLike types, baggage/scope helpers) and middleware (BaggageMiddleware, OutputLoggingMiddleware, ObservabilityHostingManager).
  • Re-exported hosting utilities from src/a365/index.ts and the root src/index.ts, and added a new sample demonstrating usage.
  • Updated Agent365 trace export URL path to include /otlp/agents/… and adjusted unit tests accordingly.

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
test/internal/unit/a365/hosting/turnContextUtils.test.ts Adds unit coverage for TurnContext baggage helpers and token parsing behavior.
test/internal/unit/a365/hosting/scopeUtils.test.ts Adds unit coverage for populating scopes from TurnContext and error paths.
test/internal/unit/a365/hosting/outputLoggingMiddleware.test.ts Adds unit coverage for OutputLoggingMiddleware span creation/linking and error recording.
test/internal/unit/a365/hosting/observabilityHostingManager.test.ts Adds unit coverage for middleware registration behavior and idempotent configure().
test/internal/unit/a365/hosting/baggageMiddleware.test.ts Adds unit coverage for baggage propagation and ContinueConversation bypass.
test/internal/unit/a365/hosting/baggageBuilderUtils.test.ts Adds unit coverage for building baggage from TurnContext helpers.
test/internal/unit/a365/agent365Exporter.test.ts Updates expectations for the new /otlp/agents/ exporter path.
src/index.ts Re-exports hosting utilities/middleware from the public package entrypoint.
src/a365/middleware/BaggageBuilder.ts Minor edit around setPairs (lint suppression/comment change).
src/a365/index.ts Re-exports hosting utilities/middleware via the A365 sub-entrypoint.
src/a365/hosting/types.ts Introduces minimal structural interfaces for hosting adapters (Activity/TurnContext/Middleware).
src/a365/hosting/turnContextUtils.ts Adds TurnContext-to-baggage extraction utilities + inline JWT claim extraction.
src/a365/hosting/scopeUtils.ts Adds helpers to derive agent/caller/channel details and populate scopes from TurnContext.
src/a365/hosting/outputLoggingMiddleware.ts Adds middleware to create OutputScope spans for outgoing message activities.
src/a365/hosting/observabilityHostingManager.ts Adds a manager to configure/register hosting middleware on an adapter.
src/a365/hosting/index.ts Barrel exports for hosting utilities/middleware/types.
src/a365/hosting/baggageMiddleware.ts Adds middleware to build/run an OTel baggage scope for each turn.
src/a365/hosting/baggageBuilderUtils.ts Adds helper class to populate BaggageBuilder from TurnContext.
src/a365/exporter/Agent365Exporter.ts Updates exporter endpoint construction to include /otlp/agents/.
src/a365/constants.ts Extends A365 constants and sets telemetry SDK version from distro version constant.
samples/src/a365HostingMiddleware.ts New sample demonstrating hosting middleware + scope utilities usage.
samples/README.md Adds the new sample to the samples index.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/a365/hosting/outputLoggingMiddleware.ts
Comment thread src/a365/middleware/BaggageBuilder.ts
Comment thread src/a365/hosting/turnContextUtils.ts
Comment thread src/a365/hosting/scopeUtils.ts
Comment thread src/a365/hosting/scopeUtils.ts
Comment thread src/a365/hosting/outputLoggingMiddleware.ts
@hectorhdzg

Copy link
Copy Markdown
Member Author

Copilot address the comments in this PR

@hectorhdzg
Hector Hernandez (hectorhdzg) merged commit 9497f3c into microsoft:main Apr 22, 2026
5 checks passed
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