-
Notifications
You must be signed in to change notification settings - Fork 5
Add per-request token export via OpenTelemetry Context #145
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: fpfp100 <[email protected]>
…able constants Co-authored-by: fpfp100 <[email protected]>
|
@copilot fix lint error and alignment issue |
Co-authored-by: fpfp100 <[email protected]>
|
@copilot undo the changes under tests-agent/openai-agent-auto-instrument-sample/src |
Co-authored-by: fpfp100 <[email protected]>
There was a problem hiding this 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
PerRequestSpanProcessorto buffer spans per trace and export them when the request completes, using the token stored in OTel Context - Introduced
runWithExportToken()andgetExportToken()utilities for managing tokens in OpenTelemetry Context - Modified
Agent365Exporterto support both batch export (using tokenResolver) and per-request export (using token from Context) - Added environment variable
ENABLE_A365_OBSERVABILITY_PER_REQUEST_EXPORTto 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 |
packages/agents-a365-observability/src/tracing/PerRequestSpanProcessor.ts
Outdated
Show resolved
Hide resolved
packages/agents-a365-observability/src/tracing/context/token-context.ts
Outdated
Show resolved
Hide resolved
packages/agents-a365-observability/src/tracing/PerRequestSpanProcessor.ts
Show resolved
Hide resolved
…entation, fix security concern, and improve test assertions Co-authored-by: fpfp100 <[email protected]>
This PR introduces the following changes:
AGENT365_OTEL_EXPORT_TOKEN. When set totrue, token export is enabled; otherwise, the default behavior is used and no token is exported.Sample output:
when export is enabled and per request export is enabled (token failed case)

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.