Skip to content

Commit bd0b101

Browse files
committed
use replace exports utility
1 parent 4be2e67 commit bd0b101

File tree

3 files changed

+7
-61
lines changed

3 files changed

+7
-61
lines changed

packages/node/src/integrations/tracing/anthropic-ai/instrumentation.ts

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
_INTERNAL_shouldSkipAiProviderWrapping,
1010
ANTHROPIC_AI_INTEGRATION_NAME,
1111
instrumentAnthropicAiClient,
12+
replaceExports,
1213
SDK_VERSION,
1314
} from '@sentry/core';
1415

@@ -76,36 +77,8 @@ export class SentryAnthropicAiInstrumentation extends InstrumentationBase<Anthro
7677
}
7778
}
7879

79-
// Constructor replacement - handle read-only properties
80-
// The Anthropic property might have only a getter, so use defineProperty
81-
try {
82-
exports.Anthropic = WrappedAnthropic;
83-
} catch {
84-
// If direct assignment fails, override the property descriptor
85-
Object.defineProperty(exports, 'Anthropic', {
86-
value: WrappedAnthropic,
87-
writable: true,
88-
configurable: true,
89-
enumerable: true,
90-
});
91-
}
92-
93-
// Wrap the default export if it points to the original constructor
94-
// Constructor replacement - handle read-only properties
95-
// The Anthropic property might have only a getter, so use defineProperty
96-
if (exports.default === Original) {
97-
try {
98-
exports.default = WrappedAnthropic;
99-
} catch {
100-
// If direct assignment fails, override the property descriptor
101-
Object.defineProperty(exports, 'default', {
102-
value: WrappedAnthropic,
103-
writable: true,
104-
configurable: true,
105-
enumerable: true,
106-
});
107-
}
108-
}
80+
// Replace exports with the wrapped constructor
81+
replaceExports(exports, 'Anthropic', WrappedAnthropic);
10982
return exports;
11083
}
11184
}

packages/node/src/integrations/tracing/google-genai/instrumentation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class SentryGoogleGenAiInstrumentation extends InstrumentationBase<Google
9393
}
9494
}
9595

96-
// Replace google genai exports with the wrapped constructor
96+
// Replace exports with the wrapped constructor
9797
replaceExports(exports, 'GoogleGenAI', WrappedGoogleGenAI);
9898

9999
return exports;

packages/node/src/integrations/tracing/openai/instrumentation.ts

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
_INTERNAL_shouldSkipAiProviderWrapping,
1010
instrumentOpenAiClient,
1111
OPENAI_INTEGRATION_NAME,
12+
replaceExports,
1213
SDK_VERSION,
1314
} from '@sentry/core';
1415

@@ -90,36 +91,8 @@ export class SentryOpenAiInstrumentation extends InstrumentationBase<OpenAiInstr
9091
}
9192
}
9293

93-
// Constructor replacement - handle read-only properties
94-
// The OpenAI property might have only a getter, so use defineProperty
95-
try {
96-
exports[exportKey] = WrappedOpenAI;
97-
} catch {
98-
// If direct assignment fails, override the property descriptor
99-
Object.defineProperty(exports, exportKey, {
100-
value: WrappedOpenAI,
101-
writable: true,
102-
configurable: true,
103-
enumerable: true,
104-
});
105-
}
106-
107-
// Wrap the default export if it points to the original constructor
108-
// Constructor replacement - handle read-only properties
109-
// The OpenAI property might have only a getter, so use defineProperty
110-
if (exports.default === Original) {
111-
try {
112-
exports.default = WrappedOpenAI;
113-
} catch {
114-
// If direct assignment fails, override the property descriptor
115-
Object.defineProperty(exports, 'default', {
116-
value: WrappedOpenAI,
117-
writable: true,
118-
configurable: true,
119-
enumerable: true,
120-
});
121-
}
122-
}
94+
// Replace exports with the wrapped constructor
95+
replaceExports(exports, exportKey, WrappedOpenAI);
12396
return exports;
12497
}
12598
}

0 commit comments

Comments
 (0)