From 2b65b39e3225dce51c58222c9c0c70e0d6234828 Mon Sep 17 00:00:00 2001 From: TingfengyuYu <821615805@qq.com> Date: Thu, 3 Dec 2026 16:47:53 +0800 Subject: [PATCH] Do not record the unset options of a spring-ai-1.x streaming chat request as the string null --- CHANGES.md | 6 ++++++ .../ai/v1/ChatModelStreamInterceptor.java | 17 ++++++++++++----- .../config/expectedData.yaml | 1 - 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index b9b125bd42..f459f75950 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,12 @@ Release Notes. 9.9.0 ------------------ +* Fix the `spring-ai-1.x` plugin recording the literal string `null` as the value of `gen_ai.request.top_k`, + `gen_ai.request.temperature`, `gen_ai.request.top_p` and `gen_ai.request.model` on the span of a streaming chat + request. `ChatOptions` returns `null` for a sampling setting the request does not carry, and `String.valueOf` turned + that into a value the collector keeps as a real tag. The span of the non-streaming request already skipped them, so + the two spans of one request carried different tags. + * Record `gen_ai.request.max_tokens`, `gen_ai.request.frequency_penalty` and `gen_ai.request.presence_penalty` on the spans of the `spring-ai-1.x` plugin. The `ChatOptions` of a request carries all three, and the OpenTelemetry GenAI semantic conventions list them as recommended span attributes, but the plugin recorded only the model name and the 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 dd65dc9d97..04dcfc6b2f 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 @@ -71,11 +71,18 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr return; } - Tags.GEN_AI_REQUEST_MODEL.set(span, chatOptions.getModel()); - Tags.GEN_AI_TEMPERATURE.set(span, String.valueOf(chatOptions.getTemperature())); - Tags.GEN_AI_TOP_K.set(span, String.valueOf(chatOptions.getTopK())); - Tags.GEN_AI_TOP_P.set(span, String.valueOf(chatOptions.getTopP())); - + if (chatOptions.getModel() != null) { + Tags.GEN_AI_REQUEST_MODEL.set(span, chatOptions.getModel()); + } + if (chatOptions.getTemperature() != null) { + Tags.GEN_AI_TEMPERATURE.set(span, String.valueOf(chatOptions.getTemperature())); + } + if (chatOptions.getTopK() != null) { + Tags.GEN_AI_TOP_K.set(span, String.valueOf(chatOptions.getTopK())); + } + if (chatOptions.getTopP() != null) { + Tags.GEN_AI_TOP_P.set(span, String.valueOf(chatOptions.getTopP())); + } if (chatOptions.getMaxTokens() != null) { Tags.GEN_AI_REQUEST_MAX_TOKENS.set(span, String.valueOf(chatOptions.getMaxTokens())); } 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 ecf778311f..2ee89f4e58 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 @@ -117,7 +117,6 @@ segmentItems: - { key: gen_ai.provider.name, value: openai } - { key: gen_ai.request.model, value: gpt-4.1-2025-04-14 } - { 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.request.max_tokens, value: '1000' } - { key: gen_ai.request.frequency_penalty, value: '0.5' }