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
6 changes: 3 additions & 3 deletions e2e/helpers/openai-scenario.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ export async function runOpenAIScenario(options) {
],
max_tokens: 16,
temperature: 0,
stream_options: {
include_usage: true,
},
};

if (options.useSyncStreamHelper === false) {
const stream = await client.chat.completions.create({
...streamArgs,
stream: true,
stream_options: {
include_usage: true,
},
});
await collectAsync(stream);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,7 @@
"scenario": null
},
"metric_keys": [
"completion_accepted_prediction_tokens",
"completion_audio_tokens",
"completion_reasoning_tokens",
"completion_rejected_prediction_tokens",
"completion_tokens",
"prompt_audio_tokens",
"prompt_cached_tokens",
"prompt_tokens",
"time_to_first_token",
"tokens"
Expand Down Expand Up @@ -646,7 +640,7 @@
"time_to_first_token",
"tokens"
],
"name": "openai.responses.create",
"name": "openai.responses.parse",
"root_span_id": "<span:1>",
"span_id": "<span:29>",
"span_parents": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,7 @@
"scenario": null
},
"metric_keys": [
"completion_accepted_prediction_tokens",
"completion_audio_tokens",
"completion_reasoning_tokens",
"completion_rejected_prediction_tokens",
"completion_tokens",
"prompt_audio_tokens",
"prompt_cached_tokens",
"prompt_tokens",
"time_to_first_token",
"tokens"
Expand Down Expand Up @@ -646,7 +640,7 @@
"time_to_first_token",
"tokens"
],
"name": "openai.responses.create",
"name": "openai.responses.parse",
"root_span_id": "<span:1>",
"span_id": "<span:29>",
"span_parents": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,7 @@
"scenario": null
},
"metric_keys": [
"completion_accepted_prediction_tokens",
"completion_audio_tokens",
"completion_reasoning_tokens",
"completion_rejected_prediction_tokens",
"completion_tokens",
"prompt_audio_tokens",
"prompt_cached_tokens",
"prompt_tokens",
"time_to_first_token",
"tokens"
Expand Down Expand Up @@ -646,7 +640,7 @@
"time_to_first_token",
"tokens"
],
"name": "openai.responses.create",
"name": "openai.responses.parse",
"root_span_id": "<span:1>",
"span_id": "<span:29>",
"span_parents": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ export async function runOpenAIAutoInstrumentationNodeHook(
projectNameBase: "e2e-openai-auto-instrumentation-hook",
rootName: "openai-auto-hook-root",
scenarioName: "openai-auto-instrumentation-node-hook",
useChatParseHelper: false,
useResponsesParseHelper: false,
useSyncStreamHelper: false,
});
}

Expand Down
18 changes: 9 additions & 9 deletions js/src/auto-instrumentations/configs/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const openaiConfigs: InstrumentationConfig[] = [
functionQuery: {
className: "Completions",
methodName: "create",
kind: "Async",
kind: "Sync",
},
},

Expand All @@ -38,7 +38,7 @@ export const openaiConfigs: InstrumentationConfig[] = [
functionQuery: {
className: "Completions",
methodName: "create",
kind: "Async",
kind: "Sync",
},
},

Expand All @@ -52,7 +52,7 @@ export const openaiConfigs: InstrumentationConfig[] = [
functionQuery: {
className: "Completions",
methodName: "create",
kind: "Async",
kind: "Sync",
},
},

Expand All @@ -67,7 +67,7 @@ export const openaiConfigs: InstrumentationConfig[] = [
functionQuery: {
className: "Embeddings",
methodName: "create",
kind: "Async",
kind: "Sync",
},
},

Expand All @@ -82,7 +82,7 @@ export const openaiConfigs: InstrumentationConfig[] = [
functionQuery: {
className: "Completions",
methodName: "parse",
kind: "Async",
kind: "Sync",
},
},

Expand All @@ -96,7 +96,7 @@ export const openaiConfigs: InstrumentationConfig[] = [
functionQuery: {
className: "Completions",
methodName: "parse",
kind: "Async",
kind: "Sync",
},
},

Expand All @@ -111,7 +111,7 @@ export const openaiConfigs: InstrumentationConfig[] = [
functionQuery: {
className: "Moderations",
methodName: "create",
kind: "Async",
kind: "Sync",
},
},

Expand Down Expand Up @@ -155,7 +155,7 @@ export const openaiConfigs: InstrumentationConfig[] = [
functionQuery: {
className: "Responses",
methodName: "create",
kind: "Async",
kind: "Sync",
},
},

Expand Down Expand Up @@ -183,7 +183,7 @@ export const openaiConfigs: InstrumentationConfig[] = [
functionQuery: {
className: "Responses",
methodName: "parse",
kind: "Async",
kind: "Sync",
},
},
];
66 changes: 65 additions & 1 deletion js/src/instrumentation/core/channel-tracing.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EventEmitter } from "node:events";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import {
_exportsForTestingOnly,
Expand All @@ -8,13 +9,17 @@ import {
} from "../../logger";
import { configureNode } from "../../node/config";
import { channel, defineChannels } from "./channel-definitions";
import { traceAsyncChannel } from "./channel-tracing";
import { traceAsyncChannel, traceSyncStreamChannel } from "./channel-tracing";

const testChannels = defineChannels("channel-tracing-test", {
asyncCall: channel<[Record<string, never>], { ok: true }>({
channelName: "async.call",
kind: "async",
}),
syncStreamCall: channel<[Record<string, never>], EventEmitter>({
channelName: "sync-stream.call",
kind: "sync-stream",
}),
});

describe("traceAsyncChannel current span binding", () => {
Expand Down Expand Up @@ -74,4 +79,63 @@ describe("traceAsyncChannel current span binding", () => {
const spans = await backgroundLogger.drain();
expect(spans).toHaveLength(1);
});

it("logs totalUsage metrics for sync streams", async () => {
const unsubscribe = traceSyncStreamChannel(testChannels.syncStreamCall, {
name: "channel-tracing-sync-stream",
type: "llm",
extractInput: () => ({
input: "input",
metadata: { provider: "openai" },
}),
extractFromTotalUsage: () => ({
metrics: {
prompt_tokens: 3,
completion_tokens: 2,
tokens: 5,
},
}),
});

try {
const stream = new EventEmitter();
const result = testChannels.syncStreamCall.traceSync(() => stream, {
arguments: [{}],
} as any);

expect(result).toBe(stream);

stream.emit("chunk", { delta: { content: "hello" } });
stream.emit("chatCompletion", {
choices: [{ message: { role: "assistant", content: "hello world" } }],
});
stream.emit("totalUsage", {
prompt_tokens: 3,
completion_tokens: 2,
total_tokens: 5,
});
stream.emit("end");

await new Promise((resolve) => setImmediate(resolve));
} finally {
unsubscribe();
}

const spans = await backgroundLogger.drain();
expect(spans).toHaveLength(1);
expect(spans[0]).toMatchObject({
metadata: {
provider: "openai",
},
output: [{ message: { role: "assistant", content: "hello world" } }],
metrics: expect.objectContaining({
prompt_tokens: 3,
completion_tokens: 2,
tokens: 5,
}),
});
expect(
(spans[0] as Record<string, any>).metrics.time_to_first_token,
).toEqual(expect.any(Number));
});
});
Loading
Loading