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
13 changes: 9 additions & 4 deletions packages/junior/src/chat/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1835,8 +1835,9 @@ export function extractGenAiUsageAttributes(
Record<
| "gen_ai.usage.input_tokens"
| "gen_ai.usage.output_tokens"
| "gen_ai.usage.cache_read.input_tokens"
| "gen_ai.usage.cache_creation.input_tokens",
| "gen_ai.usage.total_tokens"
| "gen_ai.usage.input_tokens.cached"
| "gen_ai.usage.input_tokens.cache_write",
number
>
> {
Expand All @@ -1847,6 +1848,7 @@ export function extractGenAiUsageAttributes(
cachedInputTokens,
cacheCreationTokens,
);
const totalTokens = sumTokenCounts(semanticInputTokens, outputTokens);

return {
...(semanticInputTokens !== undefined
Expand All @@ -1855,11 +1857,14 @@ export function extractGenAiUsageAttributes(
...(outputTokens !== undefined
? { "gen_ai.usage.output_tokens": outputTokens }
: {}),
...(totalTokens !== undefined
? { "gen_ai.usage.total_tokens": totalTokens }
: {}),
...(cachedInputTokens !== undefined
? { "gen_ai.usage.cache_read.input_tokens": cachedInputTokens }
? { "gen_ai.usage.input_tokens.cached": cachedInputTokens }
: {}),
...(cacheCreationTokens !== undefined
? { "gen_ai.usage.cache_creation.input_tokens": cacheCreationTokens }
? { "gen_ai.usage.input_tokens.cache_write": cacheCreationTokens }
: {}),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe("extractGenAiUsageSummary", () => {
});
});

it("maps cache token counters to current GenAI semantic attributes", () => {
it("maps cache token counters to Sentry convention attribute names", () => {
expect(
extractGenAiUsageAttributes({
inputTokens: 10,
Expand All @@ -111,8 +111,9 @@ describe("extractGenAiUsageSummary", () => {
).toEqual({
"gen_ai.usage.input_tokens": 15,
"gen_ai.usage.output_tokens": 2,
"gen_ai.usage.cache_read.input_tokens": 4,
"gen_ai.usage.cache_creation.input_tokens": 1,
"gen_ai.usage.total_tokens": 17,
"gen_ai.usage.input_tokens.cached": 4,
"gen_ai.usage.input_tokens.cache_write": 1,
});
});
});
5 changes: 3 additions & 2 deletions specs/logging/semantics.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ This file is the canonical attribute and naming map for instrumentation in this
- `gen_ai.output.messages` (when captured)
- `gen_ai.usage.input_tokens` (when available)
- `gen_ai.usage.output_tokens` (when available)
- `gen_ai.usage.cache_read.input_tokens` (when available)
- `gen_ai.usage.cache_creation.input_tokens` (when available)
- `gen_ai.usage.total_tokens` (when available)
- `gen_ai.usage.input_tokens.cached` (when available; Sentry convention)
- `gen_ai.usage.input_tokens.cache_write` (when available; Sentry convention)
- `gen_ai.tool.description` (when available)
- `gen_ai.tool.name` (for `execute_tool`)
- `gen_ai.tool.call.id` (when available)
Expand Down
4 changes: 2 additions & 2 deletions specs/logging/tracing-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ Define the canonical tracing contract for span naming, boundaries, attributes, a
- `gen_ai.response.finish_reasons` when available from provider responses.
- `gen_ai.system_instructions` when provided separately from chat history and safely captured.
- `gen_ai.input.messages` / `gen_ai.output.messages` when safely captured.
- `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` when available from provider responses.
- `gen_ai.usage.cache_read.input_tokens` / `gen_ai.usage.cache_creation.input_tokens` when available from provider responses.
- `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` / `gen_ai.usage.total_tokens` when available from provider responses.
- `gen_ai.usage.input_tokens.cached` / `gen_ai.usage.input_tokens.cache_write` when available from provider responses (Sentry convention names; required for AI Monitoring UI visibility).
- `gen_ai.tool.description` when available on tool execution spans.
- `gen_ai.tool.call.arguments` / `gen_ai.tool.call.result` on tool execution spans when captured.
- Keep existing context keys aligned with `packages/junior/src/chat/logging.ts`.
Expand Down
Loading