Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/core/src/tracing/google-genai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,16 @@ function instrumentMethod<T extends unknown[], R>(
* Recursively instruments methods and handles special cases like chats.create
*/
function createDeepProxy<T extends object>(target: T, currentPath = '', options: GoogleGenAIOptions): T {
const propertyCache = new Map<PropertyKey, { original: unknown; wrapped: unknown }>();

return new Proxy(target, {
get: (t, prop, receiver) => {
const value = Reflect.get(t, prop, receiver);
const cachedEntry = propertyCache.get(prop);
if (cachedEntry && cachedEntry.original === value) {
return cachedEntry.wrapped;
}

const methodPath = buildMethodPath(currentPath, String(prop));

const instrumentedMethod: InstrumentedMethodEntry | undefined =
Expand All @@ -374,29 +381,36 @@ function createDeepProxy<T extends object>(target: T, currentPath = '', options:
: value.bind(t);

if (!instrumentedMethod.proxyResultPath) {
propertyCache.set(prop, { original: value, wrapped: wrappedMethod });
return wrappedMethod;
}

// If a proxyResultPath is specified, we need to proxy the result of the method.
// Note: This currently only properly handles synchronous methods. For async methods,
// the Promise itself would be proxied instead of the resolved value. Currently we
// don't have a case where this is needed, so I'll keep it simple for now.
return function (...args: unknown[]): unknown {
const methodWithProxiedResult = function (...args: unknown[]): unknown {
const result = wrappedMethod(...args);
if (result && typeof result === 'object') {
return createDeepProxy(result as object, instrumentedMethod.proxyResultPath, options);
}
return result;
};
propertyCache.set(prop, { original: value, wrapped: methodWithProxiedResult });
return methodWithProxiedResult;
}

if (typeof value === 'function') {
// Bind non-instrumented functions to preserve the original `this` context
return value.bind(t);
const boundMethod = value.bind(t);
propertyCache.set(prop, { original: value, wrapped: boundMethod });
return boundMethod;
}

if (value && typeof value === 'object') {
return createDeepProxy(value, methodPath, options);
const nestedProxy = createDeepProxy(value, methodPath, options);
propertyCache.set(prop, { original: value, wrapped: nestedProxy });
return nestedProxy;
}

return value;
Expand Down
23 changes: 18 additions & 5 deletions packages/core/src/tracing/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,30 +238,43 @@ function instrumentMethod<T extends unknown[], R>(
* Create a deep proxy for OpenAI client instrumentation
*/
function createDeepProxy<T extends object>(target: T, currentPath = '', options: OpenAiOptions): T {
const propertyCache = new Map<PropertyKey, { original: unknown; wrapped: unknown }>();

return new Proxy(target, {
get(obj: object, prop: string): unknown {
const value = (obj as Record<string, unknown>)[prop];
get(obj: object, prop: string | symbol): unknown {
const value = Reflect.get(obj, prop) as unknown;
const cachedEntry = propertyCache.get(prop);
if (cachedEntry && cachedEntry.original === value) {
return cachedEntry.wrapped;
}

const methodPath = buildMethodPath(currentPath, String(prop));

const instrumentedMethod = OPENAI_METHOD_REGISTRY[methodPath as keyof typeof OPENAI_METHOD_REGISTRY];
if (typeof value === 'function' && instrumentedMethod) {
return instrumentMethod(
const wrappedMethod = instrumentMethod(
value as (...args: unknown[]) => Promise<unknown>,
methodPath,
instrumentedMethod,
obj,
options,
);
propertyCache.set(prop, { original: value, wrapped: wrappedMethod });
return wrappedMethod;
}

if (typeof value === 'function') {
// Bind non-instrumented functions to preserve the original `this` context,
// which is required for accessing private class fields (e.g. #baseURL) in OpenAI SDK v5.
return value.bind(obj);
const boundMethod = value.bind(obj);
propertyCache.set(prop, { original: value, wrapped: boundMethod });
return boundMethod;
}

if (value && typeof value === 'object') {
return createDeepProxy(value, methodPath, options);
const nestedProxy = createDeepProxy(value, methodPath, options);
propertyCache.set(prop, { original: value, wrapped: nestedProxy });
return nestedProxy;
}

return value;
Expand Down
37 changes: 37 additions & 0 deletions packages/core/test/tracing/ai-integration-proxy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, it } from 'vitest';
import { instrumentGoogleGenAIClient } from '../../src/tracing/google-genai';
import { instrumentOpenAiClient } from '../../src/tracing/openai';

describe('AI integration deep proxies', () => {
it('returns stable references for OpenAI client properties', () => {
const client = {
chat: { completions: { create: () => Promise.resolve({}) } },
getBaseURL: () => 'https://api.openai.com',
};

const instrumentedClient = instrumentOpenAiClient(client);
const completions = instrumentedClient.chat.completions;
const create = completions.create;
const getBaseURL = instrumentedClient.getBaseURL;

expect(instrumentedClient.chat.completions).toBe(completions);
expect(instrumentedClient.chat.completions.create).toBe(create);
expect(instrumentedClient.getBaseURL).toBe(getBaseURL);
});

it('returns stable references for Google GenAI client properties', () => {
const client = {
chats: { create: () => ({ sendMessage: () => Promise.resolve({}) }) },
getVersion: () => '1.0.0',
};

const instrumentedClient = instrumentGoogleGenAIClient(client);
const chats = instrumentedClient.chats;
const create = chats.create;
const getVersion = instrumentedClient.getVersion;

expect(instrumentedClient.chats).toBe(chats);
expect(instrumentedClient.chats.create).toBe(create);
expect(instrumentedClient.getVersion).toBe(getVersion);
});
});
Loading