Skip to content

Commit f9730ba

Browse files
committed
fix
1 parent 4f97346 commit f9730ba

5 files changed

Lines changed: 22 additions & 26 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ private void collectContent(AbstractSpan span, Object[] allArguments, ChatRespon
140140
return;
141141
}
142142

143-
if (SpringAiPluginConfig.Plugin.SpringAi.COLLECT_PROMPT) {
143+
if (SpringAiPluginConfig.Plugin.SpringAi.COLLECT_INPUT_MESSAGES) {
144144
collectPrompt(span, allArguments);
145145
}
146146

147-
if (SpringAiPluginConfig.Plugin.SpringAi.COLLECT_COMPLETION) {
147+
if (SpringAiPluginConfig.Plugin.SpringAi.COLLECT_OUTPUT_MESSAGES) {
148148
collectCompletion(span, response);
149149
}
150150
}
@@ -162,7 +162,7 @@ private void collectPrompt(AbstractSpan span, Object[] allArguments) {
162162

163163
InputMessages inputMessages = InputMessages.fromPrompt(prompt);
164164
String inputMessagesJson = inputMessages.toJson();
165-
int limit = SpringAiPluginConfig.Plugin.SpringAi.PROMPT_LENGTH_LIMIT;
165+
int limit = SpringAiPluginConfig.Plugin.SpringAi.INPUT_MESSAGES_LENGTH_LIMIT;
166166
if (limit > 0 && inputMessagesJson.length() > limit) {
167167
inputMessagesJson = inputMessagesJson.substring(0, limit);
168168
}
@@ -174,7 +174,7 @@ private void collectCompletion(AbstractSpan span, ChatResponse response) {
174174

175175
OutputMessages outputMessages = OutputMessages.fromChatResponse(response);
176176
String outputMessagesJson = outputMessages.toJson();
177-
int limit = SpringAiPluginConfig.Plugin.SpringAi.COMPLETION_LENGTH_LIMIT;
177+
int limit = SpringAiPluginConfig.Plugin.SpringAi.OUTPUT_MESSAGES_LENGTH_LIMIT;
178178

179179
if (limit > 0 && outputMessagesJson.length() > limit) {
180180
outputMessagesJson = outputMessagesJson.substring(0, limit);

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,28 +115,25 @@ private void onStreamNext(AbstractSpan span, ChatResponse response, StreamState
115115
private void onStreamFinally(AbstractSpan span, Object[] allArguments, StreamState state) {
116116
try {
117117
ChatResponse finalResponse = state.lastResponseRef.get();
118-
if (finalResponse == null) {
119-
return;
120-
}
118+
long totalTokens = 0;
121119

122-
ChatResponseMetadata metadata = finalResponse.getMetadata();
123-
if (metadata == null) {
124-
return;
120+
if (finalResponse != null && finalResponse.getMetadata() != null) {
121+
ChatResponseMetadata metadata = finalResponse.getMetadata();
122+
collectResponseTags(span, metadata, state);
123+
collectUsageTags(span, metadata.getUsage());
124+
totalTokens = collectUsageTags(span, metadata.getUsage());
125125
}
126126

127-
collectResponseTags(span, metadata, state);
128-
129-
long totalTokens = collectUsageTags(span, metadata.getUsage());
130-
131127
int tokenThreshold = SpringAiPluginConfig.Plugin.SpringAi.CONTENT_COLLECT_THRESHOLD_TOKENS;
132128
if (tokenThreshold >= 0 && totalTokens < tokenThreshold) {
133129
return;
134130
}
135131

136-
if (SpringAiPluginConfig.Plugin.SpringAi.COLLECT_PROMPT) {
132+
if (SpringAiPluginConfig.Plugin.SpringAi.COLLECT_INPUT_MESSAGES) {
137133
collectPrompt(span, allArguments);
138134
}
139-
if (SpringAiPluginConfig.Plugin.SpringAi.COLLECT_COMPLETION) {
135+
136+
if (SpringAiPluginConfig.Plugin.SpringAi.COLLECT_OUTPUT_MESSAGES) {
140137
collectCompletion(span, state);
141138
}
142139
} catch (Throwable t) {
@@ -215,7 +212,7 @@ private void collectPrompt(AbstractSpan span, Object[] allArguments) {
215212
InputMessages inputMessages = InputMessages.fromPrompt(prompt);
216213
String inputMessagesJson = inputMessages.toJson();
217214

218-
int limit = SpringAiPluginConfig.Plugin.SpringAi.PROMPT_LENGTH_LIMIT;
215+
int limit = SpringAiPluginConfig.Plugin.SpringAi.INPUT_MESSAGES_LENGTH_LIMIT;
219216
if (limit > 0 && inputMessagesJson.length() > limit) {
220217
inputMessagesJson = inputMessagesJson.substring(0, limit);
221218
}
@@ -235,7 +232,7 @@ private void collectCompletion(AbstractSpan span, StreamState state) {
235232
OutputMessages outputMessages = OutputMessages.create().append(OutputMessages.OutputMessage.create("assistant", parts, finishReason));
236233
String outputMessagesJson = outputMessages.toJson();
237234

238-
int limit = SpringAiPluginConfig.Plugin.SpringAi.COMPLETION_LENGTH_LIMIT;
235+
int limit = SpringAiPluginConfig.Plugin.SpringAi.OUTPUT_MESSAGES_LENGTH_LIMIT;
239236
if (limit > 0 && outputMessagesJson.length() > limit) {
240237
outputMessagesJson = outputMessagesJson.substring(0, limit);
241238
}

apm-sniffer/apm-sdk-plugin/spring-plugins/spring-ai-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/ai/v1/config/SpringAiPluginConfig.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,32 @@ public static class SpringAi {
3030
/**
3131
* Whether to collect the prompt content (input text) of the GenAI request.
3232
*/
33-
public static boolean COLLECT_PROMPT = false;
33+
public static boolean COLLECT_INPUT_MESSAGES = false;
3434

3535
/**
3636
* Whether to collect the completion content (output text) of the GenAI response.
3737
*/
38-
public static boolean COLLECT_COMPLETION = false;
38+
public static boolean COLLECT_OUTPUT_MESSAGES = false;
3939

4040
/**
4141
* The maximum characters of the collected prompt content.
4242
* If the content exceeds this limit, it will be truncated.
4343
* Use a negative value to represent no limit, but be aware this could cause OOM.
4444
*/
45-
public static int PROMPT_LENGTH_LIMIT = 1024;
45+
public static int INPUT_MESSAGES_LENGTH_LIMIT = 1024;
4646

4747
/**
4848
* The maximum characters of the collected completion content.
4949
* If the content exceeds this limit, it will be truncated.
5050
* Use a negative value to represent no limit, but be aware this could cause OOM.
5151
*/
52-
public static int COMPLETION_LENGTH_LIMIT = 1024;
52+
public static int OUTPUT_MESSAGES_LENGTH_LIMIT = 1024;
5353

5454
/**
5555
* The threshold for token usage to trigger content collection.
5656
* When set to a positive value, prompt and completion will only be collected
5757
* if the total token usage of the request exceeds this threshold.
58-
* * This requires {@link #COLLECT_PROMPT} or {@link #COLLECT_COMPLETION} to be enabled first.
58+
* * This requires {@link #COLLECT_INPUT_MESSAGES} or {@link #COLLECT_OUTPUT_MESSAGES} to be enabled first.
5959
* Use a negative value to disable this threshold-based filtering (collect all).
6060
*/
6161
public static int CONTENT_COLLECT_THRESHOLD_TOKENS = -1;

apm-sniffer/apm-sdk-plugin/spring-plugins/spring-ai-1.x-plugin/src/main/resources/skywalking-plugin.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
spring-ai-1.x=org.apache.skywalking.apm.plugin.spring.ai.v1.define.ChatModelInstrumentation
1818
spring-ai-1.x=org.apache.skywalking.apm.plugin.spring.ai.v1.define.ToolCallbackInstrumentation
1919
spring-ai-1.x=org.apache.skywalking.apm.plugin.spring.ai.v1.define.DefaultToolCallingManagerInstrumentation
20-
spring-ai-1.x=org.apache.skywalking.apm.plugin.spring.ai.v1.define.DefaultToolCallingManagerInstrumentation
2120
spring-ai-1.x=org.apache.skywalking.apm.plugin.spring.ai.v1.define.provider.AnthropicApiInstrumentation
2221
spring-ai-1.x=org.apache.skywalking.apm.plugin.spring.ai.v1.define.provider.DeepSeekApiInstrumentation
2322
spring-ai-1.x=org.apache.skywalking.apm.plugin.spring.ai.v1.define.provider.HuggingfaceChatModelInstrumentation

test/plugin/scenarios/spring-ai-1.x-scenario/config/expectedData.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ segmentItems:
5656
- { key: gen_ai.usage.output_tokens, value: '17' }
5757
- { key: gen_ai.client.token.usage, value: '69' }
5858
- { key: gen_ai.response.finish_reasons, value: STOP }
59-
- { key: gen_ai.input.messages, value: '[{"role":"system","parts":[{"type":"text","content":"You are a professional technical assistant.\\nStrictly use the provided context to answer questions.\\nIf the information is not in the context, say: \\\"I''m sorry, I don''t have that information in my knowledge base.\\\"\\nDo not use outside knowledge. Be concise.\\n"}]},{"role":"user","parts":[{"type":"text","content":"What''s the weather in New York?"}]}]' }
60-
- { key: gen_ai.output.messages, value: '[{"role":"assistant","parts":[{"type":"text","content":"The weather in New York is currently sunny with a temperature of 10°C."}],"finish_reason":"stop"}]' }
59+
- { key: gen_ai.input.messages, value: not null }
60+
- { key: gen_ai.output.messages, value: not null }
6161

6262
- operationName: GET:/spring-ai-1.x-scenario/case/spring-ai-1.x-scenario-case
6363
parentSpanId: -1

0 commit comments

Comments
 (0)