Skip to content

Commit b2ac226

Browse files
author
cod1k
committed
Refactor SentryCloudflareTracer span proxying to improve performance and prevent redundant proxy creation.
1 parent 0d67dbc commit b2ac226

File tree

1 file changed

+9
-3
lines changed
  • packages/cloudflare/src/opentelemetry

1 file changed

+9
-3
lines changed

packages/cloudflare/src/opentelemetry/tracer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ class SentryCloudflareTracer implements Tracer {
4747
if (!topSpan) {
4848
return sentrySpan;
4949
}
50+
const _proxied = new WeakMap<CallableFunction, CallableFunction>();
5051
return new Proxy(sentrySpan, {
5152
set: (target, p, newValue, receiver) => {
5253
try {
53-
Reflect.set(topSpan, p, newValue, receiver);
54+
Reflect.set(topSpan, p, newValue);
5455
} catch {
5556
//
5657
}
57-
return Reflect.set(target, p, newValue);
58+
return Reflect.set(target, p, newValue, receiver);
5859
},
5960
get: (target, p) => {
6061
const propertyValue = Reflect.get(target, p);
@@ -65,7 +66,10 @@ class SentryCloudflareTracer implements Tracer {
6566
if (typeof proxyTo !== 'function') {
6667
return propertyValue;
6768
}
68-
return new Proxy(propertyValue, {
69+
if (_proxied.has(propertyValue)) {
70+
return _proxied.get(propertyValue);
71+
}
72+
const proxy = new Proxy(propertyValue, {
6973
apply: (target, thisArg, argArray) => {
7074
try {
7175
Reflect.apply(proxyTo, topSpan, argArray);
@@ -75,6 +79,8 @@ class SentryCloudflareTracer implements Tracer {
7579
return Reflect.apply(target, thisArg, argArray);
7680
},
7781
});
82+
_proxied.set(propertyValue, proxy);
83+
return proxy;
7884
},
7985
});
8086
}

0 commit comments

Comments
 (0)