Skip to content

Commit b3c8f6c

Browse files
committed
proxy stream method
1 parent 061ff71 commit b3c8f6c

File tree

1 file changed

+62
-62
lines changed
  • packages/core/src/utils/google-genai

1 file changed

+62
-62
lines changed

packages/core/src/utils/google-genai/index.ts

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -222,75 +222,75 @@ function instrumentMethod<T extends unknown[], R>(
222222
): (...args: T) => R | Promise<R> {
223223
const isSyncCreate = methodPath === CHATS_CREATE_METHOD;
224224

225-
const run = (...args: T): R | Promise<R> => {
226-
const params = args[0] as Record<string, unknown> | undefined;
227-
const requestAttributes = extractRequestAttributes(methodPath, params, context);
228-
const model = requestAttributes[GEN_AI_REQUEST_MODEL_ATTRIBUTE] ?? 'unknown';
229-
const operationName = getFinalOperationName(methodPath);
230-
231-
// Check if this is a streaming method
232-
if (isStreamingMethod(methodPath)) {
233-
// Use startSpanManual for streaming methods to control span lifecycle
234-
return startSpanManual(
225+
return new Proxy(originalMethod, {
226+
apply(target, _, args: T): R | Promise<R> {
227+
const params = args[0] as Record<string, unknown> | undefined;
228+
const requestAttributes = extractRequestAttributes(methodPath, params, context);
229+
const model = requestAttributes[GEN_AI_REQUEST_MODEL_ATTRIBUTE] ?? 'unknown';
230+
const operationName = getFinalOperationName(methodPath);
231+
232+
// Check if this is a streaming method
233+
if (isStreamingMethod(methodPath)) {
234+
// Use startSpanManual for streaming methods to control span lifecycle
235+
return startSpanManual(
236+
{
237+
name: `${operationName} ${model} stream-response`,
238+
op: getSpanOperation(methodPath),
239+
attributes: requestAttributes,
240+
},
241+
async (span: Span) => {
242+
try {
243+
if (options.recordInputs && params) {
244+
addPrivateRequestAttributes(span, params);
245+
}
246+
const stream = await target.apply(context, args);
247+
return instrumentStream(stream, span, Boolean(options.recordOutputs)) as R;
248+
} catch (error) {
249+
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
250+
captureException(error, {
251+
mechanism: {
252+
handled: false,
253+
type: 'auto.ai.google_genai',
254+
data: { function: methodPath },
255+
},
256+
});
257+
span.end();
258+
throw error;
259+
}
260+
},
261+
);
262+
}
263+
// Single span for both sync and async operations
264+
return startSpan(
235265
{
236-
name: `${operationName} ${model} stream-response`,
266+
name: isSyncCreate ? `${operationName} ${model} create` : `${operationName} ${model}`,
237267
op: getSpanOperation(methodPath),
238268
attributes: requestAttributes,
239269
},
240-
async (span: Span) => {
241-
try {
242-
if (options.recordInputs && params) {
243-
addPrivateRequestAttributes(span, params);
244-
}
245-
const stream = await originalMethod.apply(context, args);
246-
return instrumentStream(stream, span, Boolean(options.recordOutputs)) as R;
247-
} catch (error) {
248-
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
249-
captureException(error, {
250-
mechanism: {
251-
handled: false,
252-
type: 'auto.ai.google_genai',
253-
data: { function: methodPath },
254-
},
255-
});
256-
span.end();
257-
throw error;
270+
(span: Span) => {
271+
if (options.recordInputs && params) {
272+
addPrivateRequestAttributes(span, params);
258273
}
274+
275+
return handleCallbackErrors(
276+
() => target.apply(context, args),
277+
error => {
278+
captureException(error, {
279+
mechanism: { handled: false, type: 'auto.ai.google_genai', data: { function: methodPath } },
280+
});
281+
},
282+
() => {},
283+
result => {
284+
// Only add response attributes for content-producing methods, not for chats.create
285+
if (!isSyncCreate) {
286+
addResponseAttributes(span, result, options.recordOutputs);
287+
}
288+
},
289+
);
259290
},
260291
);
261-
}
262-
// Single span for both sync and async operations
263-
return startSpan(
264-
{
265-
name: isSyncCreate ? `${operationName} ${model} create` : `${operationName} ${model}`,
266-
op: getSpanOperation(methodPath),
267-
attributes: requestAttributes,
268-
},
269-
(span: Span) => {
270-
if (options.recordInputs && params) {
271-
addPrivateRequestAttributes(span, params);
272-
}
273-
274-
return handleCallbackErrors(
275-
() => originalMethod.apply(context, args),
276-
error => {
277-
captureException(error, {
278-
mechanism: { handled: false, type: 'auto.ai.google_genai', data: { function: methodPath } },
279-
});
280-
},
281-
() => {},
282-
result => {
283-
// Only add response attributes for content-producing methods, not for chats.create
284-
if (!isSyncCreate) {
285-
addResponseAttributes(span, result, options.recordOutputs);
286-
}
287-
},
288-
);
289-
},
290-
);
291-
};
292-
293-
return run;
292+
},
293+
}) as (...args: T) => R | Promise<R>;
294294
}
295295

296296
/**

0 commit comments

Comments
 (0)