Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

feat(typescript-sdk): align trace attributes with Python SDK for all instrumentation providers (#1026)

Aman Agarwal8e85609e0629
diff --git a/sdk/typescript/package-lock.json b/sdk/typescript/package-lock.json
index 87c08128..826f077c 100644
--- a/sdk/typescript/package-lock.json
+++ b/sdk/typescript/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "openlit",
- "version": "1.9.0",
+ "version": "1.9.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "openlit",
- "version": "1.9.0",
+ "version": "1.9.1",
"license": "ISC",
"dependencies": {
"@anthropic-ai/sdk": "^0.25.0",
diff --git a/sdk/typescript/package.json b/sdk/typescript/package.json
index 2ab3e42c..f7d6c327 100644
--- a/sdk/typescript/package.json
+++ b/sdk/typescript/package.json
@@ -1,6 +1,6 @@
{
"name": "openlit",
- "version": "1.9.0",
+ "version": "1.9.1",
"homepage": "https://github.com/openlit/openlit#readme",
"bugs": {
"url": "https://github.com/openlit/openlit/issues",
diff --git a/sdk/typescript/src/constant.ts b/sdk/typescript/src/constant.ts
index 5a26eb8f..fe977c99 100644
--- a/sdk/typescript/src/constant.ts
+++ b/sdk/typescript/src/constant.ts
@@ -1,4 +1,5 @@
export const SDK_NAME = 'openlit';
+export const SDK_VERSION = '1.9.1';
export const DEFAULT_ENVIRONMENT = 'default';
export const DEFAULT_APPLICATION_NAME = 'default';
export const INSTRUMENTATION_PREFIX = '@openlit';
diff --git a/sdk/typescript/src/instrumentation/__tests__/anthropic-wrapper.test.ts b/sdk/typescript/src/instrumentation/__tests__/anthropic-wrapper.test.ts
index 02bf6cf3..b948a48d 100644
--- a/sdk/typescript/src/instrumentation/__tests__/anthropic-wrapper.test.ts
+++ b/sdk/typescript/src/instrumentation/__tests__/anthropic-wrapper.test.ts
@@ -47,6 +47,8 @@ describe('AnthropicWrapper', () => {
user: 'test-user',
cost: 0.5,
aiSystem: 'anthropic',
+ serverAddress: 'api.anthropic.com',
+ serverPort: 443,
};
});
@@ -63,6 +65,8 @@ describe('AnthropicWrapper', () => {
cost: 0.5,
aiSystem: 'anthropic',
genAIEndpoint: 'anthropic.endpoint',
+ serverAddress: 'api.anthropic.com',
+ serverPort: 443,
});
});
});
diff --git a/sdk/typescript/src/instrumentation/__tests__/google-ai-trace-comparison.test.ts b/sdk/typescript/src/instrumentation/__tests__/google-ai-trace-comparison.test.ts
index e7275763..0f539bad 100644
--- a/sdk/typescript/src/instrumentation/__tests__/google-ai-trace-comparison.test.ts
+++ b/sdk/typescript/src/instrumentation/__tests__/google-ai-trace-comparison.test.ts
@@ -45,6 +45,13 @@ describe('Google AI Studio Cross-Language Trace Comparison', () => {
if (attrs.cost !== undefined) {
span.setAttribute(SemanticConvention.GEN_AI_USAGE_COST, attrs.cost);
}
+ if (attrs.serverAddress) {
+ span.setAttribute(SemanticConvention.SERVER_ADDRESS, attrs.serverAddress);
+ }
+ if (attrs.serverPort !== undefined) {
+ span.setAttribute(SemanticConvention.SERVER_PORT, attrs.serverPort);
+ }
+ span.setAttribute(SemanticConvention.GEN_AI_SDK_VERSION, '1.9.0');
});
});
@@ -100,6 +107,10 @@ describe('Google AI Studio Cross-Language Trace Comparison', () => {
expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, 5);
expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, 10);
expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS, 15);
+ // Python SDK parity: server.address, server.port, gen_ai.sdk.version
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.SERVER_ADDRESS, 'generativelanguage.googleapis.com');
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.SERVER_PORT, 443);
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_SDK_VERSION, '1.9.0');
});
});
});
diff --git a/sdk/typescript/src/instrumentation/__tests__/groq-trace-comparison.test.ts b/sdk/typescript/src/instrumentation/__tests__/groq-trace-comparison.test.ts
index 84f8e5c6..f2118d11 100644
--- a/sdk/typescript/src/instrumentation/__tests__/groq-trace-comparison.test.ts
+++ b/sdk/typescript/src/instrumentation/__tests__/groq-trace-comparison.test.ts
@@ -61,6 +61,13 @@ describe('Groq Cross-Language Trace Comparison', () => {
if (attrs.cost !== undefined) {
span.setAttribute(SemanticConvention.GEN_AI_USAGE_COST, attrs.cost);
}
+ if (attrs.serverAddress) {
+ span.setAttribute(SemanticConvention.SERVER_ADDRESS, attrs.serverAddress);
+ }
+ if (attrs.serverPort !== undefined) {
+ span.setAttribute(SemanticConvention.SERVER_PORT, attrs.serverPort);
+ }
+ span.setAttribute(SemanticConvention.GEN_AI_SDK_VERSION, '1.9.0');
});
});
@@ -119,6 +126,10 @@ describe('Groq Cross-Language Trace Comparison', () => {
expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, 100);
expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, false);
expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, ['stop']);
+ // Python SDK parity: server.address, server.port, gen_ai.sdk.version
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.SERVER_ADDRESS, 'api.groq.com');
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.SERVER_PORT, 443);
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_SDK_VERSION, '1.9.0');
});
it('should set streaming attributes matching Python SDK', async () => {
diff --git a/sdk/typescript/src/instrumentation/__tests__/mistral-trace-comparison.test.ts b/sdk/typescript/src/instrumentation/__tests__/mistral-trace-comparison.test.ts
index 174b33ef..03d329f2 100644
--- a/sdk/typescript/src/instrumentation/__tests__/mistral-trace-comparison.test.ts
+++ b/sdk/typescript/src/instrumentation/__tests__/mistral-trace-comparison.test.ts
@@ -47,6 +47,13 @@ describe('Mistral Cross-Language Trace Comparison', () => {
if (attrs.cost !== undefined) {
span.setAttribute(SemanticConvention.GEN_AI_USAGE_COST, attrs.cost);
}
+ if (attrs.serverAddress) {
+ span.setAttribute(SemanticConvention.SERVER_ADDRESS, attrs.serverAddress);
+ }
+ if (attrs.serverPort !== undefined) {
+ span.setAttribute(SemanticConvention.SERVER_PORT, attrs.serverPort);
+ }
+ span.setAttribute(SemanticConvention.GEN_AI_SDK_VERSION, '1.9.0');
});
});
@@ -99,6 +106,10 @@ describe('Mistral Cross-Language Trace Comparison', () => {
expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, 8);
expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, 15);
expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS, 23);
+ // Python SDK parity: server.address, server.port, gen_ai.sdk.version
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.SERVER_ADDRESS, 'api.mistral.ai');
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.SERVER_PORT, 443);
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_SDK_VERSION, '1.9.0');
});
});
diff --git a/sdk/typescript/src/instrumentation/__tests__/together-trace-comparison.test.ts b/sdk/typescript/src/instrumentation/__tests__/together-trace-comparison.test.ts
index 4fa80660..6c6bc067 100644
--- a/sdk/typescript/src/instrumentation/__tests__/together-trace-comparison.test.ts
+++ b/sdk/typescript/src/instrumentation/__tests__/together-trace-comparison.test.ts
@@ -46,6 +46,13 @@ describe('Together AI Cross-Language Trace Comparison', () => {
if (attrs.cost !== undefined) {
span.setAttribute(SemanticConvention.GEN_AI_USAGE_COST, attrs.cost);
}
+ if (attrs.serverAddress) {
+ span.setAttribute(SemanticConvention.SERVER_ADDRESS, attrs.serverAddress);
+ }
+ if (attrs.serverPort !== undefined) {
+ span.setAttribute(SemanticConvention.SERVER_PORT, attrs.serverPort);
+ }
+ span.setAttribute(SemanticConvention.GEN_AI_SDK_VERSION, '1.9.0');
});
});
@@ -99,6 +106,10 @@ describe('Together AI Cross-Language Trace Comparison', () => {
expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, 9);
expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, 12);
expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS, 21);
+ // Python SDK parity: server.address, server.port, gen_ai.sdk.version
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.SERVER_ADDRESS, 'api.together.xyz');
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.SERVER_PORT, 443);
+ expect(mockSpan.setAttribute).toHaveBeenCalledWith(SemanticConvention.GEN_AI_SDK_VERSION, '1.9.0');
});
});
});
diff --git a/sdk/typescript/src/instrumentation/__tests__/trace-comparison-utils.ts b/sdk/typescript/src/instrumentation/__tests__/trace-comparison-utils.ts
index 19378764..7dc30bc1 100644
--- a/sdk/typescript/src/instrumentation/__tests__/trace-comparison-utils.ts
+++ b/sdk/typescript/src/instrumentation/__tests__/trace-comparison-utils.ts
@@ -146,6 +146,9 @@ function compareAttributes(
'gen_ai.endpoint',
'gen_ai.environment',
'gen_ai.application_name',
+ 'server.address',
+ 'server.port',
+ 'gen_ai.sdk.version',
];
for (const key of allKeys) {
diff --git a/sdk/typescript/src/instrumentation/anthropic/wrapper.ts b/sdk/typescript/src/instrumentation/anthropic/wrapper.ts
index b1fd876b..676ecabe 100644
--- a/sdk/typescript/src/instrumentation/anthropic/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/anthropic/wrapper.ts
@@ -6,6 +6,8 @@ import BaseWrapper, { BaseSpanAttributes } from '../base-wrapper';
export default class AnthropicWrapper extends BaseWrapper {
static aiSystem = SemanticConvention.GEN_AI_SYSTEM_ANTHROPIC;
+ static serverAddress = 'api.anthropic.com';
+ static serverPort = 443;
static _patchMessageCreate(tracer: Tracer): any {
const genAIEndpoint = 'anthropic.resources.messages';
@@ -87,6 +89,8 @@ export default class AnthropicWrapper extends BaseWrapper {
span: Span;
}) {
let metricParams: BaseSpanAttributes | undefined;
+ const timestamps: number[] = [];
+ const startTime = Date.now();
try {
const result = {
id: '0',
@@ -102,9 +106,12 @@ export default class AnthropicWrapper extends BaseWrapper {
input_tokens: 0,
output_tokens: 0,
total_tokens: 0,
+ cache_creation_input_tokens: 0,
+ cache_read_input_tokens: 0,
},
};
for await (const chunk of response) {
+ timestamps.push(Date.now());
switch (chunk.type) {
case 'content_block_delta':
result.content[0].text += chunk.delta?.text ?? '';
@@ -122,6 +129,8 @@ export default class AnthropicWrapper extends BaseWrapper {
result.content[0].role = chunk.message.role;
result.usage.input_tokens += Number(chunk.message.usage?.input_tokens) ?? 0;
result.usage.output_tokens += Number(chunk.message.usage?.output_tokens) ?? 0;
+ result.usage.cache_creation_input_tokens = Number(chunk.message.usage?.cache_creation_input_tokens) || 0;
+ result.usage.cache_read_input_tokens = Number(chunk.message.usage?.cache_read_input_tokens) || 0;
result.stop_reason = chunk.message?.stop_reason ?? '';
}
break;
@@ -132,6 +141,12 @@ export default class AnthropicWrapper extends BaseWrapper {
case 'message_delta':
result.stop_reason = chunk.delta?.stop_reason ?? '';
result.usage.output_tokens += Number(chunk.usage?.output_tokens) ?? 0;
+ if (chunk.usage?.cache_creation_input_tokens) {
+ result.usage.cache_creation_input_tokens = Number(chunk.usage.cache_creation_input_tokens) || 0;
+ }
+ if (chunk.usage?.cache_read_input_tokens) {
+ result.usage.cache_read_input_tokens = Number(chunk.usage.cache_read_input_tokens) || 0;
+ }
break;
}
@@ -140,11 +155,21 @@ export default class AnthropicWrapper extends BaseWrapper {
result.usage.total_tokens = result.usage.output_tokens + result.usage.input_tokens;
+ // Calculate TTFT and TBT
+ const ttft = timestamps.length > 0 ? (timestamps[0] - startTime) / 1000 : 0;
+ let tbt = 0;
+ if (timestamps.length > 1) {
+ const timeDiffs = timestamps.slice(1).map((t, i) => t - timestamps[i]);
+ tbt = timeDiffs.reduce((a, b) => a + b, 0) / timeDiffs.length / 1000;
+ }
+
metricParams = await AnthropicWrapper._messageCreateCommonSetter({
args,
genAIEndpoint,
result,
span,
+ ttft,
+ tbt,
});
return response;
} catch (e: any) {
@@ -162,15 +187,20 @@ export default class AnthropicWrapper extends BaseWrapper {
genAIEndpoint,
result,
span,
+ ttft = 0,
+ tbt = 0,
}: {
args: any[];
genAIEndpoint: string;
result: any;
span: Span;
+ ttft?: number;
+ tbt?: number;
}) {
const traceContent = OpenlitConfig.traceContent;
const {
messages,
+ system,
max_tokens = null,
seed = null,
temperature = 1,
@@ -204,6 +234,8 @@ export default class AnthropicWrapper extends BaseWrapper {
user,
cost,
aiSystem: AnthropicWrapper.aiSystem,
+ serverAddress: AnthropicWrapper.serverAddress,
+ serverPort: AnthropicWrapper.serverPort,
});
// Response model
@@ -217,6 +249,13 @@ export default class AnthropicWrapper extends BaseWrapper {
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, stream);
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_SEED, seed);
+
+ // System instructions
+ if (system) {
+ const systemStr = typeof system === 'string' ? system : JSON.stringify(system);
+ span.setAttribute(SemanticConvention.GEN_AI_SYSTEM_INSTRUCTIONS, systemStr);
+ }
+
if (traceContent) {
span.setAttribute(SemanticConvention.GEN_AI_INPUT_MESSAGES, OpenLitHelper.buildInputMessages(messages || []));
}
@@ -230,13 +269,53 @@ export default class AnthropicWrapper extends BaseWrapper {
);
span.setAttribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, result.usage.input_tokens + result.usage.output_tokens);
+ // Cache token attributes (Anthropic prompt caching)
+ if (result.usage.cache_creation_input_tokens) {
+ span.setAttribute(
+ SemanticConvention.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
+ result.usage.cache_creation_input_tokens
+ );
+ }
+ if (result.usage.cache_read_input_tokens) {
+ span.setAttribute(
+ SemanticConvention.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
+ result.usage.cache_read_input_tokens
+ );
+ }
+
+ // TTFT and TBT streaming metrics
+ if (ttft > 0) {
+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft);
+ }
+ if (tbt > 0) {
+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt);
+ }
+
if (result.stop_reason) {
span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [result.stop_reason]);
}
+ // Tool calls from content blocks of type 'tool_use'
+ const toolUseBlocks = (result.content || []).filter((b: any) => b.type === 'tool_use');
+ if (toolUseBlocks.length > 0) {
+ const toolNames = toolUseBlocks.map((b: any) => b.name || '').filter(Boolean);
+ const toolIds = toolUseBlocks.map((b: any) => b.id || '').filter(Boolean);
+ const toolArgs = toolUseBlocks.map((b: any) => JSON.stringify(b.input || {}));
+ if (toolNames.length > 0) span.setAttribute(SemanticConvention.GEN_AI_TOOL_NAME, toolNames.join(', '));
+ if (toolIds.length > 0) span.setAttribute(SemanticConvention.GEN_AI_TOOL_CALL_ID, toolIds.join(', '));
+ if (toolArgs.length > 0) span.setAttribute(SemanticConvention.GEN_AI_TOOL_CALL_ARGUMENTS, toolArgs);
+ span.setAttribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, SemanticConvention.GEN_AI_OUTPUT_TYPE_JSON);
+ } else {
+ span.setAttribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, SemanticConvention.GEN_AI_OUTPUT_TYPE_TEXT);
+ }
+
if (traceContent) {
+ const textContent = (result.content || [])
+ .filter((b: any) => b.type === 'text')
+ .map((b: any) => b.text || '')
+ .join('');
span.setAttribute(SemanticConvention.GEN_AI_OUTPUT_MESSAGES,
- OpenLitHelper.buildOutputMessages(result.content?.[0]?.text || '', result.stop_reason || 'stop'));
+ OpenLitHelper.buildOutputMessages(textContent, result.stop_reason || 'stop', toolUseBlocks.length > 0 ? toolUseBlocks : undefined));
}
return {
genAIEndpoint,
@@ -244,6 +323,8 @@ export default class AnthropicWrapper extends BaseWrapper {
user,
cost,
aiSystem: AnthropicWrapper.aiSystem,
+ serverAddress: AnthropicWrapper.serverAddress,
+ serverPort: AnthropicWrapper.serverPort,
};
}
}
diff --git a/sdk/typescript/src/instrumentation/base-wrapper.ts b/sdk/typescript/src/instrumentation/base-wrapper.ts
index e07c9de3..ac53b7a8 100644
--- a/sdk/typescript/src/instrumentation/base-wrapper.ts
+++ b/sdk/typescript/src/instrumentation/base-wrapper.ts
@@ -1,5 +1,5 @@
import OpenlitConfig from '../config';
-import { SDK_NAME, TELEMETRY_SDK_NAME } from '../constant';
+import { SDK_NAME, SDK_VERSION, TELEMETRY_SDK_NAME } from '../constant';
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import SemanticConvention from '../semantic-convention';
import { Span, SpanStatusCode } from '@opentelemetry/api';
@@ -11,16 +11,18 @@ export type BaseSpanAttributes = {
user?: unknown;
cost?: number | string;
aiSystem: string;
+ serverAddress?: string;
+ serverPort?: number;
};
export default class BaseWrapper {
static setBaseSpanAttributes(
span: Span,
- { genAIEndpoint, model, user, cost, aiSystem }: BaseSpanAttributes
+ { genAIEndpoint, model, user, cost, aiSystem, serverAddress, serverPort }: BaseSpanAttributes
) {
const applicationName = OpenlitConfig.applicationName;
const environment = OpenlitConfig.environment;
-
+
if (!applicationName) {
throw new Error("[Openlit] OpenlitConfig.applicationName is not set. Please check your configuration.");
}
@@ -38,6 +40,13 @@ export default class BaseWrapper {
span.setAttribute(SemanticConvention.GEN_AI_ENVIRONMENT, environment);
span.setAttribute(SemanticConvention.GEN_AI_APPLICATION_NAME, applicationName);
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_MODEL, model);
+ span.setAttribute(SemanticConvention.GEN_AI_SDK_VERSION, SDK_VERSION);
+ if (serverAddress) {
+ span.setAttribute(SemanticConvention.SERVER_ADDRESS, serverAddress);
+ }
+ if (serverPort !== undefined) {
+ span.setAttribute(SemanticConvention.SERVER_PORT, serverPort);
+ }
if (typeof user === 'string' || typeof user === 'number') {
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_USER, user);
}
@@ -77,6 +86,17 @@ export default class BaseWrapper {
if (Number.isFinite(tbt)) Metrics.genaiServerTbt?.record(tbt as number, attributes);
const ttft = BaseWrapper.getSpanAttribute(span, SemanticConvention.GEN_AI_SERVER_TTFT);
if (Number.isFinite(ttft)) Metrics.genaiServerTtft?.record(ttft as number, attributes);
+ // Client-perspective streaming metrics (OTel semconv v1.29+)
+ if (Number.isFinite(ttft) && (ttft as number) > 0) {
+ Metrics.genaiClientTimeToFirstChunk?.record(ttft as number, attributes);
+ }
+ if (Number.isFinite(tbt) && (tbt as number) > 0) {
+ Metrics.genaiClientTimePerOutputChunk?.record(tbt as number, attributes);
+ // Server request duration = TTFT + TBT * max(output_tokens - 1, 0)
+ const outputTokensVal = Number.isFinite(outputTokens) ? (outputTokens as number) : 0;
+ const serverRequestDuration = (ttft as number) + (tbt as number) * Math.max(outputTokensVal - 1, 0);
+ Metrics.genaiServerRequestDuration?.record(serverRequestDuration, attributes);
+ }
if (cost !== undefined) {
const numericCost = typeof cost === 'number' ? cost : Number(cost);
if (Number.isFinite(numericCost)) {
diff --git a/sdk/typescript/src/instrumentation/bedrock/wrapper.ts b/sdk/typescript/src/instrumentation/bedrock/wrapper.ts
index 015fc137..37c62724 100644
--- a/sdk/typescript/src/instrumentation/bedrock/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/bedrock/wrapper.ts
@@ -94,6 +94,8 @@ class BedrockWrapper extends BaseWrapper {
model: modelId,
cost,
aiSystem: BedrockWrapper.aiSystem,
+ serverAddress: BEDROCK_SERVER_ADDRESS,
+ serverPort: BEDROCK_SERVER_PORT,
});
span.setAttribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT);
@@ -110,9 +112,9 @@ class BedrockWrapper extends BaseWrapper {
span.setAttribute(SemanticConvention.SERVER_PORT, BEDROCK_SERVER_PORT);
if (cacheReadTokens > 0)
- span.setAttribute(SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ, cacheReadTokens);
+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cacheReadTokens);
if (cacheWriteTokens > 0)
- span.setAttribute(SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_WRITE, cacheWriteTokens);
+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, cacheWriteTokens);
const requestId = response.$metadata?.requestId;
if (requestId)
@@ -183,13 +185,16 @@ class BedrockWrapper extends BaseWrapper {
let cacheReadTokens = 0;
let cacheWriteTokens = 0;
let firstChunkTime: number | null = null;
+ const chunkTimestamps: number[] = [];
const originalStream: AsyncIterable<any> = response.stream;
async function* wrappedStream() {
try {
for await (const event of originalStream) {
- if (firstChunkTime === null) firstChunkTime = Date.now();
+ const now = Date.now();
+ if (firstChunkTime === null) firstChunkTime = now;
+ chunkTimestamps.push(now);
if (event.contentBlockDelta?.delta?.text)
llmResponse += event.contentBlockDelta.delta.text;
@@ -208,6 +213,11 @@ class BedrockWrapper extends BaseWrapper {
try {
const duration = (Date.now() - startTime) / 1000;
const ttft = firstChunkTime !== null ? (firstChunkTime - startTime) / 1000 : 0;
+ let tbt = 0;
+ if (chunkTimestamps.length > 1) {
+ const timeDiffs = chunkTimestamps.slice(1).map((t, i) => t - chunkTimestamps[i]);
+ tbt = timeDiffs.reduce((a, b) => a + b, 0) / timeDiffs.length / 1000;
+ }
const totalTokens = inputTokens + outputTokens;
const pricingInfo = await OpenlitConfig.updatePricingJson(OpenlitConfig.pricing_json);
@@ -230,14 +240,15 @@ class BedrockWrapper extends BaseWrapper {
span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [finishReason]);
span.setAttribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, SemanticConvention.GEN_AI_OUTPUT_TYPE_TEXT);
span.setAttribute(SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION, duration);
- span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft);
+ if (ttft > 0) span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft);
+ if (tbt > 0) span.setAttribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt);
span.setAttribute(SemanticConvention.SERVER_ADDRESS, BEDROCK_SERVER_ADDRESS);
span.setAttribute(SemanticConvention.SERVER_PORT, BEDROCK_SERVER_PORT);
if (cacheReadTokens > 0)
- span.setAttribute(SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ, cacheReadTokens);
+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cacheReadTokens);
if (cacheWriteTokens > 0)
- span.setAttribute(SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_WRITE, cacheWriteTokens);
+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, cacheWriteTokens);
const requestId = response.$metadata?.requestId;
if (requestId)
@@ -356,6 +367,8 @@ class BedrockWrapper extends BaseWrapper {
model: modelId,
cost,
aiSystem: BedrockWrapper.aiSystem,
+ serverAddress: BEDROCK_SERVER_ADDRESS,
+ serverPort: BEDROCK_SERVER_PORT,
});
span.setAttribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT);
diff --git a/sdk/typescript/src/instrumentation/cohere/wrapper.ts b/sdk/typescript/src/instrumentation/cohere/wrapper.ts
index 98168748..6c4b8811 100644
--- a/sdk/typescript/src/instrumentation/cohere/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/cohere/wrapper.ts
@@ -7,6 +7,8 @@ import BaseWrapper from '../base-wrapper';
export default class CohereWrapper extends BaseWrapper {
static aiSystem = SemanticConvention.GEN_AI_SYSTEM_COHERE;
+ static serverAddress = 'api.cohere.com';
+ static serverPort = 443;
static _patchEmbed(tracer: Tracer): any {
const genAIEndpoint = 'cohere.embed';
const traceContent = OpenlitConfig.traceContent;
@@ -164,6 +166,8 @@ export default class CohereWrapper extends BaseWrapper {
span: Span;
}): AsyncGenerator<unknown, any, unknown> {
let metricParams;
+ const timestamps: number[] = [];
+ const startTime = Date.now();
try {
let result = {
response_id: '',
@@ -177,6 +181,7 @@ export default class CohereWrapper extends BaseWrapper {
},
};
for await (const chunk of response) {
+ timestamps.push(Date.now());
if (chunk.eventType === 'stream-end') {
result = chunk.response;
}
@@ -184,12 +189,22 @@ export default class CohereWrapper extends BaseWrapper {
yield chunk;
}
+ // Calculate TTFT and TBT
+ const ttft = timestamps.length > 0 ? (timestamps[0] - startTime) / 1000 : 0;
+ let tbt = 0;
+ if (timestamps.length > 1) {
+ const timeDiffs = timestamps.slice(1).map((t, i) => t - timestamps[i]);
+ tbt = timeDiffs.reduce((a, b) => a + b, 0) / timeDiffs.length / 1000;
+ }
+
metricParams = await CohereWrapper._chatCommonSetter({
args,
genAIEndpoint,
result,
span,
stream: true,
+ ttft,
+ tbt,
});
return result;
@@ -210,12 +225,16 @@ export default class CohereWrapper extends BaseWrapper {
result,
span,
stream,
+ ttft = 0,
+ tbt = 0,
}: {
args: any[];
genAIEndpoint: string;
result: any;
span: Span;
stream: boolean;
+ ttft?: number;
+ tbt?: number;
}) {
const traceContent = OpenlitConfig.traceContent;
const {
@@ -263,6 +282,8 @@ export default class CohereWrapper extends BaseWrapper {
user,
cost,
aiSystem: CohereWrapper.aiSystem,
+ serverAddress: CohereWrapper.serverAddress,
+ serverPort: CohereWrapper.serverPort,
});
// Response model
@@ -282,6 +303,14 @@ export default class CohereWrapper extends BaseWrapper {
);
span.setAttribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, result.meta.billedUnits.inputTokens + result.meta.billedUnits.outputTokens);
+ // TTFT and TBT streaming metrics
+ if (ttft > 0) {
+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft);
+ }
+ if (tbt > 0) {
+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt);
+ }
+
if (result.finishReason) {
span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [result.finishReason]);
}
diff --git a/sdk/typescript/src/instrumentation/google-ai/wrapper.ts b/sdk/typescript/src/instrumentation/google-ai/wrapper.ts
index 723af413..0dbcd46a 100644
--- a/sdk/typescript/src/instrumentation/google-ai/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/google-ai/wrapper.ts
@@ -6,6 +6,8 @@ import BaseWrapper from '../base-wrapper';
class GoogleAIWrapper extends BaseWrapper {
static aiSystem = 'google_ai_studio';
+ static serverAddress = 'generativelanguage.googleapis.com';
+ static serverPort = 443;
static _patchGenerateContent(tracer: Tracer): any {
const genAIEndpoint = 'google.generativeai.models.generate_content';
@@ -265,6 +267,8 @@ class GoogleAIWrapper extends BaseWrapper {
user: undefined,
cost,
aiSystem: GoogleAIWrapper.aiSystem,
+ serverAddress: GoogleAIWrapper.serverAddress,
+ serverPort: GoogleAIWrapper.serverPort,
});
// Response model
diff --git a/sdk/typescript/src/instrumentation/groq/wrapper.ts b/sdk/typescript/src/instrumentation/groq/wrapper.ts
index 8437d64e..10af640b 100644
--- a/sdk/typescript/src/instrumentation/groq/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/groq/wrapper.ts
@@ -6,6 +6,8 @@ import BaseWrapper from '../base-wrapper';
class GroqWrapper extends BaseWrapper {
static aiSystem = 'groq';
+ static serverAddress = 'api.groq.com';
+ static serverPort = 443;
static _patchChatCompletionCreate(tracer: Tracer): any {
const genAIEndpoint = 'groq.chat.completions';
@@ -306,6 +308,8 @@ class GroqWrapper extends BaseWrapper {
user,
cost,
aiSystem: GroqWrapper.aiSystem,
+ serverAddress: GroqWrapper.serverAddress,
+ serverPort: GroqWrapper.serverPort,
});
// Response model
diff --git a/sdk/typescript/src/instrumentation/langchain/wrapper.ts b/sdk/typescript/src/instrumentation/langchain/wrapper.ts
index ae4bcee9..ffc629b0 100644
--- a/sdk/typescript/src/instrumentation/langchain/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/langchain/wrapper.ts
@@ -251,6 +251,8 @@ class OpenLITCallbackHandler {
model: responseModel,
cost,
aiSystem: SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,
+ serverAddress: 'localhost',
+ serverPort: 80,
});
const metricParams: BaseSpanAttributes = {
@@ -258,6 +260,8 @@ class OpenLITCallbackHandler {
model: responseModel,
cost,
aiSystem: SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,
+ serverAddress: 'localhost',
+ serverPort: 80,
};
span.setStatus({ code: 1 });
span.end();
diff --git a/sdk/typescript/src/instrumentation/llamaindex/wrapper.ts b/sdk/typescript/src/instrumentation/llamaindex/wrapper.ts
index ecef0e72..fdb7b39d 100644
--- a/sdk/typescript/src/instrumentation/llamaindex/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/llamaindex/wrapper.ts
@@ -57,7 +57,7 @@ class LlamaIndexWrapper extends BaseWrapper {
const { address, port } = LlamaIndexWrapper._extractServerInfo(this);
- LlamaIndexWrapper.setBaseSpanAttributes(span, { genAIEndpoint, model: modelId, cost, aiSystem });
+ LlamaIndexWrapper.setBaseSpanAttributes(span, { genAIEndpoint, model: modelId, cost, aiSystem, serverAddress: address, serverPort: port });
span.setAttribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT);
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, false);
@@ -126,7 +126,7 @@ class LlamaIndexWrapper extends BaseWrapper {
const { address, port } = LlamaIndexWrapper._extractServerInfo(this);
- LlamaIndexWrapper.setBaseSpanAttributes(span, { genAIEndpoint, model: modelId, cost, aiSystem });
+ LlamaIndexWrapper.setBaseSpanAttributes(span, { genAIEndpoint, model: modelId, cost, aiSystem, serverAddress: address, serverPort: port });
span.setAttribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT);
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, false);
@@ -188,7 +188,7 @@ class LlamaIndexWrapper extends BaseWrapper {
const { address, port } = LlamaIndexWrapper._extractServerInfo(llm || this);
- LlamaIndexWrapper.setBaseSpanAttributes(span, { genAIEndpoint, model: modelId, aiSystem });
+ LlamaIndexWrapper.setBaseSpanAttributes(span, { genAIEndpoint, model: modelId, aiSystem, serverAddress: address, serverPort: port });
span.setAttribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK);
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, false);
@@ -260,7 +260,7 @@ class LlamaIndexWrapper extends BaseWrapper {
const { address, port } = LlamaIndexWrapper._extractServerInfo(llm || this);
- LlamaIndexWrapper.setBaseSpanAttributes(span, { genAIEndpoint, model: modelId, aiSystem });
+ LlamaIndexWrapper.setBaseSpanAttributes(span, { genAIEndpoint, model: modelId, aiSystem, serverAddress: address, serverPort: port });
span.setAttribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK);
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, false);
diff --git a/sdk/typescript/src/instrumentation/mistral/wrapper.ts b/sdk/typescript/src/instrumentation/mistral/wrapper.ts
index 35524e4b..df29b061 100644
--- a/sdk/typescript/src/instrumentation/mistral/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/mistral/wrapper.ts
@@ -6,6 +6,8 @@ import BaseWrapper, { BaseSpanAttributes } from '../base-wrapper';
class MistralWrapper extends BaseWrapper {
static aiSystem = 'mistral';
+ static serverAddress = 'api.mistral.ai';
+ static serverPort = 443;
static _patchChatCompletionCreate(tracer: Tracer): any {
const genAIEndpoint = 'mistral.chat.completions';
@@ -292,6 +294,8 @@ class MistralWrapper extends BaseWrapper {
user,
cost,
aiSystem: MistralWrapper.aiSystem,
+ serverAddress: MistralWrapper.serverAddress,
+ serverPort: MistralWrapper.serverPort,
});
// Response model
@@ -399,6 +403,8 @@ class MistralWrapper extends BaseWrapper {
user,
cost,
aiSystem: MistralWrapper.aiSystem,
+ serverAddress: MistralWrapper.serverAddress,
+ serverPort: MistralWrapper.serverPort,
});
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS, [encoding_format]);
diff --git a/sdk/typescript/src/instrumentation/ollama/wrapper.ts b/sdk/typescript/src/instrumentation/ollama/wrapper.ts
index 679c7faa..ea0b7549 100644
--- a/sdk/typescript/src/instrumentation/ollama/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/ollama/wrapper.ts
@@ -6,6 +6,8 @@ import BaseWrapper, { BaseSpanAttributes } from '../base-wrapper';
export default class OllamaWrapper extends BaseWrapper {
static aiSystem = 'ollama';
+ static serverAddress = '127.0.0.1';
+ static serverPort = 11434;
static _patchChat(tracer: Tracer): any {
const genAIEndpoint = 'ollama.chat';
@@ -183,6 +185,8 @@ export default class OllamaWrapper extends BaseWrapper {
user,
cost,
aiSystem: OllamaWrapper.aiSystem,
+ serverAddress: OllamaWrapper.serverAddress,
+ serverPort: OllamaWrapper.serverPort,
});
// Response model
diff --git a/sdk/typescript/src/instrumentation/openai/wrapper.ts b/sdk/typescript/src/instrumentation/openai/wrapper.ts
index e7b69f9c..b461e0fd 100644
--- a/sdk/typescript/src/instrumentation/openai/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/openai/wrapper.ts
@@ -6,6 +6,8 @@ import BaseWrapper, { BaseSpanAttributes } from '../base-wrapper';
class OpenAIWrapper extends BaseWrapper {
static aiSystem = SemanticConvention.GEN_AI_SYSTEM_OPENAI;
+ static serverAddress = 'api.openai.com';
+ static serverPort = 443;
static _patchChatCompletionCreate(tracer: Tracer): any {
const genAIEndpoint = 'openai.resources.chat.completions';
return (originalMethod: (...args: any[]) => any) => {
@@ -316,11 +318,13 @@ class OpenAIWrapper extends BaseWrapper {
user,
cost,
aiSystem: OpenAIWrapper.aiSystem,
+ serverAddress: OpenAIWrapper.serverAddress,
+ serverPort: OpenAIWrapper.serverPort,
});
// Response model
span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, responseModel);
-
+
// OpenAI-specific attributes
if (result.system_fingerprint) {
span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT, result.system_fingerprint);
@@ -470,15 +474,11 @@ class OpenAIWrapper extends BaseWrapper {
user,
cost,
aiSystem: OpenAIWrapper.aiSystem,
+ serverAddress: OpenAIWrapper.serverAddress,
+ serverPort: OpenAIWrapper.serverPort,
});
- // Set missing critical attributes to match Python SDK
- span.setAttribute(SemanticConvention.SERVER_ADDRESS, 'api.openai.com');
- span.setAttribute(SemanticConvention.SERVER_PORT, 443);
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, false);
- span.setAttribute(SemanticConvention.GEN_AI_SERVER_TBT, 0);
- span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, 0);
- span.setAttribute(SemanticConvention.GEN_AI_SDK_VERSION, '1.7.0');
// Request Params attributes : Start
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_ENCODING_FORMATS, [encoding_format]);
@@ -550,12 +550,14 @@ class OpenAIWrapper extends BaseWrapper {
validation_file,
} = args[0];
- // Set base span attribues
+ // Set base span attributes
OpenAIWrapper.setBaseSpanAttributes(span, {
genAIEndpoint,
model,
user,
aiSystem: OpenAIWrapper.aiSystem,
+ serverAddress: OpenAIWrapper.serverAddress,
+ serverPort: OpenAIWrapper.serverPort,
});
span.setAttribute(
@@ -652,6 +654,8 @@ class OpenAIWrapper extends BaseWrapper {
user,
cost,
aiSystem: OpenAIWrapper.aiSystem,
+ serverAddress: OpenAIWrapper.serverAddress,
+ serverPort: OpenAIWrapper.serverPort,
});
// Request Params attributes : Start
@@ -745,6 +749,8 @@ class OpenAIWrapper extends BaseWrapper {
user,
cost,
aiSystem: OpenAIWrapper.aiSystem,
+ serverAddress: OpenAIWrapper.serverAddress,
+ serverPort: OpenAIWrapper.serverPort,
});
// Request Params attributes : Start
@@ -827,6 +833,8 @@ class OpenAIWrapper extends BaseWrapper {
user,
cost,
aiSystem: OpenAIWrapper.aiSystem,
+ serverAddress: OpenAIWrapper.serverAddress,
+ serverPort: OpenAIWrapper.serverPort,
});
// Request Params attributes : Start
@@ -1115,6 +1123,8 @@ class OpenAIWrapper extends BaseWrapper {
user: '',
cost,
aiSystem: OpenAIWrapper.aiSystem,
+ serverAddress: OpenAIWrapper.serverAddress,
+ serverPort: OpenAIWrapper.serverPort,
});
// Response attributes
diff --git a/sdk/typescript/src/instrumentation/together/wrapper.ts b/sdk/typescript/src/instrumentation/together/wrapper.ts
index a4199c95..70094f61 100644
--- a/sdk/typescript/src/instrumentation/together/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/together/wrapper.ts
@@ -6,6 +6,8 @@ import BaseWrapper from '../base-wrapper';
class TogetherWrapper extends BaseWrapper {
static aiSystem = 'together';
+ static serverAddress = 'api.together.xyz';
+ static serverPort = 443;
static _patchChatCompletionCreate(tracer: Tracer): any {
const genAIEndpoint = 'together.chat.completions';
@@ -287,6 +289,8 @@ class TogetherWrapper extends BaseWrapper {
user,
cost,
aiSystem: TogetherWrapper.aiSystem,
+ serverAddress: TogetherWrapper.serverAddress,
+ serverPort: TogetherWrapper.serverPort,
});
// Response model
diff --git a/sdk/typescript/src/instrumentation/vercel-ai/wrapper.ts b/sdk/typescript/src/instrumentation/vercel-ai/wrapper.ts
index ebcd389a..9dac50e3 100644
--- a/sdk/typescript/src/instrumentation/vercel-ai/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/vercel-ai/wrapper.ts
@@ -92,6 +92,7 @@ class VercelAIWrapper extends BaseWrapper {
return async function (this: any, ...args: any[]) {
const span = tracer.startSpan(genAIEndpoint, { kind: SpanKind.CLIENT });
const startTime = Date.now();
+ const chunkTimestamps: number[] = [];
try {
const response = await originalMethod.apply(this, args);
@@ -112,13 +113,46 @@ class VercelAIWrapper extends BaseWrapper {
span.setAttribute(SemanticConvention.GEN_AI_INPUT_MESSAGES, OpenLitHelper.buildInputMessages(messages));
}
+ // Intercept textStream to capture per-chunk timestamps for TTFT/TBT
+ try {
+ const originalTextStream = response.textStream as ReadableStream<string>;
+ if (originalTextStream && typeof originalTextStream.getReader === 'function') {
+ const reader = originalTextStream.getReader();
+ const wrappedTextStream = new ReadableStream<string>({
+ async pull(controller) {
+ const { done, value } = await reader.read();
+ if (done) {
+ controller.close();
+ } else {
+ chunkTimestamps.push(Date.now());
+ controller.enqueue(value);
+ }
+ },
+ cancel() {
+ reader.cancel();
+ },
+ });
+ Object.defineProperty(response, 'textStream', {
+ value: wrappedTextStream,
+ writable: true,
+ configurable: true,
+ });
+ }
+ } catch (_) {
+ // Stream interception failed; TTFT/TBT won't be captured from textStream
+ }
+
// Observe stream completion via usage promise
Promise.resolve(response.usage)
.then(async (usage: any) => {
try {
- const ttft = response.experimental_providerMetadata
- ? undefined
- : (Date.now() - startTime) / 1000;
+ const ttft = chunkTimestamps.length > 0 ? (chunkTimestamps[0] - startTime) / 1000 : 0;
+ let tbt = 0;
+ if (chunkTimestamps.length > 1) {
+ const timeDiffs = chunkTimestamps.slice(1).map((t, i) => t - chunkTimestamps[i]);
+ tbt = timeDiffs.reduce((a, b) => a + b, 0) / timeDiffs.length / 1000;
+ }
+
const pricingInfo = await OpenlitConfig.updatePricingJson(OpenlitConfig.pricing_json);
const cost = OpenLitHelper.getChatModelCost(
modelId,
@@ -135,7 +169,8 @@ class VercelAIWrapper extends BaseWrapper {
span.setAttribute(SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS, usage?.totalTokens || 0);
span.setAttribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, usage?.totalTokens || 0);
span.setAttribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, SemanticConvention.GEN_AI_OUTPUT_TYPE_TEXT);
- if (ttft) span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft);
+ if (ttft > 0) span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft);
+ if (tbt > 0) span.setAttribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt);
const finishReason = await Promise.resolve(response.finishReason).catch(() => 'stop');
span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, [finishReason || 'stop']);
diff --git a/sdk/typescript/src/otel/metrics.ts b/sdk/typescript/src/otel/metrics.ts
index 8939ec02..f14e5cb7 100644
--- a/sdk/typescript/src/otel/metrics.ts
+++ b/sdk/typescript/src/otel/metrics.ts
@@ -35,6 +35,9 @@ export default class Metrics {
>;
static genaiServerTbt: ReturnType<ReturnType<typeof metrics.getMeter>['createHistogram']>;
static genaiServerTtft: ReturnType<ReturnType<typeof metrics.getMeter>['createHistogram']>;
+ static genaiClientTimeToFirstChunk: ReturnType<ReturnType<typeof metrics.getMeter>['createHistogram']>;
+ static genaiClientTimePerOutputChunk: ReturnType<ReturnType<typeof metrics.getMeter>['createHistogram']>;
+ static genaiServerRequestDuration: ReturnType<ReturnType<typeof metrics.getMeter>['createHistogram']>;
static dbClientOperationDuration: ReturnType<
ReturnType<typeof metrics.getMeter>['createHistogram']
>;
@@ -80,6 +83,36 @@ export default class Metrics {
explicitBucketBoundaries: GEN_AI_SERVER_TFTT,
},
});
+ this.genaiClientTimeToFirstChunk = this.meter.createHistogram(
+ SemanticConvention.GEN_AI_CLIENT_OPERATION_TIME_TO_FIRST_CHUNK,
+ {
+ description: 'Time from client request to first response chunk',
+ unit: 's',
+ advice: {
+ explicitBucketBoundaries: GEN_AI_CLIENT_OPERATION_DURATION_BUCKETS,
+ },
+ }
+ );
+ this.genaiClientTimePerOutputChunk = this.meter.createHistogram(
+ SemanticConvention.GEN_AI_CLIENT_OPERATION_TIME_PER_OUTPUT_CHUNK,
+ {
+ description: 'Time between consecutive response chunks from client perspective',
+ unit: 's',
+ advice: {
+ explicitBucketBoundaries: GEN_AI_CLIENT_OPERATION_DURATION_BUCKETS,
+ },
+ }
+ );
+ this.genaiServerRequestDuration = this.meter.createHistogram(
+ SemanticConvention.GEN_AI_SERVER_REQUEST_DURATION,
+ {
+ description: 'Total server-side processing time from request receipt to response transmission',
+ unit: 's',
+ advice: {
+ explicitBucketBoundaries: GEN_AI_CLIENT_OPERATION_DURATION_BUCKETS,
+ },
+ }
+ );
this.dbClientOperationDuration = this.meter.createHistogram(
SemanticConvention.DB_CLIENT_OPERATION_DURATION,
{
diff --git a/sdk/typescript/src/semantic-convention.ts b/sdk/typescript/src/semantic-convention.ts
index 82e3aaef..a5526cab 100644
--- a/sdk/typescript/src/semantic-convention.ts
+++ b/sdk/typescript/src/semantic-convention.ts
@@ -57,6 +57,9 @@ export default class SemanticConvention {
static GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_REASONING = 'gen_ai.usage.completion_tokens_details.reasoning';
static GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ = 'gen_ai.usage.prompt_tokens_details.cache_read';
static GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_WRITE = 'gen_ai.usage.prompt_tokens_details.cache_write';
+ // OTel semconv standard cache token attribute names (aligned with Python SDK)
+ static GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS = 'gen_ai.usage.cache_creation.input_tokens';
+ static GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS = 'gen_ai.usage.cache_read.input_tokens';
// GenAI Response
static GEN_AI_RESPONSE_ID = 'gen_ai.response.id';
@@ -162,6 +165,9 @@ export default class SemanticConvention {
static DB_UPDATE_VALUES = 'db.update.values';
static DB_UPDATE_ID = 'db.update.id';
static GEN_AI_CLIENT_OPERATION_DURATION = 'gen_ai.client.operation.duration';
+ static GEN_AI_CLIENT_OPERATION_TIME_TO_FIRST_CHUNK = 'gen_ai.client.operation.time_to_first_chunk';
+ static GEN_AI_CLIENT_OPERATION_TIME_PER_OUTPUT_CHUNK = 'gen_ai.client.operation.time_per_output_chunk';
+ static GEN_AI_SERVER_REQUEST_DURATION = 'gen_ai.server.request.duration';
static GEN_AI_CLIENT_TOKEN_USAGE = 'gen_ai.client.token.usage';
static GEN_AI_SERVER_TBT = 'gen_ai.server.time_per_output_token';
static GEN_AI_SERVER_TTFT = 'gen_ai.server.time_to_first_token';