Skip to content

Commit 2e099c8

Browse files
author
cod1k
committed
Refactor SentryCloudflareTracer to enhance span proxying and ensure seamless integration with underlying tracer.
1 parent 765c1c8 commit 2e099c8

File tree

1 file changed

+30
-12
lines changed
  • packages/cloudflare/src/opentelemetry

1 file changed

+30
-12
lines changed

packages/cloudflare/src/opentelemetry/tracer.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,36 @@ class SentryCloudflareTraceProvider implements TracerProvider {
3131
class SentryCloudflareTracer implements Tracer {
3232
public constructor(private readonly _tracer: Tracer) {}
3333
public startSpan(name: string, options?: SpanOptions): Span {
34-
try {
35-
return startInactiveSpan({
36-
name,
37-
...options,
38-
attributes: {
39-
...options?.attributes,
40-
'sentry.cloudflare_tracer': true,
41-
},
42-
});
43-
} finally {
44-
this._tracer.startSpan(name, options);
45-
}
34+
const topSpan = this._tracer.startSpan(name, options);
35+
const sentrySpan = startInactiveSpan({
36+
name,
37+
...options,
38+
attributes: {
39+
...options?.attributes,
40+
'sentry.cloudflare_tracer': true,
41+
},
42+
});
43+
return new Proxy(sentrySpan, {
44+
get: (target, p) => {
45+
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+
});
61+
}
62+
},
63+
});
4664
}
4765

4866
/**

0 commit comments

Comments
 (0)