Skip to content

Commit 4f540e5

Browse files
committed
fix it for ai
1 parent d0af51d commit 4f540e5

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

packages/node/src/integrations/tracing/vercelai/index.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
/* eslint-disable complexity */
22
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, defineIntegration, spanToJSON } from '@sentry/core';
33
import type { IntegrationFn } from '@sentry/core';
4-
import { generateInstrumentOnce, instrumentWhenWrapped } from '../../../otel/instrument';
4+
import { generateInstrumentOnce } from '../../../otel/instrument';
55
import { addOriginToSpan } from '../../../utils/addOriginToSpan';
6-
import { SentryVercelAiInstrumentation, sentryVercelAiPatched } from './instrumentation';
6+
import { SentryVercelAiInstrumentation } from './instrumentation';
77

88
const INTEGRATION_NAME = 'VercelAI';
99

1010
export const instrumentVercelAi = generateInstrumentOnce(INTEGRATION_NAME, () => new SentryVercelAiInstrumentation({}));
1111

1212
const _vercelAIIntegration = (() => {
13-
let instrumentationWrappedCallback: undefined | ((callback: () => void) => void);
13+
let instrumentation: undefined | SentryVercelAiInstrumentation;
1414

1515
return {
1616
name: INTEGRATION_NAME,
1717
setupOnce() {
18-
const instrumentation = instrumentVercelAi();
19-
instrumentationWrappedCallback = instrumentWhenWrapped(instrumentation);
18+
instrumentation = instrumentVercelAi();
2019
},
2120
setup(client) {
22-
instrumentationWrappedCallback?.(() => {
21+
instrumentation?.callWhenPatched(() => {
2322
client.on('spanStart', span => {
24-
if (!sentryVercelAiPatched) {
25-
return;
26-
}
27-
2823
const { data: attributes, description: name } = spanToJSON(span);
2924

3025
if (!name) {

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ type MethodArgs = [MethodFirstArg, ...unknown[]];
2323
type PatchedModuleExports = Record<(typeof INSTRUMENTED_METHODS)[number], (...args: MethodArgs) => unknown> &
2424
Record<string, unknown>;
2525

26-
export let sentryVercelAiPatched = false;
27-
2826
/**
2927
* This detects is added by the Sentry Vercel AI Integration to detect if the integration should
3028
* be enabled.
3129
*
3230
* It also patches the `ai` module to enable Vercel AI telemetry automatically for all methods.
3331
*/
3432
export class SentryVercelAiInstrumentation extends InstrumentationBase {
33+
private _isPatched = false;
34+
private _callbacks: (() => void)[] = [];
35+
3536
public constructor(config: InstrumentationConfig = {}) {
3637
super('@sentry/instrumentation-vercel-ai', SDK_VERSION, config);
3738
}
@@ -44,11 +45,26 @@ export class SentryVercelAiInstrumentation extends InstrumentationBase {
4445
return module;
4546
}
4647

48+
/**
49+
* Call the provided callback when the module is patched.
50+
* If it has already been patched, the callback will be called immediately.
51+
*/
52+
public callWhenPatched(callback: () => void): void {
53+
if (this._isPatched) {
54+
callback();
55+
} else {
56+
this._callbacks.push(callback);
57+
}
58+
}
59+
4760
/**
4861
* Patches module exports to enable Vercel AI telemetry.
4962
*/
5063
private _patch(moduleExports: PatchedModuleExports): unknown {
51-
sentryVercelAiPatched = true;
64+
this._isPatched = true;
65+
66+
this._callbacks.forEach(callback => callback());
67+
this._callbacks = [];
5268

5369
function generatePatch(name: string) {
5470
return (...args: MethodArgs) => {

0 commit comments

Comments
 (0)