-
Notifications
You must be signed in to change notification settings - Fork 5
Add multi-instance SDK support via delegating providers #177
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
Open
JacksonWeber
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
JacksonWeber:feature/multi-instance-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d5dd4a7
Add multi-instance SDK support via delegating providers
JacksonWeber bda7f89
Address PR review feedback on multi-instance SDK
JacksonWeber f05c718
feat(multiInstance): bind per-instance instrumentations and A365 export
JacksonWeber a5fd4a8
Merge origin/main into feature/multi-instance-sdk
JacksonWeber 661887d
fix(multiInstance): route synchronous metric measurements by ambient …
JacksonWeber 7b93036
test(multiInstance): restore global context state after metric routin…
JacksonWeber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import type { | ||
| Tracer, | ||
| TracerProvider, | ||
| Span, | ||
| SpanOptions, | ||
| Context, | ||
| Meter, | ||
| MeterProvider, | ||
| MeterOptions, | ||
| MetricOptions, | ||
| BatchObservableCallback, | ||
| Observable, | ||
| Counter, | ||
| UpDownCounter, | ||
| Gauge, | ||
| Histogram, | ||
| ObservableGauge, | ||
| ObservableCounter, | ||
| ObservableUpDownCounter, | ||
| } from "@opentelemetry/api"; | ||
| import { createNoopMeter, ProxyTracerProvider } from "@opentelemetry/api"; | ||
| import type { Logger, LoggerProvider, LoggerOptions, LogRecord } from "@opentelemetry/api-logs"; | ||
| import { NOOP_LOGGER } from "@opentelemetry/api-logs"; | ||
|
|
||
| import { resolveInstanceProviders } from "./instanceRegistry.js"; | ||
|
|
||
| // Shared fallbacks used when no instance is registered/resolved yet. They are | ||
| // no-ops so that early or out-of-band global API access never throws. | ||
| const NOOP_TRACER_PROVIDER = new ProxyTracerProvider(); | ||
| const NOOP_METER = createNoopMeter(); | ||
|
|
||
| /** | ||
| * A Tracer that resolves the current instance's tracer on every call. Resolution | ||
| * MUST be per-call (never cached) because the ambient instance changes with the | ||
| * active context. | ||
| */ | ||
| class DelegatingTracer implements Tracer { | ||
| constructor( | ||
| private readonly name: string, | ||
| private readonly version?: string, | ||
| private readonly options?: { schemaUrl?: string }, | ||
| ) {} | ||
|
|
||
| private delegate(): Tracer { | ||
| const providers = resolveInstanceProviders(); | ||
| const provider: TracerProvider = providers?.tracerProvider ?? NOOP_TRACER_PROVIDER; | ||
| return provider.getTracer(this.name, this.version, this.options); | ||
| } | ||
|
|
||
| startSpan(name: string, options?: SpanOptions, context?: Context): Span { | ||
| return this.delegate().startSpan(name, options, context); | ||
| } | ||
|
|
||
| // The api defines several overloads for startActiveSpan; forward all args. | ||
| startActiveSpan<F extends (span: Span) => unknown>(name: string, fn: F): ReturnType<F>; | ||
| startActiveSpan<F extends (span: Span) => unknown>( | ||
| name: string, | ||
| options: SpanOptions, | ||
| fn: F, | ||
| ): ReturnType<F>; | ||
| startActiveSpan<F extends (span: Span) => unknown>( | ||
| name: string, | ||
| options: SpanOptions, | ||
| context: Context, | ||
| fn: F, | ||
| ): ReturnType<F>; | ||
| startActiveSpan(name: string, ...args: unknown[]): unknown { | ||
| return (this.delegate().startActiveSpan as (...a: unknown[]) => unknown)(name, ...args); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Global parent TracerProvider registered once. It owns no pipeline itself; it | ||
| * delegates to the resolved child instance's TracerProvider. | ||
| */ | ||
| export class ParentTracerProvider implements TracerProvider { | ||
| getTracer(name: string, version?: string, options?: { schemaUrl?: string }): Tracer { | ||
| return new DelegatingTracer(name, version, options); | ||
| } | ||
| } | ||
|
|
||
| /** A Meter that resolves the current instance's meter on every instrument call. */ | ||
| class DelegatingMeter implements Meter { | ||
| constructor( | ||
| private readonly name: string, | ||
| private readonly version?: string, | ||
| private readonly options?: MeterOptions, | ||
| ) {} | ||
|
|
||
| private delegate(): Meter { | ||
| const providers = resolveInstanceProviders(); | ||
| const provider: MeterProvider | undefined = providers?.meterProvider; | ||
| return provider ? provider.getMeter(this.name, this.version, this.options) : NOOP_METER; | ||
| } | ||
|
|
||
| createGauge(name: string, options?: MetricOptions): Gauge { | ||
| return this.delegate().createGauge(name, options); | ||
| } | ||
| createHistogram(name: string, options?: MetricOptions): Histogram { | ||
| return this.delegate().createHistogram(name, options); | ||
| } | ||
| createCounter(name: string, options?: MetricOptions): Counter { | ||
| return this.delegate().createCounter(name, options); | ||
| } | ||
| createUpDownCounter(name: string, options?: MetricOptions): UpDownCounter { | ||
| return this.delegate().createUpDownCounter(name, options); | ||
| } | ||
| createObservableGauge(name: string, options?: MetricOptions): ObservableGauge { | ||
| return this.delegate().createObservableGauge(name, options); | ||
| } | ||
| createObservableCounter(name: string, options?: MetricOptions): ObservableCounter { | ||
| return this.delegate().createObservableCounter(name, options); | ||
| } | ||
| createObservableUpDownCounter(name: string, options?: MetricOptions): ObservableUpDownCounter { | ||
| return this.delegate().createObservableUpDownCounter(name, options); | ||
| } | ||
| addBatchObservableCallback(callback: BatchObservableCallback, observables: Observable[]): void { | ||
| this.delegate().addBatchObservableCallback(callback, observables); | ||
| } | ||
| removeBatchObservableCallback( | ||
| callback: BatchObservableCallback, | ||
| observables: Observable[], | ||
| ): void { | ||
| this.delegate().removeBatchObservableCallback(callback, observables); | ||
| } | ||
| } | ||
|
|
||
| /** Global parent MeterProvider registered once; delegates to the resolved child. */ | ||
| export class ParentMeterProvider implements MeterProvider { | ||
| getMeter(name: string, version?: string, options?: MeterOptions): Meter { | ||
| return new DelegatingMeter(name, version, options); | ||
| } | ||
| } | ||
|
|
||
| /** A Logger that resolves the current instance's logger on every emit. */ | ||
| class DelegatingLogger implements Logger { | ||
| constructor( | ||
| private readonly name: string, | ||
| private readonly version?: string, | ||
| private readonly options?: LoggerOptions, | ||
| ) {} | ||
|
|
||
| private delegate(): Logger { | ||
| const providers = resolveInstanceProviders(); | ||
| return providers | ||
| ? providers.loggerProvider.getLogger(this.name, this.version, this.options) | ||
| : NOOP_LOGGER; | ||
| } | ||
|
|
||
| emit(logRecord: LogRecord): void { | ||
| this.delegate().emit(logRecord); | ||
| } | ||
|
|
||
| enabled(options?: Parameters<Logger["enabled"]>[0]): boolean { | ||
| return this.delegate().enabled(options); | ||
| } | ||
| } | ||
|
|
||
| /** Global parent LoggerProvider registered once; delegates to the resolved child. */ | ||
| export class ParentLoggerProvider implements LoggerProvider { | ||
| getLogger(name: string, version?: string, options?: LoggerOptions): Logger { | ||
| return new DelegatingLogger(name, version, options); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { context, metrics, propagation, trace } from "@opentelemetry/api"; | ||
| import { logs } from "@opentelemetry/api-logs"; | ||
| import { AsyncLocalStorageContextManager } from "@opentelemetry/context-async-hooks"; | ||
| import { | ||
| CompositePropagator, | ||
| W3CBaggagePropagator, | ||
| W3CTraceContextPropagator, | ||
| } from "@opentelemetry/core"; | ||
|
|
||
| import { | ||
| ParentLoggerProvider, | ||
| ParentMeterProvider, | ||
| ParentTracerProvider, | ||
| } from "./delegatingProviders.js"; | ||
|
|
||
| let globalSetupDone = false; | ||
|
|
||
| /** | ||
| * Register the parent (delegating) providers and the shared process-global | ||
| * context manager + propagator exactly once. Context and propagation are | ||
| * process-wide concerns shared by every instance, so they are NOT duplicated | ||
| * per instance. | ||
| * | ||
| * Idempotent: safe to call on every `useMicrosoftOpenTelemetry()` / | ||
| * `createMicrosoftOpenTelemetryInstance()` invocation. | ||
| */ | ||
| export function ensureGlobalSetup(): void { | ||
| if (globalSetupDone) { | ||
| return; | ||
| } | ||
|
|
||
| // Clear any stale OpenTelemetry API global state to avoid version conflicts | ||
| // (mirrors the cleanup performed by the single-instance distro path). | ||
| trace.disable(); | ||
| metrics.disable(); | ||
| logs.disable(); | ||
| const globalOpentelemetryApiKey = Symbol.for("opentelemetry.js.api.1"); | ||
| delete (globalThis as Record<symbol, unknown>)[globalOpentelemetryApiKey]; | ||
|
|
||
| const contextManager = new AsyncLocalStorageContextManager(); | ||
| contextManager.enable(); | ||
| context.setGlobalContextManager(contextManager); | ||
|
|
||
| propagation.setGlobalPropagator( | ||
| new CompositePropagator({ | ||
| propagators: [new W3CTraceContextPropagator(), new W3CBaggagePropagator()], | ||
| }), | ||
| ); | ||
|
|
||
| trace.setGlobalTracerProvider(new ParentTracerProvider()); | ||
| metrics.setGlobalMeterProvider(new ParentMeterProvider()); | ||
| logs.setGlobalLoggerProvider(new ParentLoggerProvider()); | ||
|
|
||
| globalSetupDone = true; | ||
| } | ||
|
|
||
| /** Test helper: allow re-running global setup. @internal */ | ||
| export function _resetGlobalSetup(): void { | ||
| globalSetupDone = false; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| export { createMicrosoftOpenTelemetryInstance } from "./instance.js"; | ||
| export { | ||
| withInstance as runWithMicrosoftOpenTelemetryInstance, | ||
| getCurrentInstanceId as _getCurrentInstanceId, | ||
| } from "./instanceRegistry.js"; |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.