Skip to content

Commit a26d4ea

Browse files
committed
fix span processor interface
1 parent 9cb9567 commit a26d4ea

1 file changed

Lines changed: 38 additions & 13 deletions

File tree

packages/instrumentation/src/browser-document-url/instrumentation.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,24 @@ import type { BrowserDocumentUrlInstrumentationConfig } from './types.ts';
1313

1414
/**
1515
* Duck-typed interface for TracerProviders that support adding span processors
16-
* at runtime (e.g. `BasicTracerProvider` / `WebTracerProvider` from the SDK).
16+
* at runtime (SDK v1.x `BasicTracerProvider` had a public `addSpanProcessor`
17+
* method; v2.x removed it).
1718
*/
18-
interface TracerProviderWithSpanProcessor {
19+
interface TracerProviderV1 {
1920
addSpanProcessor?(processor: SpanProcessorLike): void;
2021
}
2122

23+
/**
24+
* Duck-typed interface for SDK v2.x `BasicTracerProvider`, which stores
25+
* processors inside `_activeSpanProcessor` (a `MultiSpanProcessor` whose
26+
* `_spanProcessors` array is iterated on every `onStart` call).
27+
*/
28+
interface TracerProviderV2 {
29+
_activeSpanProcessor?: {
30+
_spanProcessors?: SpanProcessorLike[];
31+
};
32+
}
33+
2234
/**
2335
* Duck-typed interface for TracerProvider proxies that wrap a delegate
2436
* (e.g. `ProxyTracerProvider` from `@opentelemetry/api`).
@@ -140,9 +152,12 @@ export class BrowserDocumentUrlInstrumentation
140152
* Called by `registerInstrumentations()` with the active tracer provider.
141153
*
142154
* Unwraps `ProxyTracerProvider` (which `@opentelemetry/api` uses as the
143-
* global provider) to reach the real SDK provider, then calls
144-
* `addSpanProcessor(this)` via duck-typing if the method is available (e.g.
145-
* on `WebTracerProvider` / `BasicTracerProvider`).
155+
* global provider) to reach the real SDK provider, then injects `this` as a
156+
* span processor using whichever hook is available:
157+
*
158+
* - SDK v1.x: `addSpanProcessor()` (public method, removed in v2).
159+
* - SDK v2.x: push directly into `_activeSpanProcessor._spanProcessors`,
160+
* the array that `MultiSpanProcessor.onStart` iterates at call time.
146161
*/
147162
override setTracerProvider(tracerProvider: TracerProvider): void {
148163
super.setTracerProvider(tracerProvider);
@@ -154,15 +169,25 @@ export class BrowserDocumentUrlInstrumentation
154169
realProvider = asProxy.getDelegate();
155170
}
156171

157-
const asRegistrar = realProvider as TracerProviderWithSpanProcessor;
158-
if (typeof asRegistrar.addSpanProcessor === 'function') {
159-
asRegistrar.addSpanProcessor(this);
160-
} else {
161-
this._diag.debug(
162-
'BrowserDocumentUrlInstrumentation: TracerProvider does not support ' +
163-
'addSpanProcessor — span attribute will not be set automatically.',
164-
);
172+
// SDK v1.x path
173+
const asV1 = realProvider as TracerProviderV1;
174+
if (typeof asV1.addSpanProcessor === 'function') {
175+
asV1.addSpanProcessor(this);
176+
return;
177+
}
178+
179+
// SDK v2.x path — inject into MultiSpanProcessor's processor list
180+
const asV2 = realProvider as TracerProviderV2;
181+
const spanProcessors = asV2._activeSpanProcessor?._spanProcessors;
182+
if (spanProcessors && !spanProcessors.includes(this)) {
183+
spanProcessors.push(this);
184+
return;
165185
}
186+
187+
this._diag.debug(
188+
'BrowserDocumentUrlInstrumentation: TracerProvider does not support ' +
189+
'span processor injection — span attribute will not be set automatically.',
190+
);
166191
}
167192

168193
// -------------------------------------------------------------------------

0 commit comments

Comments
 (0)