Add per-request export with zero-storage token management via OpenTelemetry Context #146
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.
Implements toggleable per-request span export using OpenTelemetry Context (AsyncLocalStorage) to carry authentication tokens, eliminating the need for token registries or caches.
Changes
Agent365ExporterOptions: Added
enablePerRequestContextTokentoggle and configuration for flush grace period (perRequestFlushGraceMs) and max trace age (perRequestMaxTraceAgeMs)token-context.ts: Context utilities
runWithExportToken()andgetExportToken()for token management via OTel Context APIPerRequestSpanProcessor: Custom span processor that buffers spans per trace, flushes on root span completion, and exports under the original request Context so
tokenResolverreads fromcontext.active()withExportToken: Middleware helper to wrap requests with token-carrying Context
setup-per-request-export.ts: Initialization helper that registers
PerRequestSpanProcessorwhen enabled, otherwise usesBatchSpanProcessorUsage
No changes to
Agent365Exporter- it uses the providedtokenResolverregardless of mode. When per-request mode is enabled, the processor stores only spans and a Context reference per trace; tokens exist solely in AsyncLocalStorage.Original prompt
Add a toggleable, zero-storage per-request export feature using OpenTelemetry Context (AsyncLocalStorage) to carry the token. When enabled, spans are exported per trace/request via a custom PerRequestSpanProcessor, and Agent365Exporter reads the token from the active OTel Context at export time (no registry/cache). When disabled, retain existing behavior using BatchSpanProcessor with the provided tokenResolver.
Changes to implement:
enablePerRequestContextToken: booleanto Agent365ExporterOptions to toggle the feature.token-context.tsutilities to set/get a per-request export token via OTel Context.PerRequestSpanProcessorthat buffers spans per trace and flushes when the root span ends (with a grace period); on flush, export is performed under the saved request Context so the exporter’s tokenResolver can read the token from context.active().withExportTokenmiddleware helper to run each request within a Context that carries the token. No explicit unset required; Context ends with request.setup-per-request-export.tshelper to initialize tracing: when the new option is true, register PerRequestSpanProcessor and set Agent365ExporterOptions.tokenResolver to read from OTel Context; otherwise register BatchSpanProcessor using existing batching options.tokenResolverat export time.Notes:
withExportTokenearly in the request pipeline so the server span is created inside the token-carrying Context.Proposed files and modifications: