From 01c0a2acfaedf331ad66c39bb66b8b070a8329a6 Mon Sep 17 00:00:00 2001 From: TingfengyuYu <821615805@qq.com> Date: Wed, 2 Dec 2026 18:41:37 +0800 Subject: [PATCH] Record gen_ai.request.max_tokens, frequency_penalty and presence_penalty in the spring-ai-1.x plugin The OpenTelemetry GenAI semantic conventions list gen_ai.request.max_tokens, gen_ai.request.frequency_penalty and gen_ai.request.presence_penalty as recommended attributes of the client inference span. The ChatOptions of a Spring AI request carries all three, but the plugin recorded only the model name and the sampling settings (temperature, top_k, top_p). Tags gets GEN_AI_REQUEST_MAX_TOKENS, GEN_AI_REQUEST_FREQUENCY_PENALTY and GEN_AI_REQUEST_PRESENCE_PENALTY, and both ChatModelCallInterceptor and ChatModelStreamInterceptor set them after GEN_AI_TOP_P. The three are optional in ChatOptions, so each is guarded by a null check: an option the caller left unset leaves no tag on the span, instead of writing the string null as the value. gen_ai.request.stop_sequences is left out. The conventions type it as a string array while a span tag holds one string, so the format of the join needs a decision first. --- CHANGES.md | 6 ++++++ .../apm/agent/core/context/tag/Tags.java | 15 +++++++++++++++ .../spring/ai/v1/ChatModelCallInterceptor.java | 9 +++++++++ .../spring/ai/v1/ChatModelStreamInterceptor.java | 10 ++++++++++ .../config/expectedData.yaml | 9 +++++++++ .../src/main/resources/application.yaml | 2 ++ 6 files changed, 51 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index bbd0b768ed..b9b125bd42 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,12 @@ Release Notes. 9.9.0 ------------------ +* 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 + sampling settings. `gen_ai.request.stop_sequences` is left out because the conventions type it as a string array while + a span tag holds one string, so the format of the join needs a decision first. + * Fix the gRPC server plugin leaking the trace context on an executor thread when `close` runs after `onCancel`. Both paths async-finish the same entry span, and the second `asyncFinish` threw before `stopSpan` (apache/skywalking#14268). 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 6deea4011d..ff7e3ee7ac 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 @@ -266,6 +266,21 @@ public static final class HTTP { */ public static final StringTag GEN_AI_RETRIEVAL_QUERY_TEXT = new StringTag(45, "gen_ai.retrieval.query.text"); + /** + * GEN_AI_REQUEST_MAX_TOKENS represents the maximum number of tokens the model generates for a request. + */ + public static final StringTag GEN_AI_REQUEST_MAX_TOKENS = new StringTag(46, "gen_ai.request.max_tokens"); + + /** + * GEN_AI_REQUEST_FREQUENCY_PENALTY represents the frequency penalty setting for the GenAI request. + */ + public static final StringTag GEN_AI_REQUEST_FREQUENCY_PENALTY = new StringTag(47, "gen_ai.request.frequency_penalty"); + + /** + * GEN_AI_REQUEST_PRESENCE_PENALTY represents the presence penalty setting for the GenAI request. + */ + public static final StringTag GEN_AI_REQUEST_PRESENCE_PENALTY = new StringTag(48, "gen_ai.request.presence_penalty"); + /** * Creates a {@code StringTag} with the given key and cache it, if it's created before, simply return it without * creating a new one. 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/ChatModelCallInterceptor.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/ChatModelCallInterceptor.java index 2e25f9e13b..a32730eab6 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/ChatModelCallInterceptor.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/ChatModelCallInterceptor.java @@ -69,6 +69,15 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr 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())); + } + if (chatOptions.getFrequencyPenalty() != null) { + Tags.GEN_AI_REQUEST_FREQUENCY_PENALTY.set(span, String.valueOf(chatOptions.getFrequencyPenalty())); + } + if (chatOptions.getPresencePenalty() != null) { + Tags.GEN_AI_REQUEST_PRESENCE_PENALTY.set(span, String.valueOf(chatOptions.getPresencePenalty())); + } } @Override 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 f1d1a99898..dd65dc9d97 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 @@ -75,6 +75,16 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr 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.getMaxTokens() != null) { + Tags.GEN_AI_REQUEST_MAX_TOKENS.set(span, String.valueOf(chatOptions.getMaxTokens())); + } + if (chatOptions.getFrequencyPenalty() != null) { + Tags.GEN_AI_REQUEST_FREQUENCY_PENALTY.set(span, String.valueOf(chatOptions.getFrequencyPenalty())); + } + if (chatOptions.getPresencePenalty() != null) { + Tags.GEN_AI_REQUEST_PRESENCE_PENALTY.set(span, String.valueOf(chatOptions.getPresencePenalty())); + } } @Override 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 8a9f4e4a76..ecf778311f 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 @@ -91,6 +91,9 @@ segmentItems: - { 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_p, value: '0.9' } + - { key: gen_ai.request.max_tokens, value: '1000' } + - { key: gen_ai.request.frequency_penalty, value: '0.5' } + - { key: gen_ai.request.presence_penalty, value: '0.3' } - { key: gen_ai.response.id, value: 'chatcmpl-CyJXJt7gxwDgz' } - { key: gen_ai.response.model, value: gpt-4.1-2025-04-14 } - { key: gen_ai.usage.input_tokens, value: '52' } @@ -116,6 +119,9 @@ 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.request.max_tokens, value: '1000' } + - { key: gen_ai.request.frequency_penalty, value: '0.5' } + - { key: gen_ai.request.presence_penalty, value: '0.3' } - { 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 } @@ -176,6 +182,9 @@ segmentItems: - { 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_p, value: '0.9' } + - { key: gen_ai.request.max_tokens, value: '1000' } + - { key: gen_ai.request.frequency_penalty, value: '0.5' } + - { key: gen_ai.request.presence_penalty, value: '0.3' } - { key: gen_ai.response.id, value: chatcmpl-DknJunZ3tgcSkKiv } - { key: gen_ai.response.model, value: gpt-4.1-2025-04-14 } - { key: gen_ai.usage.input_tokens, value: '72' } diff --git a/test/plugin/scenarios/spring-ai-1.x-scenario/src/main/resources/application.yaml b/test/plugin/scenarios/spring-ai-1.x-scenario/src/main/resources/application.yaml index fceabe447e..9f6ff8c64c 100644 --- a/test/plugin/scenarios/spring-ai-1.x-scenario/src/main/resources/application.yaml +++ b/test/plugin/scenarios/spring-ai-1.x-scenario/src/main/resources/application.yaml @@ -29,6 +29,8 @@ spring: model: gpt-4.1-2025-04-14 temperature: 0.7 max-tokens: 1000 + frequency-penalty: 0.5 + presence-penalty: 0.3 top-p: 0.9 embedding: options: