From 10c1df1785b96c9e4dc856069eb36c823dff730d Mon Sep 17 00:00:00 2001 From: peachisai <96932958+peachisai@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:00:39 +0800 Subject: [PATCH] Report the time to first token of the spring-ai-1.x plugin in gen_ai.response.time_to_first_chunk The OpenTelemetry GenAI semantic conventions define gen_ai.response.time_to_first_chunk, in seconds, as the span attribute for it. gen_ai.server.time_to_first_token, which the plugin used with a value in milliseconds, is the name of a metric there. Tags.GEN_AI_SERVER_TIME_TO_FIRST_TOKEN is renamed to Tags.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK. --- CHANGES.md | 5 +++++ .../apache/skywalking/apm/agent/core/context/tag/Tags.java | 5 +++-- .../apm/plugin/spring/ai/v1/ChatModelStreamInterceptor.java | 3 ++- .../spring-ai-1.x-scenario/config/expectedData.yaml | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a490aa4104..a2d6adc63e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -30,6 +30,11 @@ Release Notes. * Bump the netty shaded into the agent from `4.1.124.Final` to `4.1.139.Final` (with netty-tcnative-boringssl-static `2.0.70.Final` → `2.0.84.Final`, the version netty `4.1.139.Final` is built with) to clear the netty-codec, netty-codec-http, netty-codec-http2, netty-handler and netty-handler-proxy CVEs. +* Report the time to first token of a streaming call of the `spring-ai-1.x` plugin in the + `gen_ai.response.time_to_first_chunk` tag, in seconds, the span attribute the OpenTelemetry GenAI semantic conventions + define for it, instead of in `gen_ai.server.time_to_first_token`, in milliseconds, a name the conventions give to a + metric. `Tags.GEN_AI_SERVER_TIME_TO_FIRST_TOKEN` is renamed to `Tags.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK`. An OAP that + reads only the previous tag shows no time to first token for the calls this agent traces. All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/267?closed=1) diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/tag/Tags.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/tag/Tags.java index 8acb2b9879..6deea4011d 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/tag/Tags.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/tag/Tags.java @@ -236,9 +236,10 @@ public static final class HTTP { public static final StringTag GEN_AI_RESPONSE_FINISH_REASONS = new StringTag(39, "gen_ai.response.finish_reasons"); /** - * GEN_AI_STREAM_TTFR represents the time to first response (TTFR) for streaming operations. + * GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK represents the time from the start of the call to the first chunk of a + * streaming response, in seconds. */ - public static final StringTag GEN_AI_SERVER_TIME_TO_FIRST_TOKEN = new StringTag(40, "gen_ai.server.time_to_first_token"); + public static final StringTag GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK = new StringTag(40, "gen_ai.response.time_to_first_chunk"); /** * GEN_AI_INPUT_MESSAGES represents the chat history provided to the model as an input. diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-ai-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/ai/v1/ChatModelStreamInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-ai-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/ai/v1/ChatModelStreamInterceptor.java index 981c90f74b..f1d1a99898 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-ai-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/ai/v1/ChatModelStreamInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/spring-ai-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/ai/v1/ChatModelStreamInterceptor.java @@ -161,7 +161,8 @@ private void recordTtfrIfFirstToken(AbstractSpan span, Generation generation, St return; } if (state.firstResponseReceived.compareAndSet(false, true)) { - Tags.GEN_AI_SERVER_TIME_TO_FIRST_TOKEN.set(span, String.valueOf(System.currentTimeMillis() - state.startTime)); + Tags.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK.set( + span, String.valueOf((System.currentTimeMillis() - state.startTime) / 1000.0)); } } diff --git a/test/plugin/scenarios/spring-ai-1.x-scenario/config/expectedData.yaml b/test/plugin/scenarios/spring-ai-1.x-scenario/config/expectedData.yaml index d714ae2542..8a9f4e4a76 100644 --- a/test/plugin/scenarios/spring-ai-1.x-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/spring-ai-1.x-scenario/config/expectedData.yaml @@ -116,7 +116,7 @@ segmentItems: - { key: gen_ai.request.temperature, value: '0.7' } - { key: gen_ai.request.top_k, value: null } - { key: gen_ai.request.top_p, value: '0.9' } - - { key: gen_ai.server.time_to_first_token, value: not null } + - { key: gen_ai.response.time_to_first_chunk, value: not null } - { key: gen_ai.response.id, value: 'chatcmpl-fc1b64d3' } - { key: gen_ai.response.model, value: gpt-4.1-2025-04-14 } - { key: gen_ai.response.finish_reasons, value: STOP }