Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 12, 2026

This PR introduces the following changes:

  • Add PerRequestSpanProcessor which allows Per-request token extraction and trace export: provide interface to set token needed for trace exporting to OpenTelemetry Context, making it available throughout the request lifecycle.
  • Environment variable control: The feature is gated by the environment variable AGENT365_OTEL_EXPORT_TOKEN. When set to true, token export is enabled; otherwise, the default behavior is used and no token is exported.
  • Add single shared timer in PerRequestSpanProcessor that periodically scans buffered traces and force-flushes any that exceed the root-ended grace window or the max trace age, then stops itself when no traces remain.

Sample output:

  1. Success case using per request trace export
image
  1. when export is enabled and per request export is enabled (token failed case)
    image

  2. when export is not enabled but per request export is enabled: trace will be output to console per trace
    [INFO] [PerRequestSpanProcessor] Span start name=execute_tool send-email traceId=cc169943a4013b7ddb553d6619b2d2cd spanId=ae2b25b522479d7b root=false openCount=2
    [INFO] [A365Observability] Span[ae2b25b522479d7b] execute_tool send-email, operation: execute_tool started successfully
    [INFO] [A365Observability] Ending span[ae2b25b522479d7b], duration: 2.006s
    [INFO] [PerRequestSpanProcessor] Span end name=execute_tool send-email traceId=cc169943a4013b7ddb553d6619b2d2cd spanId=ae2b25b522479d7b root=false openCount=1 rootEnded=false
    [INFO] [A365Observability] Ending span[bb6acbd7d6a36902], duration: 6.027s
    [INFO] [PerRequestSpanProcessor] Span end name=invoke_agent A365 Testing Agent traceId=cc169943a4013b7ddb553d6619b2d2cd spanId=bb6acbd7d6a36902 root=true openCount=0 rootEnded=false
    [INFO] [PerRequestSpanProcessor] Flushing trace traceId=cc169943a4013b7ddb553d6619b2d2cd reason=trace_completed spans=3 rootEnded=true
    {...spans output...}
    [INFO] [PerRequestSpanProcessor] Export succeeded traceId=cc169943a4013b7ddb553d6619b2d2cd reason=trace_completed spans=3


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add toggleable per-request export feature with OpenTelemetry Context Add per-request token export via OpenTelemetry Context Jan 13, 2026
Copilot AI requested a review from fpfp100 January 13, 2026 00:12
@fpfp100 fpfp100 closed this Jan 14, 2026
@fpfp100 fpfp100 reopened this Jan 15, 2026
@fpfp100
Copy link
Contributor

fpfp100 commented Jan 15, 2026

@copilot fix lint error and alignment issue

Copy link
Contributor Author

Copilot AI commented Jan 15, 2026

@copilot fix lint error and alignment issue

Fixed in commit f53c93f:

  • Removed unused getExportToken import from openai-agent-auto-instrument-sample
  • Corrected } else { alignment in PerRequestSpanProcessor (was on separate line)

All linting now passes.

@fpfp100
Copy link
Contributor

fpfp100 commented Jan 15, 2026

@copilot undo the changes under tests-agent/openai-agent-auto-instrument-sample/src

Copy link
Contributor Author

Copilot AI commented Jan 15, 2026

@copilot undo the changes under tests-agent/openai-agent-auto-instrument-sample/src

Reverted in commit 945dd4f. All changes under tests-agent/openai-agent-auto-instrument-sample/src/ have been restored to their original state.

@fpfp100 fpfp100 marked this pull request as ready for review January 15, 2026 21:07
@fpfp100 fpfp100 requested a review from a team as a code owner January 15, 2026 21:07
Copilot AI review requested due to automatic review settings January 15, 2026 21:07
@fpfp100 fpfp100 requested a review from a team as a code owner January 15, 2026 21:07
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds support for per-request token export via OpenTelemetry Context in the observability system. The implementation provides a zero-storage approach where authentication tokens are stored only in OTel Context (AsyncLocalStorage) and exported per trace/request when enabled, while maintaining backward compatibility with the existing batch export behavior.

Changes:

  • Added PerRequestSpanProcessor to buffer spans per trace and export them when the request completes, using the token stored in OTel Context
  • Introduced runWithExportToken() and getExportToken() utilities for managing tokens in OpenTelemetry Context
  • Modified Agent365Exporter to support both batch export (using tokenResolver) and per-request export (using token from Context)
  • Added environment variable ENABLE_A365_OBSERVABILITY_PER_REQUEST_EXPORT to toggle between batch and per-request export modes
  • Updated sample application to demonstrate per-request token exchange and export

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
packages/agents-a365-observability/src/tracing/context/token-context.ts New file implementing token storage/retrieval in OTel Context
packages/agents-a365-observability/src/tracing/PerRequestSpanProcessor.ts New processor that buffers spans per trace and exports with captured context
packages/agents-a365-observability/src/tracing/constants.ts Added ENABLE_A365_OBSERVABILITY_PER_REQUEST_EXPORT constant
packages/agents-a365-observability/src/tracing/exporter/utils.ts Added isPerRequestExportEnabled() utility function
packages/agents-a365-observability/src/tracing/exporter/Agent365Exporter.ts Modified to support both batch and per-request export modes
packages/agents-a365-observability/src/ObservabilityBuilder.ts Updated to create appropriate processor based on export mode
packages/agents-a365-observability/src/index.ts Exported new token context utilities
tests-agent/basic-agent-sdk-sample/src/index.ts Added per-request token exchange and export logic
tests/observability/core/PerRequestSpanProcessor.test.ts Comprehensive test coverage for the new processor
tests/observability/core/agent365-exporter.test.ts Added test for per-request export environment variable

…entation, fix security concern, and improve test assertions

Co-authored-by: fpfp100 <[email protected]>
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