feat: Add Open Telemetric semantic standard compliant log (#11975)

This commit is contained in:
Christie Warwick (Wilson)
2025-10-28 13:02:46 -07:00
committed by GitHub
parent 44bdd3ad11
commit 70996bfdee
11 changed files with 1371 additions and 179 deletions
+12 -17
View File
@@ -89,6 +89,7 @@ export function logUserPrompt(config: Config, event: UserPromptEvent): void {
if (!isTelemetrySdkInitialized()) return;
const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = {
body: event.toLogBody(),
attributes: event.toOpenTelemetryAttributes(config),
@@ -219,11 +220,9 @@ export function logApiError(config: Config, event: ApiErrorEvent): void {
if (!isTelemetrySdkInitialized()) return;
const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = {
body: event.toLogBody(),
attributes: event.toOpenTelemetryAttributes(config),
};
logger.emit(logRecord);
logger.emit(event.toLogRecord(config));
logger.emit(event.toSemanticLogRecord(config));
recordApiErrorMetrics(config, event.duration_ms, {
model: event.model,
status_code: event.status_code,
@@ -231,12 +230,11 @@ export function logApiError(config: Config, event: ApiErrorEvent): void {
});
// Record GenAI operation duration for errors
const conventionAttributes = getConventionAttributes(event);
recordApiResponseMetrics(config, event.duration_ms, {
model: event.model,
status_code: event.status_code,
genAiAttributes: {
...conventionAttributes,
...getConventionAttributes(event),
'error.type': event.error_type || 'unknown',
},
});
@@ -253,11 +251,8 @@ export function logApiResponse(config: Config, event: ApiResponseEvent): void {
if (!isTelemetrySdkInitialized()) return;
const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = {
body: event.toLogBody(),
attributes: event.toOpenTelemetryAttributes(config),
};
logger.emit(logRecord);
logger.emit(event.toLogRecord(config));
logger.emit(event.toSemanticLogRecord(config));
const conventionAttributes = getConventionAttributes(event);
@@ -268,11 +263,11 @@ export function logApiResponse(config: Config, event: ApiResponseEvent): void {
});
const tokenUsageData = [
{ count: event.input_token_count, type: 'input' as const },
{ count: event.output_token_count, type: 'output' as const },
{ count: event.cached_content_token_count, type: 'cache' as const },
{ count: event.thoughts_token_count, type: 'thought' as const },
{ count: event.tool_token_count, type: 'tool' as const },
{ count: event.usage.input_token_count, type: 'input' as const },
{ count: event.usage.output_token_count, type: 'output' as const },
{ count: event.usage.cached_content_token_count, type: 'cache' as const },
{ count: event.usage.thoughts_token_count, type: 'thought' as const },
{ count: event.usage.tool_token_count, type: 'tool' as const },
];
for (const { count, type } of tokenUsageData) {