Skip to content

Commit 5a71f3b

Browse files
author
cod1k
committed
Improve setupOpenTelemetryTracer to support ProxyTracerProvider and refactor span proxy logic in SentryCloudflareTraceProvider.
1 parent 2e099c8 commit 5a71f3b

File tree

1 file changed

+19
-17
lines changed
  • packages/cloudflare/src/opentelemetry

1 file changed

+19
-17
lines changed

packages/cloudflare/src/opentelemetry/tracer.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Context, Span, SpanOptions, Tracer, TracerProvider } from '@opentelemetry/api';
2-
import { trace } from '@opentelemetry/api';
2+
import { ProxyTracerProvider, trace } from '@opentelemetry/api';
33
import { startInactiveSpan, startSpanManual } from '@sentry/core';
44

55
/**
@@ -8,7 +8,8 @@ import { startInactiveSpan, startSpanManual } from '@sentry/core';
88
*/
99
export function setupOpenTelemetryTracer(): void {
1010
const current = trace.getTracerProvider();
11-
trace.setGlobalTracerProvider(new SentryCloudflareTraceProvider(current));
11+
const delegate = current instanceof ProxyTracerProvider ? current.getDelegate() : current;
12+
trace.setGlobalTracerProvider(new SentryCloudflareTraceProvider(delegate));
1213
}
1314

1415
class SentryCloudflareTraceProvider implements TracerProvider {
@@ -43,22 +44,23 @@ class SentryCloudflareTracer implements Tracer {
4344
return new Proxy(sentrySpan, {
4445
get: (target, p) => {
4546
const propertyValue = Reflect.get(target, p);
46-
if (typeof propertyValue === 'function') {
47-
const proxyTo = Reflect.get(topSpan, p);
48-
if (typeof proxyTo !== 'function') {
49-
return propertyValue;
50-
}
51-
return new Proxy(propertyValue, {
52-
apply: (target, thisArg, argArray) => {
53-
try {
54-
Reflect.apply(proxyTo, topSpan, argArray);
55-
} catch (e) {
56-
//
57-
}
58-
return Reflect.apply(target, thisArg, argArray);
59-
},
60-
});
47+
if (typeof propertyValue !== 'function') {
48+
return propertyValue;
6149
}
50+
const proxyTo = Reflect.get(topSpan, p);
51+
if (typeof proxyTo !== 'function') {
52+
return propertyValue;
53+
}
54+
return new Proxy(propertyValue, {
55+
apply: (target, thisArg, argArray) => {
56+
try {
57+
Reflect.apply(proxyTo, topSpan, argArray);
58+
} catch (e) {
59+
//
60+
}
61+
return Reflect.apply(target, thisArg, argArray);
62+
},
63+
});
6264
},
6365
});
6466
}

0 commit comments

Comments
 (0)