From 0ceac113e356248352303dafe58a0255f02ba60a Mon Sep 17 00:00:00 2001 From: hegerchr Date: Fri, 30 Oct 2026 11:42:34 +0100 Subject: [PATCH 01/15] Add Spring AI auto-instrumentation --- CHANGELOG.next-release.md | 1 + apm-agent-builds/pom.xml | 5 + .../apm-spring-ai-plugin/pom.xml | 58 ++++ .../AbstractSpringAiInstrumentation.java | 41 +++ .../springai/ChatModelInstrumentation.java | 77 +++++ .../EmbeddingModelInstrumentation.java | 71 +++++ .../agent/springai/SpringAiSpanHelper.java | 293 ++++++++++++++++++ .../StreamingChatModelInstrumentation.java | 76 +++++ .../springai/ToolCallbackInstrumentation.java | 68 ++++ .../springai/VectorStoreInstrumentation.java | 81 +++++ ...ic.apm.agent.sdk.ElasticApmInstrumentation | 5 + .../springai/SpringAiInstrumentationTest.java | 257 +++++++++++++++ apm-agent-plugins/pom.xml | 1 + docs/reference/config-core.md | 4 +- .../config-reference-properties-file.md | 4 +- docs/reference/supported-technologies.md | 8 + pom.xml | 2 +- 17 files changed, 1047 insertions(+), 5 deletions(-) create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/pom.xml create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/AbstractSpringAiInstrumentation.java create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/StreamingChatModelInstrumentation.java create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java diff --git a/CHANGELOG.next-release.md b/CHANGELOG.next-release.md index 6850e01088..14d9e9ee98 100644 --- a/CHANGELOG.next-release.md +++ b/CHANGELOG.next-release.md @@ -13,6 +13,7 @@ This file contains all changes which are not released yet. # Features and enhancements +- Add experimental auto-instrumentation for Spring AI 2.x chat models, embedding models, vector stores, and tool callbacks # Deprecations diff --git a/apm-agent-builds/pom.xml b/apm-agent-builds/pom.xml index 50948a497b..dd39521454 100644 --- a/apm-agent-builds/pom.xml +++ b/apm-agent-builds/pom.xml @@ -346,6 +346,11 @@ apm-sparkjava-plugin ${project.version} + + ${project.groupId} + apm-spring-ai-plugin + ${project.version} + ${project.groupId} apm-spring-resttemplate-plugin diff --git a/apm-agent-plugins/apm-spring-ai-plugin/pom.xml b/apm-agent-plugins/apm-spring-ai-plugin/pom.xml new file mode 100644 index 0000000000..64152ca9fa --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + + co.elastic.apm + apm-agent-plugins + 1.57.2-SNAPSHOT + + + apm-spring-ai-plugin + ${project.groupId}:${project.artifactId} + + + ${project.basedir}/../.. + true + 8 + 8 + 2.0.2 + 3.8.8 + 2.24.3 + + + + + org.springframework.ai + spring-ai-model + ${version.spring-ai} + provided + + + org.springframework.ai + spring-ai-vector-store + ${version.spring-ai} + provided + + + io.projectreactor + reactor-core + ${version.reactor} + provided + + + + org.apache.logging.log4j + log4j-api + ${version.log4j2-test} + test + + + org.apache.logging.log4j + log4j-core + ${version.log4j2-test} + test + + + diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/AbstractSpringAiInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/AbstractSpringAiInstrumentation.java new file mode 100644 index 0000000000..83598e72dc --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/AbstractSpringAiInstrumentation.java @@ -0,0 +1,41 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.sdk.ElasticApmInstrumentation; +import net.bytebuddy.matcher.ElementMatcher; + +import java.util.Arrays; +import java.util.Collection; + +import static co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass; + +abstract class AbstractSpringAiInstrumentation extends ElasticApmInstrumentation { + + @Override + public ElementMatcher.Junction getClassLoaderMatcher() { + return classLoaderCanLoadClass("org.springframework.ai.model.Model") + .and(classLoaderCanLoadClass("org.springframework.ai.observation.conventions.AiOperationType")); + } + + @Override + public Collection getInstrumentationGroupNames() { + return Arrays.asList("spring-ai", "experimental"); + } +} diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java new file mode 100644 index 0000000000..1359ecda7b --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java @@ -0,0 +1,77 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.tracer.Span; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.NamedElement; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.prompt.Prompt; + +import javax.annotation.Nullable; + +import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; +import static net.bytebuddy.matcher.ElementMatchers.isInterface; +import static net.bytebuddy.matcher.ElementMatchers.nameContains; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.not; +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class ChatModelInstrumentation extends AbstractSpringAiInstrumentation { + + @Override + public ElementMatcher getTypeMatcherPreFilter() { + return nameContains("ChatModel"); + } + + @Override + public ElementMatcher getTypeMatcher() { + return not(isInterface()).and(hasSuperType(named("org.springframework.ai.chat.model.ChatModel"))); + } + + @Override + public ElementMatcher getMethodMatcher() { + return named("call").and(takesArguments(1)) + .and(takesArgument(0, named("org.springframework.ai.chat.prompt.Prompt"))); + } + + @Override + public String getAdviceClassName() { + return getClass().getName() + "$ChatModelAdvice"; + } + + public static class ChatModelAdvice { + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + public static Object onEnter(@Advice.This Object model, @Advice.Argument(0) Prompt prompt) { + Span span = SpringAiSpanHelper.startModelSpan(model, "chat", prompt.getOptions(), false); + return span == null ? null : span.activate(); + } + + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false) + public static void onExit(@Advice.Enter @Nullable Object spanObject, + @Advice.Return @Nullable ChatResponse response, + @Advice.Thrown @Nullable Throwable thrown) { + SpringAiSpanHelper.endChatSpan((Span) spanObject, response, thrown); + } + } +} diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java new file mode 100644 index 0000000000..f6491a07e0 --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java @@ -0,0 +1,71 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.tracer.Span; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.NamedElement; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.springframework.ai.embedding.EmbeddingRequest; +import org.springframework.ai.embedding.EmbeddingResponse; + +import javax.annotation.Nullable; + +import static net.bytebuddy.matcher.ElementMatchers.*; + +public class EmbeddingModelInstrumentation extends AbstractSpringAiInstrumentation { + + @Override + public ElementMatcher getTypeMatcherPreFilter() { + return nameContains("Embedding"); + } + + @Override + public ElementMatcher getTypeMatcher() { + return not(isInterface()).and(hasSuperType(named("org.springframework.ai.embedding.EmbeddingModel"))); + } + + @Override + public ElementMatcher getMethodMatcher() { + return named("call").and(takesArguments(1)) + .and(takesArgument(0, named("org.springframework.ai.embedding.EmbeddingRequest"))); + } + + @Override + public String getAdviceClassName() { + return getClass().getName() + "$EmbeddingModelAdvice"; + } + + public static class EmbeddingModelAdvice { + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + public static Object onEnter(@Advice.This Object model, @Advice.Argument(0) EmbeddingRequest request) { + Span span = SpringAiSpanHelper.startEmbeddingSpan(model, request); + return span == null ? null : span.activate(); + } + + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false) + public static void onExit(@Advice.Enter @Nullable Object spanObject, + @Advice.Return @Nullable EmbeddingResponse response, + @Advice.Thrown @Nullable Throwable thrown) { + SpringAiSpanHelper.endEmbeddingSpan((Span) spanObject, response, thrown); + } + } +} diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java new file mode 100644 index 0000000000..0ecb3c8a47 --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java @@ -0,0 +1,293 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.tracer.GlobalTracer; +import co.elastic.apm.agent.tracer.Outcome; +import co.elastic.apm.agent.tracer.Span; +import co.elastic.apm.agent.tracer.TraceState; +import co.elastic.apm.agent.tracer.Tracer; +import org.springframework.ai.chat.metadata.ChatGenerationMetadata; +import org.springframework.ai.chat.metadata.ChatResponseMetadata; +import org.springframework.ai.chat.metadata.Usage; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.model.Generation; +import org.springframework.ai.chat.prompt.ChatOptions; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.embedding.EmbeddingOptions; +import org.springframework.ai.embedding.EmbeddingRequest; +import org.springframework.ai.embedding.EmbeddingResponse; +import org.springframework.ai.embedding.EmbeddingResponseMetadata; +import org.springframework.ai.tool.ToolCallback; +import org.springframework.ai.tool.definition.ToolDefinition; +import org.springframework.ai.vectorstore.SearchRequest; +import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore; +import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext; +import reactor.core.publisher.Flux; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + +final class SpringAiSpanHelper { + + private static final Tracer tracer = GlobalTracer.get(); + + private SpringAiSpanHelper() { + } + + @Nullable + static Span startModelSpan(Object model, String operation, @Nullable ChatOptions options, boolean streaming) { + Span span = tracer.currentContext().createSpan(); + if (span == null) { + return null; + } + + String requestModel = options == null ? null : options.getModel(); + String provider = providerFor(model); + span.withName(spanName(operation, requestModel)) + .withType("app") + .withSubtype("internal") + .withAction(operation) + .withOtelAttribute("gen_ai.operation.name", operation) + .withOtelAttribute("gen_ai.system", provider) + .withOtelAttribute("gen_ai.request.model", requestModel) + .withOtelAttribute("gen_ai.request.stream", streaming); + + if (options != null) { + span.withOtelAttribute("gen_ai.request.frequency_penalty", options.getFrequencyPenalty()) + .withOtelAttribute("gen_ai.request.max_tokens", options.getMaxTokens()) + .withOtelAttribute("gen_ai.request.presence_penalty", options.getPresencePenalty()) + .withOtelAttribute("gen_ai.request.stop_sequences", stringify(options.getStopSequences())) + .withOtelAttribute("gen_ai.request.temperature", options.getTemperature()) + .withOtelAttribute("gen_ai.request.top_k", options.getTopK()) + .withOtelAttribute("gen_ai.request.top_p", options.getTopP()); + } + return span; + } + + @Nullable + static Span startEmbeddingSpan(Object model, EmbeddingRequest request) { + EmbeddingOptions options = request.getOptions(); + Span span = tracer.currentContext().createSpan(); + if (span == null) { + return null; + } + String requestModel = options == null ? null : options.getModel(); + String provider = providerFor(model); + span.withName(spanName("embedding", requestModel)) + .withType("app") + .withSubtype("internal") + .withAction("embedding") + .withOtelAttribute("gen_ai.operation.name", "embedding") + .withOtelAttribute("gen_ai.system", provider) + .withOtelAttribute("gen_ai.request.model", requestModel) + .withOtelAttribute("gen_ai.request.embedding.dimensions", options == null ? null : options.getDimensions()); + return span; + } + + @Nullable + static Span startVectorStoreSpan(AbstractObservationVectorStore vectorStore, String operation, + @Nullable SearchRequest searchRequest) { + Span span = tracer.currentContext().createExitSpan(); + if (span == null) { + return null; + } + VectorStoreObservationContext.Builder builder = vectorStore.createObservationContextBuilder(operation); + if (searchRequest != null) { + builder.queryRequest(searchRequest); + } + VectorStoreObservationContext context = builder.build(); + String rawDbSystem = context.getDatabaseSystem(); + String dbSystem = rawDbSystem == null || rawDbSystem.isEmpty() ? "unknown" : rawDbSystem; + span.withName(dbSystem + " " + operation) + .withType("db") + .withSubtype(dbSubtype(dbSystem)) + .withAction(operation) + .withOtelAttribute("spring.ai.kind", "vector_store") + .withOtelAttribute("db.system", dbSystem) + .withOtelAttribute("db.operation.name", operation) + .withOtelAttribute("db.collection.name", context.getCollectionName()) + .withOtelAttribute("db.namespace", context.getNamespace()) + .withOtelAttribute("db.search.similarity_metric", context.getSimilarityMetric()) + .withOtelAttribute("db.vector.dimension_count", context.getDimensions()) + .withOtelAttribute("db.vector.field_name", context.getFieldName()); + if (searchRequest != null) { + span.withOtelAttribute("db.vector.query.top_k", searchRequest.getTopK()) + .withOtelAttribute("db.vector.query.similarity_threshold", searchRequest.getSimilarityThreshold()); + } + span.getContext().getServiceTarget().withType(dbSystem).withName(context.getCollectionName()); + return span; + } + + @Nullable + static Span startToolSpan(ToolCallback callback) { + Span span = tracer.currentContext().createSpan(); + if (span == null) { + return null; + } + ToolDefinition definition = callback.getToolDefinition(); + String name = definition.name(); + return span.withName("execute_tool " + name) + .withType("app") + .withSubtype("internal") + .withAction("execute_tool") + .withOtelAttribute("gen_ai.operation.name", "execute_tool") + .withOtelAttribute("gen_ai.system", "spring_ai") + .withOtelAttribute("spring.ai.kind", "tool_call") + .withOtelAttribute("spring.ai.tool.type", "function") + .withOtelAttribute("spring.ai.tool.definition.name", name) + .withOtelAttribute("spring.ai.tool.definition.description", definition.description()) + .withOtelAttribute("spring.ai.tool.definition.schema", definition.inputSchema()); + } + + @SuppressWarnings("unchecked") + static Flux wrapChatFlux(final Object model, final Prompt prompt, final Flux source) { + final Flux typedSource = (Flux) source; + return Flux.defer(() -> { + final Span span = startModelSpan(model, "chat", prompt.getOptions(), true); + if (span == null) { + return typedSource; + } + final AtomicReference lastResponse = new AtomicReference<>(); + final AtomicReference failure = new AtomicReference<>(); + final AtomicBoolean ended = new AtomicBoolean(); + span.withSync(false); + return typedSource + .doOnNext(value -> { + if (value instanceof ChatResponse) { + lastResponse.set((ChatResponse) value); + } + }) + .doOnError(failure::set) + .doFinally(signal -> { + if (ended.compareAndSet(false, true)) { + addChatResponseAttributes(span, lastResponse.get()); + endAsyncSpan(span, failure.get()); + } + }) + .contextWrite(context -> context.put(TraceState.class, span)); + }); + } + + static void endChatSpan(@Nullable Span span, @Nullable ChatResponse response, @Nullable Throwable thrown) { + if (span == null) { + return; + } + addChatResponseAttributes(span, response); + endSpan(span, thrown); + } + + private static void addChatResponseAttributes(Span span, @Nullable ChatResponse response) { + if (response != null) { + ChatResponseMetadata metadata = response.getMetadata(); + if (metadata != null) { + span.withOtelAttribute("gen_ai.response.id", metadata.getId()) + .withOtelAttribute("gen_ai.response.model", metadata.getModel()); + addUsage(span, metadata.getUsage()); + } + List finishReasons = new ArrayList<>(); + for (Generation generation : response.getResults()) { + ChatGenerationMetadata generationMetadata = generation.getMetadata(); + if (generationMetadata != null && generationMetadata.getFinishReason() != null) { + finishReasons.add(generationMetadata.getFinishReason()); + } + } + span.withOtelAttribute("gen_ai.response.finish_reasons", stringify(finishReasons)); + } + } + + static void endEmbeddingSpan(@Nullable Span span, @Nullable EmbeddingResponse response, + @Nullable Throwable thrown) { + if (span != null && response != null) { + EmbeddingResponseMetadata metadata = response.getMetadata(); + if (metadata != null) { + span.withOtelAttribute("gen_ai.response.model", metadata.getModel()); + addUsage(span, metadata.getUsage()); + } + } + endSpan(span, thrown); + } + + static void endSpan(@Nullable Span span, @Nullable Throwable thrown) { + if (span != null) { + span.captureException(thrown) + .withOutcome(thrown == null ? Outcome.SUCCESS : Outcome.FAILURE) + .deactivate() + .end(); + } + } + + private static void endAsyncSpan(Span span, @Nullable Throwable thrown) { + span.captureException(thrown) + .withOutcome(thrown == null ? Outcome.SUCCESS : Outcome.FAILURE) + .end(); + } + + private static void addUsage(Span span, @Nullable Usage usage) { + if (usage != null) { + span.withOtelAttribute("gen_ai.usage.input_tokens", usage.getPromptTokens()) + .withOtelAttribute("gen_ai.usage.output_tokens", usage.getCompletionTokens()) + .withOtelAttribute("gen_ai.usage.total_tokens", usage.getTotalTokens()) + .withOtelAttribute("gen_ai.usage.cache_read.input_tokens", usage.getCacheReadInputTokens()) + .withOtelAttribute("gen_ai.usage.cache_creation.input_tokens", usage.getCacheWriteInputTokens()); + } + } + + private static String spanName(String operation, @Nullable String model) { + return model == null || model.isEmpty() ? operation : operation + " " + model; + } + + @Nullable + private static String stringify(@Nullable List values) { + return values == null || values.isEmpty() ? null : values.toString(); + } + + private static String providerFor(Object model) { + String className = model.getClass().getName().toLowerCase(Locale.ROOT); + if (className.contains("anthropic")) return "anthropic"; + if (className.contains("bedrock")) return "bedrock_converse"; + if (className.contains("deepseek")) return "deepseek"; + if (className.contains("vertex")) return "vertex_ai"; + if (className.contains("google") || className.contains("gemini")) return "google_genai"; + if (className.contains("mistral")) return "mistral_ai"; + if (className.contains("ollama")) return "ollama"; + if (className.contains("onnx")) return "onnx"; + if (className.contains("openai") || className.contains("azure")) return "openai"; + if (className.contains("stability")) return "stability_ai"; + return "spring_ai"; + } + + private static String dbSubtype(@Nullable String databaseSystem) { + if (databaseSystem == null) { + return "unknown"; + } + String normalized = databaseSystem.toLowerCase(Locale.ROOT); + if (normalized.contains("postgres") || normalized.contains("pg_vector")) return "postgresql"; + if (normalized.contains("mongo")) return "mongodb"; + if (normalized.contains("elastic")) return "elasticsearch"; + if (normalized.contains("redis")) return "redis"; + if (normalized.contains("cassandra")) return "cassandra"; + if (normalized.contains("cosmos")) return "cosmosdb"; + return "unknown"; + } +} diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/StreamingChatModelInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/StreamingChatModelInstrumentation.java new file mode 100644 index 0000000000..cf7ea1906f --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/StreamingChatModelInstrumentation.java @@ -0,0 +1,76 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.NamedElement; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.springframework.ai.chat.prompt.Prompt; +import reactor.core.publisher.Flux; + +import javax.annotation.Nullable; + +import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; +import static net.bytebuddy.matcher.ElementMatchers.isInterface; +import static net.bytebuddy.matcher.ElementMatchers.nameContains; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.not; +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class StreamingChatModelInstrumentation extends AbstractSpringAiInstrumentation { + + @Override + public ElementMatcher getTypeMatcherPreFilter() { + return nameContains("ChatModel"); + } + + @Override + public ElementMatcher getTypeMatcher() { + return not(isInterface()).and(hasSuperType(named("org.springframework.ai.chat.model.StreamingChatModel"))); + } + + @Override + public ElementMatcher getMethodMatcher() { + return named("stream").and(takesArguments(1)) + .and(takesArgument(0, named("org.springframework.ai.chat.prompt.Prompt"))); + } + + @Override + public String getAdviceClassName() { + return getClass().getName() + "$StreamingChatModelAdvice"; + } + + public static class StreamingChatModelAdvice { + @Nullable + @Advice.AssignReturned.ToReturned + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false) + public static Flux onExit(@Advice.This Object model, + @Advice.Argument(0) Prompt prompt, + @Advice.Return @Nullable Flux flux, + @Advice.Thrown @Nullable Throwable thrown) { + if (thrown == null && flux != null) { + return SpringAiSpanHelper.wrapChatFlux(model, prompt, flux); + } + return flux; + } + } +} diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java new file mode 100644 index 0000000000..bb1403677a --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java @@ -0,0 +1,68 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.tracer.Span; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.NamedElement; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.springframework.ai.tool.ToolCallback; + +import javax.annotation.Nullable; + +import static net.bytebuddy.matcher.ElementMatchers.*; + +public class ToolCallbackInstrumentation extends AbstractSpringAiInstrumentation { + + @Override + public ElementMatcher getTypeMatcherPreFilter() { + return nameContains("Tool").or(nameContains("Callback")); + } + + @Override + public ElementMatcher getTypeMatcher() { + return not(isInterface()).and(hasSuperType(named("org.springframework.ai.tool.ToolCallback"))); + } + + @Override + public ElementMatcher getMethodMatcher() { + return named("call").and(takesArguments(String.class)); + } + + @Override + public String getAdviceClassName() { + return getClass().getName() + "$ToolCallbackAdvice"; + } + + public static class ToolCallbackAdvice { + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + public static Object onEnter(@Advice.This ToolCallback callback) { + Span span = SpringAiSpanHelper.startToolSpan(callback); + return span == null ? null : span.activate(); + } + + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false) + public static void onExit(@Advice.Enter @Nullable Object spanObject, + @Advice.Thrown @Nullable Throwable thrown) { + SpringAiSpanHelper.endSpan((Span) spanObject, thrown); + } + } +} diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java new file mode 100644 index 0000000000..3a6f779768 --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java @@ -0,0 +1,81 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.tracer.Span; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.NamedElement; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.springframework.ai.vectorstore.SearchRequest; +import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore; + +import javax.annotation.Nullable; + +import static net.bytebuddy.matcher.ElementMatchers.*; + +public class VectorStoreInstrumentation extends AbstractSpringAiInstrumentation { + + @Override + public ElementMatcher.Junction getClassLoaderMatcher() { + return super.getClassLoaderMatcher() + .and(co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass( + "org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore")); + } + + @Override + public ElementMatcher getTypeMatcherPreFilter() { + return nameContains("VectorStore"); + } + + @Override + public ElementMatcher getTypeMatcher() { + return not(isInterface()).and(hasSuperType(named( + "org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore"))); + } + + @Override + public ElementMatcher getMethodMatcher() { + return namedOneOf("add", "delete", "similaritySearch").and(takesArguments(1)); + } + + @Override + public String getAdviceClassName() { + return getClass().getName() + "$VectorStoreAdvice"; + } + + public static class VectorStoreAdvice { + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + public static Object onEnter(@Advice.This AbstractObservationVectorStore vectorStore, + @Advice.Origin("#m") String method, + @Advice.Argument(0) @Nullable Object argument) { + String operation = "similaritySearch".equals(method) ? "query" : method; + SearchRequest searchRequest = argument instanceof SearchRequest ? (SearchRequest) argument : null; + Span span = SpringAiSpanHelper.startVectorStoreSpan(vectorStore, operation, searchRequest); + return span == null ? null : span.activate(); + } + + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false) + public static void onExit(@Advice.Enter @Nullable Object spanObject, + @Advice.Thrown @Nullable Throwable thrown) { + SpringAiSpanHelper.endSpan((Span) spanObject, thrown); + } + } +} diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation b/apm-agent-plugins/apm-spring-ai-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation new file mode 100644 index 0000000000..578358975c --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation @@ -0,0 +1,5 @@ +co.elastic.apm.agent.springai.ChatModelInstrumentation +co.elastic.apm.agent.springai.StreamingChatModelInstrumentation +co.elastic.apm.agent.springai.EmbeddingModelInstrumentation +co.elastic.apm.agent.springai.VectorStoreInstrumentation +co.elastic.apm.agent.springai.ToolCallbackInstrumentation diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java new file mode 100644 index 0000000000..700cdb4585 --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java @@ -0,0 +1,257 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.AbstractInstrumentationTest; +import co.elastic.apm.agent.impl.transaction.SpanImpl; +import co.elastic.apm.agent.impl.transaction.TransactionImpl; +import org.junit.jupiter.api.Test; +import org.springframework.ai.chat.messages.AssistantMessage; +import org.springframework.ai.chat.metadata.ChatGenerationMetadata; +import org.springframework.ai.chat.metadata.ChatResponseMetadata; +import org.springframework.ai.chat.metadata.DefaultUsage; +import org.springframework.ai.chat.model.ChatModel; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.model.Generation; +import org.springframework.ai.chat.prompt.ChatOptions; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.document.Document; +import org.springframework.ai.embedding.Embedding; +import org.springframework.ai.embedding.EmbeddingModel; +import org.springframework.ai.embedding.EmbeddingOptions; +import org.springframework.ai.embedding.EmbeddingRequest; +import org.springframework.ai.embedding.EmbeddingResponse; +import org.springframework.ai.embedding.EmbeddingResponseMetadata; +import org.springframework.ai.tool.ToolCallback; +import org.springframework.ai.tool.definition.ToolDefinition; +import org.springframework.ai.vectorstore.AbstractVectorStoreBuilder; +import org.springframework.ai.vectorstore.SearchRequest; +import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore; +import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext; +import reactor.core.publisher.Flux; + +import java.util.Collections; +import java.util.List; + +import static co.elastic.apm.agent.testutils.assertions.Assertions.assertThat; + +class SpringAiInstrumentationTest extends AbstractInstrumentationTest { + + @Test + void capturesChatCallAndSemanticAttributes() { + TransactionImpl transaction = startTestRootTransaction(); + try { + ChatOptions options = ChatOptions.builder().model("gpt-test").temperature(0.2).maxTokens(42).build(); + new OpenAiTestChatModel().call(new Prompt("secret prompt", options)); + } finally { + transaction.deactivate().end(); + } + + SpanImpl span = reporter.getFirstSpan(); + assertThat(span) + .hasName("chat gpt-test") + .hasType("app") + .hasSubType("internal") + .hasAction("chat") + .hasOutcome(co.elastic.apm.agent.tracer.Outcome.SUCCESS) + .hasOtelAttribute("gen_ai.operation.name", "chat") + .hasOtelAttribute("gen_ai.system", "openai") + .hasOtelAttribute("gen_ai.request.model", "gpt-test") + .hasOtelAttribute("gen_ai.response.model", "gpt-test-result") + .hasOtelAttribute("gen_ai.usage.input_tokens", 10) + .hasOtelAttribute("gen_ai.usage.output_tokens", 5) + .hasOtelAttribute("gen_ai.usage.total_tokens", 15); + org.assertj.core.api.Assertions.assertThat(span.getOtelAttributes()).doesNotContainKey("gen_ai.input.messages"); + } + + @Test + void capturesStreamingChatUntilFluxTerminates() { + TransactionImpl transaction = startTestRootTransaction(); + try { + ChatOptions options = ChatOptions.builder().model("gpt-stream").build(); + new OpenAiTestChatModel().stream(new Prompt("secret stream prompt", options)).blockLast(); + } finally { + transaction.deactivate().end(); + } + + assertThat(reporter.getFirstSpan()) + .hasName("chat gpt-stream") + .hasOtelAttribute("gen_ai.request.stream", true) + .hasOtelAttribute("gen_ai.usage.total_tokens", 15); + } + + @Test + void capturesEmbeddingCall() { + TransactionImpl transaction = startTestRootTransaction(); + try { + EmbeddingOptions options = EmbeddingOptions.builder().model("embedding-test").dimensions(3).build(); + new OpenAiTestEmbeddingModel().call(new EmbeddingRequest(Collections.singletonList("secret"), options)); + } finally { + transaction.deactivate().end(); + } + + assertThat(reporter.getFirstSpan()) + .hasName("embedding embedding-test") + .hasAction("embedding") + .hasOtelAttribute("gen_ai.request.embedding.dimensions", 3) + .hasOtelAttribute("gen_ai.usage.input_tokens", 4); + } + + @Test + void capturesToolDefinitionButNotArgumentsOrResult() { + TransactionImpl transaction = startTestRootTransaction(); + try { + new TestToolCallback().call("{\"password\":\"secret\"}"); + } finally { + transaction.deactivate().end(); + } + + SpanImpl span = reporter.getFirstSpan(); + assertThat(span) + .hasName("execute_tool weather") + .hasType("app") + .hasSubType("internal") + .hasAction("execute_tool") + .hasOtelAttribute("spring.ai.tool.definition.name", "weather"); + org.assertj.core.api.Assertions.assertThat(span.getOtelAttributes()) + .doesNotContainKeys("spring.ai.tool.call.arguments", "spring.ai.tool.call.result"); + } + + @Test + void capturesVectorStoreQueryWithoutQueryText() { + TestVectorStore vectorStore = new TestVectorStore(new OpenAiTestEmbeddingModel()); + + TransactionImpl transaction = startTestRootTransaction(); + try { + vectorStore.similaritySearch(SearchRequest.builder() + .query("secret search query") + .topK(3) + .similarityThreshold(0.5) + .build()); + } finally { + transaction.deactivate().end(); + } + + SpanImpl span = reporter.getFirstSpan(); + assertThat(span) + .hasName("simple query") + .hasType("db") + .hasSubType("unknown") + .hasAction("query") + .hasOtelAttribute("db.system", "simple") + .hasOtelAttribute("db.operation.name", "query") + .hasOtelAttribute("db.vector.query.top_k", 3) + .hasOtelAttribute("db.vector.query.similarity_threshold", 0.5); + org.assertj.core.api.Assertions.assertThat(span.getOtelAttributes()) + .doesNotContainKeys("db.query.text", "db.vector.query.content", "db.vector.query.filter"); + } + + static class OpenAiTestChatModel implements ChatModel { + @Override + public ChatResponse call(Prompt prompt) { + return response(); + } + + @Override + public Flux stream(Prompt prompt) { + return Flux.just(response()); + } + + private static ChatResponse response() { + Generation generation = new Generation(new AssistantMessage("secret response"), + ChatGenerationMetadata.builder().finishReason("stop").build()); + ChatResponseMetadata metadata = ChatResponseMetadata.builder() + .id("response-id") + .model("gpt-test-result") + .usage(new DefaultUsage(10, 5, 15)) + .build(); + return new ChatResponse(Collections.singletonList(generation), metadata); + } + } + + static class OpenAiTestEmbeddingModel implements EmbeddingModel { + @Override + public EmbeddingResponse call(EmbeddingRequest request) { + return new EmbeddingResponse( + Collections.singletonList(new Embedding(new float[]{1, 2, 3}, 0)), + new EmbeddingResponseMetadata("embedding-result", new DefaultUsage(4, 0, 4))); + } + + @Override + public float[] embed(Document document) { + return new float[]{1, 2, 3}; + } + } + + static class TestVectorStore extends AbstractObservationVectorStore { + + TestVectorStore(EmbeddingModel embeddingModel) { + super(new TestVectorStoreBuilder(embeddingModel)); + } + + @Override + public void doAdd(List documents) { + } + + @Override + public void doDelete(List ids) { + } + + @Override + public List doSimilaritySearch(SearchRequest request) { + return Collections.emptyList(); + } + + @Override + public VectorStoreObservationContext.Builder createObservationContextBuilder(String operation) { + return VectorStoreObservationContext.builder("simple", operation) + .collectionName("test-collection") + .dimensions(3) + .similarityMetric("cosine"); + } + } + + static class TestVectorStoreBuilder extends AbstractVectorStoreBuilder { + + TestVectorStoreBuilder(EmbeddingModel embeddingModel) { + super(embeddingModel); + } + + @Override + public TestVectorStore build() { + return new TestVectorStore(embeddingModel); + } + } + + static class TestToolCallback implements ToolCallback { + @Override + public ToolDefinition getToolDefinition() { + return ToolDefinition.builder() + .name("weather") + .description("Gets the weather") + .inputSchema("{\"type\":\"object\"}") + .build(); + } + + @Override + public String call(String toolInput) { + return "secret result"; + } + } +} diff --git a/apm-agent-plugins/pom.xml b/apm-agent-plugins/pom.xml index ddca5256d7..c67cf2097b 100644 --- a/apm-agent-plugins/pom.xml +++ b/apm-agent-plugins/pom.xml @@ -28,6 +28,7 @@ apm-api-plugin apm-apache-httpclient apm-spring-resttemplate + apm-spring-ai-plugin apm-httpclient-core apm-httpserver-core apm-logging-plugin diff --git a/docs/reference/config-core.md b/docs/reference/config-core.md index 3d00506024..423d5f02f8 100644 --- a/docs/reference/config-core.md +++ b/docs/reference/config-core.md @@ -328,7 +328,7 @@ Review the data captured by Elastic APM carefully to make sure it does not captu apm_agent_java: ga 1.28.0 ``` -A list of instrumentations which should be selectively enabled. Valid options are `annotations`, `annotations-capture-span`, `annotations-capture-transaction`, `annotations-traced`, `apache-commons-exec`, `apache-httpclient`, `asynchttpclient`, `aws-lambda`, `aws-sdk`, `cassandra`, `concurrent`, `dubbo`, `elasticsearch-restclient`, `exception-handler`, `executor`, `executor-collection`, `experimental`, `finagle-httpclient`, `fork-join`, `grails`, `grpc`, `hibernate-search`, `http-client`, `jakarta-websocket`, `java-ldap`, `javalin`, `javax-websocket`, `jax-rs`, `jax-ws`, `jdbc`, `jdk-httpclient`, `jdk-httpserver`, `jedis`, `jms`, `jsf`, `kafka`, `lettuce`, `log-correlation`, `log-error`, `log-reformatting`, `logging`, `micrometer`, `mongodb`, `mongodb-client`, `okhttp`, `opentelemetry`, `opentelemetry-annotations`, `opentelemetry-metrics`, `opentracing`, `process`, `public-api`, `quartz`, `rabbitmq`, `reactor`, `redis`, `redisson`, `render`, `scala-future`, `scheduled`, `servlet-api`, `servlet-api-async`, `servlet-api-dispatch`, `servlet-input-stream`, `servlet-service-name`, `servlet-version`, `sparkjava`, `spring-amqp`, `spring-mvc`, `spring-resttemplate`, `spring-service-name`, `spring-view-render`, `spring-webclient`, `spring-webflux`, `ssl-context`, `struts`, `timer-task`, `urlconnection`, `vertx`, `vertx-web`, `vertx-webclient`, `websocket`. When set to non-empty value, only listed instrumentations will be enabled if they are not disabled through [`disable_instrumentations` ([1.0.0])](#config-disable-instrumentations) or [`enable_experimental_instrumentations` ([1.25.0])](#config-enable-experimental-instrumentations). When not set or empty (default), all instrumentations enabled by default will be enabled unless they are disabled through [`disable_instrumentations` ([1.0.0])](#config-disable-instrumentations) or [`enable_experimental_instrumentations` ([1.25.0])](#config-enable-experimental-instrumentations). +A list of instrumentations which should be selectively enabled. Valid options are `annotations`, `annotations-capture-span`, `annotations-capture-transaction`, `annotations-traced`, `apache-commons-exec`, `apache-httpclient`, `asynchttpclient`, `aws-lambda`, `aws-sdk`, `cassandra`, `concurrent`, `dubbo`, `elasticsearch-restclient`, `exception-handler`, `executor`, `executor-collection`, `experimental`, `finagle-httpclient`, `fork-join`, `grails`, `grpc`, `hibernate-search`, `http-client`, `jakarta-websocket`, `java-ldap`, `javalin`, `javax-websocket`, `jax-rs`, `jax-ws`, `jdbc`, `jdk-httpclient`, `jdk-httpserver`, `jedis`, `jms`, `jsf`, `kafka`, `lettuce`, `log-correlation`, `log-error`, `log-reformatting`, `logging`, `micrometer`, `mongodb`, `mongodb-client`, `okhttp`, `opentelemetry`, `opentelemetry-annotations`, `opentelemetry-metrics`, `opentracing`, `process`, `public-api`, `quartz`, `rabbitmq`, `reactor`, `redis`, `redisson`, `render`, `scala-future`, `scheduled`, `servlet-api`, `servlet-api-async`, `servlet-api-dispatch`, `servlet-input-stream`, `servlet-service-name`, `servlet-version`, `sparkjava`, `spring-ai`, `spring-amqp`, `spring-mvc`, `spring-resttemplate`, `spring-service-name`, `spring-view-render`, `spring-webclient`, `spring-webflux`, `ssl-context`, `struts`, `timer-task`, `urlconnection`, `vertx`, `vertx-web`, `vertx-webclient`, `websocket`. When set to non-empty value, only listed instrumentations will be enabled if they are not disabled through [`disable_instrumentations` ([1.0.0])](#config-disable-instrumentations) or [`enable_experimental_instrumentations` ([1.25.0])](#config-enable-experimental-instrumentations). When not set or empty (default), all instrumentations enabled by default will be enabled unless they are disabled through [`disable_instrumentations` ([1.0.0])](#config-disable-instrumentations) or [`enable_experimental_instrumentations` ([1.25.0])](#config-enable-experimental-instrumentations). ::::{note} Changing this value at runtime can slow down the application temporarily. @@ -352,7 +352,7 @@ Changing this value at runtime can slow down the application temporarily. apm_agent_java: ga 1.0.0 ``` -A list of instrumentations which should be disabled. Valid options are `annotations`, `annotations-capture-span`, `annotations-capture-transaction`, `annotations-traced`, `apache-commons-exec`, `apache-httpclient`, `asynchttpclient`, `aws-lambda`, `aws-sdk`, `cassandra`, `concurrent`, `dubbo`, `elasticsearch-restclient`, `exception-handler`, `executor`, `executor-collection`, `experimental`, `finagle-httpclient`, `fork-join`, `grails`, `grpc`, `hibernate-search`, `http-client`, `jakarta-websocket`, `java-ldap`, `javalin`, `javax-websocket`, `jax-rs`, `jax-ws`, `jdbc`, `jdk-httpclient`, `jdk-httpserver`, `jedis`, `jms`, `jsf`, `kafka`, `lettuce`, `log-correlation`, `log-error`, `log-reformatting`, `logging`, `micrometer`, `mongodb`, `mongodb-client`, `okhttp`, `opentelemetry`, `opentelemetry-annotations`, `opentelemetry-metrics`, `opentracing`, `process`, `public-api`, `quartz`, `rabbitmq`, `reactor`, `redis`, `redisson`, `render`, `scala-future`, `scheduled`, `servlet-api`, `servlet-api-async`, `servlet-api-dispatch`, `servlet-input-stream`, `servlet-service-name`, `servlet-version`, `sparkjava`, `spring-amqp`, `spring-mvc`, `spring-resttemplate`, `spring-service-name`, `spring-view-render`, `spring-webclient`, `spring-webflux`, `ssl-context`, `struts`, `timer-task`, `urlconnection`, `vertx`, `vertx-web`, `vertx-webclient`, `websocket`. For version `1.25.0` and later, use [`enable_experimental_instrumentations` ([1.25.0])](#config-enable-experimental-instrumentations) to enable experimental instrumentations. +A list of instrumentations which should be disabled. Valid options are `annotations`, `annotations-capture-span`, `annotations-capture-transaction`, `annotations-traced`, `apache-commons-exec`, `apache-httpclient`, `asynchttpclient`, `aws-lambda`, `aws-sdk`, `cassandra`, `concurrent`, `dubbo`, `elasticsearch-restclient`, `exception-handler`, `executor`, `executor-collection`, `experimental`, `finagle-httpclient`, `fork-join`, `grails`, `grpc`, `hibernate-search`, `http-client`, `jakarta-websocket`, `java-ldap`, `javalin`, `javax-websocket`, `jax-rs`, `jax-ws`, `jdbc`, `jdk-httpclient`, `jdk-httpserver`, `jedis`, `jms`, `jsf`, `kafka`, `lettuce`, `log-correlation`, `log-error`, `log-reformatting`, `logging`, `micrometer`, `mongodb`, `mongodb-client`, `okhttp`, `opentelemetry`, `opentelemetry-annotations`, `opentelemetry-metrics`, `opentracing`, `process`, `public-api`, `quartz`, `rabbitmq`, `reactor`, `redis`, `redisson`, `render`, `scala-future`, `scheduled`, `servlet-api`, `servlet-api-async`, `servlet-api-dispatch`, `servlet-input-stream`, `servlet-service-name`, `servlet-version`, `sparkjava`, `spring-ai`, `spring-amqp`, `spring-mvc`, `spring-resttemplate`, `spring-service-name`, `spring-view-render`, `spring-webclient`, `spring-webflux`, `ssl-context`, `struts`, `timer-task`, `urlconnection`, `vertx`, `vertx-web`, `vertx-webclient`, `websocket`. For version `1.25.0` and later, use [`enable_experimental_instrumentations` ([1.25.0])](#config-enable-experimental-instrumentations) to enable experimental instrumentations. ::::{note} Changing this value at runtime can slow down the application temporarily. diff --git a/docs/reference/config-reference-properties-file.md b/docs/reference/config-reference-properties-file.md index 66c462ad83..edf6be384b 100644 --- a/docs/reference/config-reference-properties-file.md +++ b/docs/reference/config-reference-properties-file.md @@ -332,7 +332,7 @@ applies_to: # sanitize_field_names=password,passwd,pwd,secret,*key,*token*,*session*,*credit*,*card*,*auth*,*principal*,set-cookie # A list of instrumentations which should be selectively enabled. -# Valid options are `annotations`, `annotations-capture-span`, `annotations-capture-transaction`, `annotations-traced`, `apache-commons-exec`, `apache-httpclient`, `asynchttpclient`, `aws-lambda`, `aws-sdk`, `cassandra`, `concurrent`, `dubbo`, `elasticsearch-restclient`, `exception-handler`, `executor`, `executor-collection`, `experimental`, `finagle-httpclient`, `fork-join`, `grails`, `grpc`, `hibernate-search`, `http-client`, `jakarta-websocket`, `java-ldap`, `javalin`, `javax-websocket`, `jax-rs`, `jax-ws`, `jdbc`, `jdk-httpclient`, `jdk-httpserver`, `jedis`, `jms`, `jsf`, `kafka`, `lettuce`, `log-correlation`, `log-error`, `log-reformatting`, `logging`, `micrometer`, `mongodb`, `mongodb-client`, `okhttp`, `opentelemetry`, `opentelemetry-annotations`, `opentelemetry-metrics`, `opentracing`, `process`, `public-api`, `quartz`, `rabbitmq`, `reactor`, `redis`, `redisson`, `render`, `scala-future`, `scheduled`, `servlet-api`, `servlet-api-async`, `servlet-api-dispatch`, `servlet-input-stream`, `servlet-service-name`, `servlet-version`, `sparkjava`, `spring-amqp`, `spring-mvc`, `spring-resttemplate`, `spring-service-name`, `spring-view-render`, `spring-webclient`, `spring-webflux`, `ssl-context`, `struts`, `timer-task`, `urlconnection`, `vertx`, `vertx-web`, `vertx-webclient`, `websocket`. +# Valid options are `annotations`, `annotations-capture-span`, `annotations-capture-transaction`, `annotations-traced`, `apache-commons-exec`, `apache-httpclient`, `asynchttpclient`, `aws-lambda`, `aws-sdk`, `cassandra`, `concurrent`, `dubbo`, `elasticsearch-restclient`, `exception-handler`, `executor`, `executor-collection`, `experimental`, `finagle-httpclient`, `fork-join`, `grails`, `grpc`, `hibernate-search`, `http-client`, `jakarta-websocket`, `java-ldap`, `javalin`, `javax-websocket`, `jax-rs`, `jax-ws`, `jdbc`, `jdk-httpclient`, `jdk-httpserver`, `jedis`, `jms`, `jsf`, `kafka`, `lettuce`, `log-correlation`, `log-error`, `log-reformatting`, `logging`, `micrometer`, `mongodb`, `mongodb-client`, `okhttp`, `opentelemetry`, `opentelemetry-annotations`, `opentelemetry-metrics`, `opentracing`, `process`, `public-api`, `quartz`, `rabbitmq`, `reactor`, `redis`, `redisson`, `render`, `scala-future`, `scheduled`, `servlet-api`, `servlet-api-async`, `servlet-api-dispatch`, `servlet-input-stream`, `servlet-service-name`, `servlet-version`, `sparkjava`, `spring-ai`, `spring-amqp`, `spring-mvc`, `spring-resttemplate`, `spring-service-name`, `spring-view-render`, `spring-webclient`, `spring-webflux`, `ssl-context`, `struts`, `timer-task`, `urlconnection`, `vertx`, `vertx-web`, `vertx-webclient`, `websocket`. # When set to non-empty value, only listed instrumentations will be enabled if they are not disabled through <> or <>. # When not set or empty (default), all instrumentations enabled by default will be enabled unless they are disabled through <> or <>. # @@ -345,7 +345,7 @@ applies_to: # enable_instrumentations= # A list of instrumentations which should be disabled. -# Valid options are `annotations`, `annotations-capture-span`, `annotations-capture-transaction`, `annotations-traced`, `apache-commons-exec`, `apache-httpclient`, `asynchttpclient`, `aws-lambda`, `aws-sdk`, `cassandra`, `concurrent`, `dubbo`, `elasticsearch-restclient`, `exception-handler`, `executor`, `executor-collection`, `experimental`, `finagle-httpclient`, `fork-join`, `grails`, `grpc`, `hibernate-search`, `http-client`, `jakarta-websocket`, `java-ldap`, `javalin`, `javax-websocket`, `jax-rs`, `jax-ws`, `jdbc`, `jdk-httpclient`, `jdk-httpserver`, `jedis`, `jms`, `jsf`, `kafka`, `lettuce`, `log-correlation`, `log-error`, `log-reformatting`, `logging`, `micrometer`, `mongodb`, `mongodb-client`, `okhttp`, `opentelemetry`, `opentelemetry-annotations`, `opentelemetry-metrics`, `opentracing`, `process`, `public-api`, `quartz`, `rabbitmq`, `reactor`, `redis`, `redisson`, `render`, `scala-future`, `scheduled`, `servlet-api`, `servlet-api-async`, `servlet-api-dispatch`, `servlet-input-stream`, `servlet-service-name`, `servlet-version`, `sparkjava`, `spring-amqp`, `spring-mvc`, `spring-resttemplate`, `spring-service-name`, `spring-view-render`, `spring-webclient`, `spring-webflux`, `ssl-context`, `struts`, `timer-task`, `urlconnection`, `vertx`, `vertx-web`, `vertx-webclient`, `websocket`. +# Valid options are `annotations`, `annotations-capture-span`, `annotations-capture-transaction`, `annotations-traced`, `apache-commons-exec`, `apache-httpclient`, `asynchttpclient`, `aws-lambda`, `aws-sdk`, `cassandra`, `concurrent`, `dubbo`, `elasticsearch-restclient`, `exception-handler`, `executor`, `executor-collection`, `experimental`, `finagle-httpclient`, `fork-join`, `grails`, `grpc`, `hibernate-search`, `http-client`, `jakarta-websocket`, `java-ldap`, `javalin`, `javax-websocket`, `jax-rs`, `jax-ws`, `jdbc`, `jdk-httpclient`, `jdk-httpserver`, `jedis`, `jms`, `jsf`, `kafka`, `lettuce`, `log-correlation`, `log-error`, `log-reformatting`, `logging`, `micrometer`, `mongodb`, `mongodb-client`, `okhttp`, `opentelemetry`, `opentelemetry-annotations`, `opentelemetry-metrics`, `opentracing`, `process`, `public-api`, `quartz`, `rabbitmq`, `reactor`, `redis`, `redisson`, `render`, `scala-future`, `scheduled`, `servlet-api`, `servlet-api-async`, `servlet-api-dispatch`, `servlet-input-stream`, `servlet-service-name`, `servlet-version`, `sparkjava`, `spring-ai`, `spring-amqp`, `spring-mvc`, `spring-resttemplate`, `spring-service-name`, `spring-view-render`, `spring-webclient`, `spring-webflux`, `ssl-context`, `struts`, `timer-task`, `urlconnection`, `vertx`, `vertx-web`, `vertx-webclient`, `websocket`. # For version `1.25.0` and later, use <> to enable experimental instrumentations. # # NOTE: Changing this value at runtime can slow down the application temporarily. diff --git a/docs/reference/supported-technologies.md b/docs/reference/supported-technologies.md index ce1f62e017..29eed072ae 100644 --- a/docs/reference/supported-technologies.md +++ b/docs/reference/supported-technologies.md @@ -19,6 +19,7 @@ Elastic supports OpenTelemetry, which allows logs, metrics, and trace signal col * [Web Frameworks](#supported-web-frameworks) * [Application Servers/Servlet Containers](#supported-app-servers) * [Data Stores](#supported-databases) +* [Generative AI](#supported-generative-ai) * [Networking frameworks](#supported-networking-frameworks) * [Asynchronous frameworks](#supported-async-frameworks) * [Messaging frameworks](#supported-messaging-frameworks) @@ -128,6 +129,13 @@ The Elastic APM Java agent has generic support for the Servlet API 3+. However, | AWS S3 | ≥ 1.x, ≤ 2.39.x | The agent creates spans for interactions with the AWS S3 service through the AWS Java SDK. | 1.31.0, 2.21+ since 1.44.0 | +## Generative AI [supported-generative-ai] + +| Framework | Supported versions | Description | Since | +|-----------|--------------------|-------------|-------| +| Spring AI | 2.x | Creates spans for synchronous and streaming chat model calls, embedding model calls, vector store operations, and tool callbacks. Prompt, response, document, query, tool argument, and tool result content is not captured. This experimental instrumentation is disabled by default; enable it with [`enable_experimental_instrumentations`](/reference/config-core.md#config-enable-experimental-instrumentations). | 1.58.0 (experimental) | + + ## Networking frameworks [supported-networking-frameworks] Distributed tracing will only work if you are using one of the supported networking frameworks. diff --git a/pom.xml b/pom.xml index 434b341cda..2fbe7f9128 100644 --- a/pom.xml +++ b/pom.xml @@ -361,7 +361,7 @@ central true - apm-agent-common,apm-agent-cached-lookup-key,apm-agent-bootstrap,apm-agent-core,elastic-apm-agent-premain,apm-agent-plugins,apm-httpclient-core,apm-apache-httpclient,apm-apache-httpclient3-plugin,apm-apache-httpclient-common,apm-apache-httpclient4-plugin,apm-apache-httpclient5-plugin,apm-api-plugin,apm-asynchttpclient-plugin,apm-java-concurrent-plugin,apm-cassandra,apm-cassandra-core-plugin,apm-cassandra3-plugin,apm-cassandra4-plugin,apm-okhttp-plugin,apm-dubbo-plugin,apm-es-restclient-plugin,apm-es-restclient-plugin-common,apm-es-restclient-plugin-5_6,apm-es-restclient-plugin-6_4,apm-es-restclient-plugin-7_x,apm-es-restclient-plugin-8_x,apm-grails-plugin,apm-grpc,apm-grpc-plugin,apm-hibernate-search-plugin,apm-hibernate-search-plugin-common,apm-hibernate-search-plugin-5_x,apm-hibernate-search-plugin-6_x,apm-httpserver-core,apm-servlet-plugin,apm-javalin-plugin,apm-jaxrs-plugin,apm-jaxws-plugin,apm-jdbc-plugin,apm-jdk-httpclient-plugin,apm-jdk-httpserver-plugin,apm-redis-plugin,apm-redis-common,apm-jedis-plugin,apm-jedis-4-plugin,apm-jms-plugin,apm-jms-plugin-base,apm-jms-javax,apm-jms-jakarta,apm-jmx-plugin,apm-jsf-plugin,apm-kafka-plugin,apm-kafka-base-plugin,apm-kafka-headers-plugin,apm-kafka-spring-plugin,apm-lettuce-plugin,apm-logging-plugin,apm-logging-plugin-common,apm-log4j1-plugin,apm-log4j2-plugin,apm-opentelemetry,apm-opentelemetry-embedded-metrics-sdk,apm-opentelemetry-metrics-bridge-parent,apm-opentelemetry-metrics-bridge-common,apm-opentelemetry-metrics-bridge-v1_14,apm-opentelemetry-metrics-bridge-latest,apm-opentelemetry-metricsdk-plugin,apm-opentelemetry-plugin,apm-slf4j-plugin,apm-logback-plugin,apm-logback-plugin-impl,apm-jboss-logging-plugin,apm-jul-plugin,apm-tomcat-logging-plugin,apm-micrometer-plugin,apm-mongodb,apm-mongodb-common,apm-mongodb3-plugin,apm-mongodb4-plugin,apm-opentracing-plugin,apm-process-plugin,apm-profiling-plugin,apm-quartz,apm-quartz-common,apm-quartz-plugin-1,apm-quartz-plugin-2,apm-rabbitmq,apm-rabbitmq-plugin,apm-rabbitmq-spring5,apm-reactor-plugin,apm-redisson-plugin,apm-scala-concurrent-plugin,apm-scheduled-annotation-plugin,apm-sparkjava-plugin,apm-urlconnection-plugin,apm-spring-resttemplate,apm-spring-resttemplate-plugin,apm-spring-webflux,apm-spring-webflux-common-spring5,apm-spring-webflux-common,apm-spring-webclient-plugin,apm-spring-webflux-testapp,apm-spring-webflux-testapp-spring7,apm-spring-webflux-spring5,apm-spring-webflux-plugin,apm-spring-webflux-plugin-spring7,apm-spring-webmvc,apm-spring-webmvc-spring5,apm-spring-webmvc-plugin,apm-struts-plugin,apm-vertx,apm-vertx-common,apm-vertx3-plugin,apm-vertx4-plugin,apm-awslambda-plugin,apm-jakarta-websocket-plugin,apm-ecs-logging-plugin,apm-aws-sdk,apm-aws-sdk-common,apm-aws-sdk-1-plugin,apm-aws-sdk-2-plugin,apm-finagle-httpclient-plugin,apm-java-ldap-plugin,apm-agent-builds,apm-agent,apm-agent-lambda-layer,apm-agent-java8,apm-agent-benchmarks,apm-spring-resttemplate-test,apm-spring-restclient-test,apm-logback-plugin-legacy-tests,apm-log4j2-plugin-tests,apm-jms-spring-plugin,apm-jedis-2-tests,apm-jedis-3-tests,apm-jedis-5-tests,apm-lettuce-3-tests,apm-grpc-test-1.6.1,apm-grpc-test-latest,apm-rabbitmq-test-3,apm-rabbitmq-test-4,apm-rabbitmq-test-spring6,apm-okhttp-test,apm-opentelemetry-test,apm-vertx3-test-latest,apm-servlet-jakarta-test,apm-jaxws-plugin-jakartaee-test,apm-jaxrs-plugin-jakartaee-test,apm-scheduled-annotation-plugin-jakartaee-test,integration-tests,simple-webapp,jakartaee-simple-webapp,jsf-app,jsf-app-dependent,jsf-app-standalone,jakartaee-jsf-app,jakartaee-jsf-app-dependent,jakartaee-jsf-app-standalone,cdi-app,cdi-app-dependent,cdi-app-standalone,soap-test,external-plugin-test,plugin-instrumentation-target,external-plugin-app,external-plugin-jakarta-app,external-plugin,application-server-integration-tests,spring-boot-1-5,spring-boot-2,spring-boot-2-base,spring-boot-2-jetty,spring-boot-2-tomcat,spring-boot-2-undertow,cdi-jakartaee-app,cdi-jakartaee-app-dependent,cdi-jakartaee-app-standalone,quarkus,quarkus-jaxrs-base,quarkus-jaxrs-undertow,quarkus-jaxrs-vertx,external-plugin-otel-test,external-plugin-otel-test-app,external-plugin-otel-test-plugin1,external-plugin-otel-test-plugin2,runtime-attach,runtime-attach-app,runtime-attach-test,main-app-test,spring-boot-3,spring-boot-3-jetty,spring-boot-3-tomcat,spring-boot-3-undertow,aws-lambda-test,spring-boot-4,spring-boot-4-base,spring-boot-4-jetty,spring-boot-4-tomcat,apm-spring-webmvc-spring7-test + apm-agent-common,apm-agent-cached-lookup-key,apm-agent-bootstrap,apm-agent-core,elastic-apm-agent-premain,apm-agent-plugins,apm-httpclient-core,apm-apache-httpclient,apm-apache-httpclient3-plugin,apm-apache-httpclient-common,apm-apache-httpclient4-plugin,apm-apache-httpclient5-plugin,apm-api-plugin,apm-asynchttpclient-plugin,apm-java-concurrent-plugin,apm-cassandra,apm-cassandra-core-plugin,apm-cassandra3-plugin,apm-cassandra4-plugin,apm-okhttp-plugin,apm-dubbo-plugin,apm-es-restclient-plugin,apm-es-restclient-plugin-common,apm-es-restclient-plugin-5_6,apm-es-restclient-plugin-6_4,apm-es-restclient-plugin-7_x,apm-es-restclient-plugin-8_x,apm-grails-plugin,apm-grpc,apm-grpc-plugin,apm-hibernate-search-plugin,apm-hibernate-search-plugin-common,apm-hibernate-search-plugin-5_x,apm-hibernate-search-plugin-6_x,apm-httpserver-core,apm-servlet-plugin,apm-javalin-plugin,apm-jaxrs-plugin,apm-jaxws-plugin,apm-jdbc-plugin,apm-jdk-httpclient-plugin,apm-jdk-httpserver-plugin,apm-redis-plugin,apm-redis-common,apm-jedis-plugin,apm-jedis-4-plugin,apm-jms-plugin,apm-jms-plugin-base,apm-jms-javax,apm-jms-jakarta,apm-jmx-plugin,apm-jsf-plugin,apm-kafka-plugin,apm-kafka-base-plugin,apm-kafka-headers-plugin,apm-kafka-spring-plugin,apm-lettuce-plugin,apm-logging-plugin,apm-logging-plugin-common,apm-log4j1-plugin,apm-log4j2-plugin,apm-opentelemetry,apm-opentelemetry-embedded-metrics-sdk,apm-opentelemetry-metrics-bridge-parent,apm-opentelemetry-metrics-bridge-common,apm-opentelemetry-metrics-bridge-v1_14,apm-opentelemetry-metrics-bridge-latest,apm-opentelemetry-metricsdk-plugin,apm-opentelemetry-plugin,apm-slf4j-plugin,apm-logback-plugin,apm-logback-plugin-impl,apm-jboss-logging-plugin,apm-jul-plugin,apm-tomcat-logging-plugin,apm-micrometer-plugin,apm-mongodb,apm-mongodb-common,apm-mongodb3-plugin,apm-mongodb4-plugin,apm-opentracing-plugin,apm-process-plugin,apm-profiling-plugin,apm-quartz,apm-quartz-common,apm-quartz-plugin-1,apm-quartz-plugin-2,apm-rabbitmq,apm-rabbitmq-plugin,apm-rabbitmq-spring5,apm-reactor-plugin,apm-redisson-plugin,apm-scala-concurrent-plugin,apm-scheduled-annotation-plugin,apm-sparkjava-plugin,apm-urlconnection-plugin,apm-spring-ai-plugin,apm-spring-resttemplate,apm-spring-resttemplate-plugin,apm-spring-webflux,apm-spring-webflux-common-spring5,apm-spring-webflux-common,apm-spring-webclient-plugin,apm-spring-webflux-testapp,apm-spring-webflux-testapp-spring7,apm-spring-webflux-spring5,apm-spring-webflux-plugin,apm-spring-webflux-plugin-spring7,apm-spring-webmvc,apm-spring-webmvc-spring5,apm-spring-webmvc-plugin,apm-struts-plugin,apm-vertx,apm-vertx-common,apm-vertx3-plugin,apm-vertx4-plugin,apm-awslambda-plugin,apm-jakarta-websocket-plugin,apm-ecs-logging-plugin,apm-aws-sdk,apm-aws-sdk-common,apm-aws-sdk-1-plugin,apm-aws-sdk-2-plugin,apm-finagle-httpclient-plugin,apm-java-ldap-plugin,apm-agent-builds,apm-agent,apm-agent-lambda-layer,apm-agent-java8,apm-agent-benchmarks,apm-spring-resttemplate-test,apm-spring-restclient-test,apm-logback-plugin-legacy-tests,apm-log4j2-plugin-tests,apm-jms-spring-plugin,apm-jedis-2-tests,apm-jedis-3-tests,apm-jedis-5-tests,apm-lettuce-3-tests,apm-grpc-test-1.6.1,apm-grpc-test-latest,apm-rabbitmq-test-3,apm-rabbitmq-test-4,apm-rabbitmq-test-spring6,apm-okhttp-test,apm-opentelemetry-test,apm-vertx3-test-latest,apm-servlet-jakarta-test,apm-jaxws-plugin-jakartaee-test,apm-jaxrs-plugin-jakartaee-test,apm-scheduled-annotation-plugin-jakartaee-test,integration-tests,simple-webapp,jakartaee-simple-webapp,jsf-app,jsf-app-dependent,jsf-app-standalone,jakartaee-jsf-app,jakartaee-jsf-app-dependent,jakartaee-jsf-app-standalone,cdi-app,cdi-app-dependent,cdi-app-standalone,soap-test,external-plugin-test,plugin-instrumentation-target,external-plugin-app,external-plugin-jakarta-app,external-plugin,application-server-integration-tests,spring-boot-1-5,spring-boot-2,spring-boot-2-base,spring-boot-2-jetty,spring-boot-2-tomcat,spring-boot-2-undertow,cdi-jakartaee-app,cdi-jakartaee-app-dependent,cdi-jakartaee-app-standalone,quarkus,quarkus-jaxrs-base,quarkus-jaxrs-undertow,external-plugin-otel-test,external-plugin-otel-test-app,external-plugin-otel-test-plugin1,external-plugin-otel-test-plugin2,runtime-attach,runtime-attach-app,runtime-attach-test,main-app-test,spring-boot-3,spring-boot-3-jetty,spring-boot-3-tomcat,spring-boot-3-undertow,aws-lambda-test,spring-boot-4,spring-boot-4-base,spring-boot-4-jetty,spring-boot-4-tomcat,apm-spring-webmvc-spring7-test From 7dbee416438f148811467d00d6251786389fcb06 Mon Sep 17 00:00:00 2001 From: hegerchr Date: Fri, 30 Oct 2026 12:07:48 +0100 Subject: [PATCH 02/15] Add configurable Spring AI content capture --- CHANGELOG.next-release.md | 2 +- .../configuration/CoreConfigurationImpl.java | 1 + .../springai/ChatModelInstrumentation.java | 2 +- .../agent/springai/SpringAiConfiguration.java | 39 +++++ .../agent/springai/SpringAiSpanHelper.java | 155 +++++++++++++++++- .../springai/ToolCallbackInstrumentation.java | 8 +- .../springai/VectorStoreInstrumentation.java | 7 +- .../VectorStoreQueryInstrumentation.java | 86 ++++++++++ ...ic.apm.agent.sdk.ElasticApmInstrumentation | 1 + ....configuration.ConfigurationOptionProvider | 1 + .../springai/SpringAiInstrumentationTest.java | 109 +++++++++++- .../configuration/CoreConfiguration.java | 2 + docs/reference/config-ai.md | 31 ++++ docs/reference/config-core.md | 1 + .../config-reference-properties-file.md | 18 ++ docs/reference/configuration.md | 4 + docs/reference/supported-technologies.md | 2 +- docs/reference/toc.yml | 3 +- 18 files changed, 451 insertions(+), 21 deletions(-) create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiConfiguration.java create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/resources/META-INF/services/org.stagemonitor.configuration.ConfigurationOptionProvider create mode 100644 docs/reference/config-ai.md diff --git a/CHANGELOG.next-release.md b/CHANGELOG.next-release.md index 14d9e9ee98..662320a0d9 100644 --- a/CHANGELOG.next-release.md +++ b/CHANGELOG.next-release.md @@ -13,7 +13,7 @@ This file contains all changes which are not released yet. # Features and enhancements -- Add experimental auto-instrumentation for Spring AI 2.x chat models, embedding models, vector stores, and tool callbacks +- Add experimental auto-instrumentation for Spring AI 2.x chat models, embedding models, vector stores, and tool callbacks, with opt-in AI content capture # Deprecations diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfigurationImpl.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfigurationImpl.java index d73e4185dd..00e07a73d1 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfigurationImpl.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfigurationImpl.java @@ -261,6 +261,7 @@ public class CoreConfigurationImpl extends ConfigurationOptionProvider implement "- `transaction.context.request.body`, `error.context.request.body`\n" + "- `transaction.context.message.body`, `error.context.message.body`\n" + "- `span.context.db.statement`\n" + + "- captured AI content values\n" + "\nNote that tracing data is limited at the upstream APM server to \n" + "{apm-guide-ref}/configuration-process.html#max_event_size[`max_event_size`], \n" + "which defaults to 300kB. If you configure `long_field_max_length` too large, it \n" + diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java index 1359ecda7b..ac5abc4a7c 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java @@ -63,7 +63,7 @@ public String getAdviceClassName() { public static class ChatModelAdvice { @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static Object onEnter(@Advice.This Object model, @Advice.Argument(0) Prompt prompt) { - Span span = SpringAiSpanHelper.startModelSpan(model, "chat", prompt.getOptions(), false); + Span span = SpringAiSpanHelper.startModelSpan(model, "chat", prompt, false); return span == null ? null : span.activate(); } diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiConfiguration.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiConfiguration.java new file mode 100644 index 0000000000..cf0d522d44 --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiConfiguration.java @@ -0,0 +1,39 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import org.stagemonitor.configuration.ConfigurationOption; +import org.stagemonitor.configuration.ConfigurationOptionProvider; + +public class SpringAiConfiguration extends ConfigurationOptionProvider { + + private final ConfigurationOption captureAiContent = ConfigurationOption.booleanOption() + .key("capture_ai_content") + .configurationCategory("AI") + .tags("security", "performance", "experimental", "added[1.58.0]") + .description("Enables capture of Spring AI prompt and response messages, embedding inputs, vector store documents and queries, and tool arguments and results.\n\n" + + "Captured content can contain sensitive information and can significantly increase storage usage. Content capture is disabled by default. " + + "Individual captured content values are truncated according to `long_field_max_length`.") + .dynamic(true) + .buildWithDefault(false); + + public boolean isCaptureAiContent() { + return captureAiContent.get(); + } +} diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java index 0ecb3c8a47..69cecce7c0 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java @@ -23,6 +23,8 @@ import co.elastic.apm.agent.tracer.Span; import co.elastic.apm.agent.tracer.TraceState; import co.elastic.apm.agent.tracer.Tracer; +import co.elastic.apm.agent.tracer.configuration.CoreConfiguration; +import org.springframework.ai.chat.messages.Message; import org.springframework.ai.chat.metadata.ChatGenerationMetadata; import org.springframework.ai.chat.metadata.ChatResponseMetadata; import org.springframework.ai.chat.metadata.Usage; @@ -30,6 +32,7 @@ import org.springframework.ai.chat.model.Generation; import org.springframework.ai.chat.prompt.ChatOptions; import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.document.Document; import org.springframework.ai.embedding.EmbeddingOptions; import org.springframework.ai.embedding.EmbeddingRequest; import org.springframework.ai.embedding.EmbeddingResponse; @@ -51,17 +54,20 @@ final class SpringAiSpanHelper { private static final Tracer tracer = GlobalTracer.get(); + private static final SpringAiConfiguration config = tracer.getConfig(SpringAiConfiguration.class); + private static final CoreConfiguration coreConfig = tracer.getConfig(CoreConfiguration.class); private SpringAiSpanHelper() { } @Nullable - static Span startModelSpan(Object model, String operation, @Nullable ChatOptions options, boolean streaming) { + static Span startModelSpan(Object model, String operation, Prompt prompt, boolean streaming) { Span span = tracer.currentContext().createSpan(); if (span == null) { return null; } + ChatOptions options = prompt.getOptions(); String requestModel = options == null ? null : options.getModel(); String provider = providerFor(model); span.withName(spanName(operation, requestModel)) @@ -82,6 +88,9 @@ static Span startModelSpan(Object model, String operation, @Nullable ChatOpti .withOtelAttribute("gen_ai.request.top_k", options.getTopK()) .withOtelAttribute("gen_ai.request.top_p", options.getTopP()); } + if (config.isCaptureAiContent()) { + captureStructuredContent(span, "gen_ai.input.messages", serializeMessages(prompt.getInstructions())); + } return span; } @@ -102,17 +111,21 @@ static Span startEmbeddingSpan(Object model, EmbeddingRequest request) { .withOtelAttribute("gen_ai.system", provider) .withOtelAttribute("gen_ai.request.model", requestModel) .withOtelAttribute("gen_ai.request.embedding.dimensions", options == null ? null : options.getDimensions()); + if (config.isCaptureAiContent()) { + captureStructuredContent(span, "spring.ai.embedding.request.inputs", serializeStrings(request.getInstructions())); + } return span; } @Nullable static Span startVectorStoreSpan(AbstractObservationVectorStore vectorStore, String operation, - @Nullable SearchRequest searchRequest) { + @Nullable Object argument) { Span span = tracer.currentContext().createExitSpan(); if (span == null) { return null; } VectorStoreObservationContext.Builder builder = vectorStore.createObservationContextBuilder(operation); + SearchRequest searchRequest = argument instanceof SearchRequest ? (SearchRequest) argument : null; if (searchRequest != null) { builder.queryRequest(searchRequest); } @@ -134,20 +147,28 @@ static Span startVectorStoreSpan(AbstractObservationVectorStore vectorStore, if (searchRequest != null) { span.withOtelAttribute("db.vector.query.top_k", searchRequest.getTopK()) .withOtelAttribute("db.vector.query.similarity_threshold", searchRequest.getSimilarityThreshold()); + captureContent(span, "db.vector.query.content", searchRequest.getQuery()); + if (searchRequest.getFilterExpression() != null) { + captureContent(span, "db.vector.query.filter", searchRequest.getFilterExpression().toString()); + } + } else if ("add".equals(operation) && argument instanceof List) { + if (config.isCaptureAiContent()) { + captureStructuredContent(span, "db.vector.documents", serializeDocuments((List) argument)); + } } span.getContext().getServiceTarget().withType(dbSystem).withName(context.getCollectionName()); return span; } @Nullable - static Span startToolSpan(ToolCallback callback) { + static Span startToolSpan(ToolCallback callback, String toolInput) { Span span = tracer.currentContext().createSpan(); if (span == null) { return null; } ToolDefinition definition = callback.getToolDefinition(); String name = definition.name(); - return span.withName("execute_tool " + name) + span.withName("execute_tool " + name) .withType("app") .withSubtype("internal") .withAction("execute_tool") @@ -158,13 +179,15 @@ static Span startToolSpan(ToolCallback callback) { .withOtelAttribute("spring.ai.tool.definition.name", name) .withOtelAttribute("spring.ai.tool.definition.description", definition.description()) .withOtelAttribute("spring.ai.tool.definition.schema", definition.inputSchema()); + captureContent(span, "spring.ai.tool.call.arguments", toolInput); + return span; } @SuppressWarnings("unchecked") static Flux wrapChatFlux(final Object model, final Prompt prompt, final Flux source) { final Flux typedSource = (Flux) source; return Flux.defer(() -> { - final Span span = startModelSpan(model, "chat", prompt.getOptions(), true); + final Span span = startModelSpan(model, "chat", prompt, true); if (span == null) { return typedSource; } @@ -213,7 +236,26 @@ private static void addChatResponseAttributes(Span span, @Nullable ChatRespon } } span.withOtelAttribute("gen_ai.response.finish_reasons", stringify(finishReasons)); + if (config.isCaptureAiContent()) { + captureStructuredContent(span, "gen_ai.output.messages", serializeGenerations(response.getResults())); + } + } + } + + static void endVectorStoreSpan(@Nullable Span span, @Nullable Object response, @Nullable Throwable thrown) { + if (span != null && response instanceof List) { + if (config.isCaptureAiContent()) { + captureStructuredContent(span, "db.vector.query.response.documents", serializeDocuments((List) response)); + } + } + endSpan(span, thrown); + } + + static void endToolSpan(@Nullable Span span, @Nullable String result, @Nullable Throwable thrown) { + if (span != null) { + captureContent(span, "spring.ai.tool.call.result", result); } + endSpan(span, thrown); } static void endEmbeddingSpan(@Nullable Span span, @Nullable EmbeddingResponse response, @@ -277,6 +319,109 @@ private static String providerFor(Object model) { return "spring_ai"; } + private static void captureContent(Span span, String key, @Nullable String content) { + if (config.isCaptureAiContent() && content != null) { + span.withOtelAttribute(key, truncate(content, coreConfig.getLongFieldMaxLength())); + } + } + + private static void captureStructuredContent(Span span, String key, String content) { + span.withOtelAttribute(key, content); + } + + private static String truncate(String value, int maxCodePoints) { + if (maxCodePoints < 0 || value.codePointCount(0, value.length()) <= maxCodePoints) { + return value; + } + return value.substring(0, value.offsetByCodePoints(0, maxCodePoints)); + } + + private static String serializeMessages(List messages) { + StringBuilder result = new StringBuilder("["); + for (int i = 0; i < messages.size(); i++) { + if (i > 0) { + result.append(','); + } + Message message = messages.get(i); + result.append("{\"role\":"); + appendJsonString(result, message.getMessageType().getValue()); + result.append(",\"parts\":[{\"type\":\"text\",\"content\":"); + appendJsonString(result, message.getText()); + result.append("}]}"); + } + return result.append(']').toString(); + } + + private static String serializeGenerations(List generations) { + List messages = new ArrayList<>(); + for (Generation generation : generations) { + if (generation.getOutput() != null) { + messages.add(generation.getOutput()); + } + } + return serializeMessages(messages); + } + + private static String serializeStrings(List values) { + StringBuilder result = new StringBuilder("["); + for (int i = 0; i < values.size(); i++) { + if (i > 0) { + result.append(','); + } + appendJsonString(result, values.get(i)); + } + return result.append(']').toString(); + } + + private static String serializeDocuments(List values) { + StringBuilder result = new StringBuilder("["); + boolean first = true; + for (Object value : values) { + if (!(value instanceof Document)) { + continue; + } + if (!first) { + result.append(','); + } + first = false; + Document document = (Document) value; + result.append("{\"id\":"); + appendJsonString(result, document.getId()); + result.append(",\"content\":"); + appendJsonString(result, document.getText()); + result.append('}'); + } + return result.append(']').toString(); + } + + private static void appendJsonString(StringBuilder target, @Nullable String value) { + if (value == null) { + target.append("null"); + return; + } + value = truncate(value, coreConfig.getLongFieldMaxLength()); + target.append('"'); + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + switch (c) { + case '"': target.append("\\\""); break; + case '\\': target.append("\\\\"); break; + case '\b': target.append("\\b"); break; + case '\f': target.append("\\f"); break; + case '\n': target.append("\\n"); break; + case '\r': target.append("\\r"); break; + case '\t': target.append("\\t"); break; + default: + if (c < 0x20) { + target.append(String.format(Locale.ROOT, "\\u%04x", (int) c)); + } else { + target.append(c); + } + } + } + target.append('"'); + } + private static String dbSubtype(@Nullable String databaseSystem) { if (databaseSystem == null) { return "unknown"; diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java index bb1403677a..2e4db12671 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java @@ -54,15 +54,17 @@ public String getAdviceClassName() { public static class ToolCallbackAdvice { @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) - public static Object onEnter(@Advice.This ToolCallback callback) { - Span span = SpringAiSpanHelper.startToolSpan(callback); + public static Object onEnter(@Advice.This ToolCallback callback, + @Advice.Argument(0) String toolInput) { + Span span = SpringAiSpanHelper.startToolSpan(callback, toolInput); return span == null ? null : span.activate(); } @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false) public static void onExit(@Advice.Enter @Nullable Object spanObject, + @Advice.Return @Nullable String result, @Advice.Thrown @Nullable Throwable thrown) { - SpringAiSpanHelper.endSpan((Span) spanObject, thrown); + SpringAiSpanHelper.endToolSpan((Span) spanObject, result, thrown); } } } diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java index 3a6f779768..3d65880215 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java @@ -24,7 +24,6 @@ import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; -import org.springframework.ai.vectorstore.SearchRequest; import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore; import javax.annotation.Nullable; @@ -53,7 +52,7 @@ public ElementMatcher getTypeMatcher() { @Override public ElementMatcher getMethodMatcher() { - return namedOneOf("add", "delete", "similaritySearch").and(takesArguments(1)); + return namedOneOf("add", "delete").and(takesArguments(1)); } @Override @@ -66,9 +65,7 @@ public static class VectorStoreAdvice { public static Object onEnter(@Advice.This AbstractObservationVectorStore vectorStore, @Advice.Origin("#m") String method, @Advice.Argument(0) @Nullable Object argument) { - String operation = "similaritySearch".equals(method) ? "query" : method; - SearchRequest searchRequest = argument instanceof SearchRequest ? (SearchRequest) argument : null; - Span span = SpringAiSpanHelper.startVectorStoreSpan(vectorStore, operation, searchRequest); + Span span = SpringAiSpanHelper.startVectorStoreSpan(vectorStore, method, argument); return span == null ? null : span.activate(); } diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java new file mode 100644 index 0000000000..60750f8020 --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java @@ -0,0 +1,86 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.tracer.Span; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.NamedElement; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.springframework.ai.vectorstore.SearchRequest; +import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore; + +import javax.annotation.Nullable; + +import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; +import static net.bytebuddy.matcher.ElementMatchers.isInterface; +import static net.bytebuddy.matcher.ElementMatchers.nameContains; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.not; +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class VectorStoreQueryInstrumentation extends AbstractSpringAiInstrumentation { + + @Override + public ElementMatcher.Junction getClassLoaderMatcher() { + return super.getClassLoaderMatcher() + .and(co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass( + "org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore")); + } + + @Override + public ElementMatcher getTypeMatcherPreFilter() { + return nameContains("VectorStore"); + } + + @Override + public ElementMatcher getTypeMatcher() { + return not(isInterface()).and(hasSuperType(named( + "org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore"))); + } + + @Override + public ElementMatcher getMethodMatcher() { + return named("similaritySearch").and(takesArguments(1)) + .and(takesArgument(0, named("org.springframework.ai.vectorstore.SearchRequest"))); + } + + @Override + public String getAdviceClassName() { + return getClass().getName() + "$VectorStoreQueryAdvice"; + } + + public static class VectorStoreQueryAdvice { + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + public static Object onEnter(@Advice.This AbstractObservationVectorStore vectorStore, + @Advice.Argument(0) SearchRequest request) { + Span span = SpringAiSpanHelper.startVectorStoreSpan(vectorStore, "query", request); + return span == null ? null : span.activate(); + } + + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false) + public static void onExit(@Advice.Enter @Nullable Object spanObject, + @Advice.Return @Nullable Object response, + @Advice.Thrown @Nullable Throwable thrown) { + SpringAiSpanHelper.endVectorStoreSpan((Span) spanObject, response, thrown); + } + } +} diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation b/apm-agent-plugins/apm-spring-ai-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation index 578358975c..e765d63a9d 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation @@ -2,4 +2,5 @@ co.elastic.apm.agent.springai.ChatModelInstrumentation co.elastic.apm.agent.springai.StreamingChatModelInstrumentation co.elastic.apm.agent.springai.EmbeddingModelInstrumentation co.elastic.apm.agent.springai.VectorStoreInstrumentation +co.elastic.apm.agent.springai.VectorStoreQueryInstrumentation co.elastic.apm.agent.springai.ToolCallbackInstrumentation diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/resources/META-INF/services/org.stagemonitor.configuration.ConfigurationOptionProvider b/apm-agent-plugins/apm-spring-ai-plugin/src/main/resources/META-INF/services/org.stagemonitor.configuration.ConfigurationOptionProvider new file mode 100644 index 0000000000..877ea3fe66 --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/resources/META-INF/services/org.stagemonitor.configuration.ConfigurationOptionProvider @@ -0,0 +1 @@ +co.elastic.apm.agent.springai.SpringAiConfiguration diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java index 700cdb4585..1fae5aa01b 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java @@ -21,6 +21,7 @@ import co.elastic.apm.agent.AbstractInstrumentationTest; import co.elastic.apm.agent.impl.transaction.SpanImpl; import co.elastic.apm.agent.impl.transaction.TransactionImpl; +import co.elastic.apm.agent.tracer.configuration.CoreConfiguration; import org.junit.jupiter.api.Test; import org.springframework.ai.chat.messages.AssistantMessage; import org.springframework.ai.chat.metadata.ChatGenerationMetadata; @@ -50,6 +51,7 @@ import java.util.List; import static co.elastic.apm.agent.testutils.assertions.Assertions.assertThat; +import static org.mockito.Mockito.doReturn; class SpringAiInstrumentationTest extends AbstractInstrumentationTest { @@ -77,7 +79,8 @@ void capturesChatCallAndSemanticAttributes() { .hasOtelAttribute("gen_ai.usage.input_tokens", 10) .hasOtelAttribute("gen_ai.usage.output_tokens", 5) .hasOtelAttribute("gen_ai.usage.total_tokens", 15); - org.assertj.core.api.Assertions.assertThat(span.getOtelAttributes()).doesNotContainKey("gen_ai.input.messages"); + org.assertj.core.api.Assertions.assertThat(span.getOtelAttributes()) + .doesNotContainKeys("gen_ai.input.messages", "gen_ai.output.messages"); } @Test @@ -106,11 +109,14 @@ void capturesEmbeddingCall() { transaction.deactivate().end(); } - assertThat(reporter.getFirstSpan()) + SpanImpl span = reporter.getFirstSpan(); + assertThat(span) .hasName("embedding embedding-test") .hasAction("embedding") .hasOtelAttribute("gen_ai.request.embedding.dimensions", 3) .hasOtelAttribute("gen_ai.usage.input_tokens", 4); + org.assertj.core.api.Assertions.assertThat(span.getOtelAttributes()) + .doesNotContainKey("spring.ai.embedding.request.inputs"); } @Test @@ -159,7 +165,102 @@ void capturesVectorStoreQueryWithoutQueryText() { .hasOtelAttribute("db.vector.query.top_k", 3) .hasOtelAttribute("db.vector.query.similarity_threshold", 0.5); org.assertj.core.api.Assertions.assertThat(span.getOtelAttributes()) - .doesNotContainKeys("db.query.text", "db.vector.query.content", "db.vector.query.filter"); + .doesNotContainKeys("db.query.text", "db.vector.query.content", "db.vector.query.filter", + "db.vector.query.response.documents"); + } + + @Test + void capturesChatContentWhenEnabled() { + enableContentCapture(); + TransactionImpl transaction = startTestRootTransaction(); + try { + new OpenAiTestChatModel().call(new Prompt("secret prompt")); + } finally { + transaction.deactivate().end(); + } + + org.assertj.core.api.Assertions.assertThat(reporter.getFirstSpan().getOtelAttributes()) + .extractingByKeys("gen_ai.input.messages", "gen_ai.output.messages") + .allSatisfy(value -> org.assertj.core.api.Assertions.assertThat(value.toString()).contains("secret")); + } + + @Test + void capturesEmbeddingAndToolContentWhenEnabled() { + enableContentCapture(); + TransactionImpl transaction = startTestRootTransaction(); + try { + new OpenAiTestEmbeddingModel().call(new EmbeddingRequest( + Collections.singletonList("secret embedding document"), EmbeddingOptions.builder().build())); + } finally { + transaction.deactivate().end(); + } + assertThat(reporter.getFirstSpan()) + .hasOtelAttribute("spring.ai.embedding.request.inputs", "[\"secret embedding document\"]"); + + reporter.reset(); + transaction = startTestRootTransaction(); + try { + new TestToolCallback().call("{\"password\":\"secret\"}"); + } finally { + transaction.deactivate().end(); + } + assertThat(reporter.getFirstSpan()) + .hasOtelAttribute("spring.ai.tool.call.arguments", "{\"password\":\"secret\"}") + .hasOtelAttribute("spring.ai.tool.call.result", "secret result"); + } + + @Test + void capturesVectorDocumentsAndQueryContentWhenEnabled() { + enableContentCapture(); + TestVectorStore vectorStore = new TestVectorStore(new OpenAiTestEmbeddingModel()); + TransactionImpl transaction = startTestRootTransaction(); + try { + vectorStore.add(Collections.singletonList(new Document("secret stored document"))); + } finally { + transaction.deactivate().end(); + } + org.assertj.core.api.Assertions.assertThat( + reporter.getFirstSpan().getOtelAttributes().get("db.vector.documents").toString()) + .contains("secret stored document"); + + reporter.reset(); + transaction = startTestRootTransaction(); + try { + vectorStore.similaritySearch(SearchRequest.builder() + .query("secret search query") + .filterExpression("category == 'secret'") + .build()); + } finally { + transaction.deactivate().end(); + } + assertThat(reporter.getFirstSpan()) + .hasOtelAttribute("db.vector.query.content", "secret search query"); + org.assertj.core.api.Assertions.assertThat( + reporter.getFirstSpan().getOtelAttributes()) + .hasEntrySatisfying("db.vector.query.filter", + value -> org.assertj.core.api.Assertions.assertThat(value.toString()).contains("secret")) + .hasEntrySatisfying("db.vector.query.response.documents", + value -> org.assertj.core.api.Assertions.assertThat(value.toString()).contains("secret result document")); + } + + @Test + void truncatesCapturedContentAtLongFieldLimit() { + enableContentCapture(); + doReturn(6).when(tracer.getConfig(CoreConfiguration.class)).getLongFieldMaxLength(); + TransactionImpl transaction = startTestRootTransaction(); + try { + new TestToolCallback().call("123456789"); + } finally { + transaction.deactivate().end(); + } + + assertThat(reporter.getFirstSpan()) + .hasOtelAttribute("spring.ai.tool.call.arguments", "123456") + .hasOtelAttribute("spring.ai.tool.call.result", "secret"); + } + + private static void enableContentCapture() { + doReturn(true).when(config.getConfig(SpringAiConfiguration.class)).isCaptureAiContent(); } static class OpenAiTestChatModel implements ChatModel { @@ -215,7 +316,7 @@ public void doDelete(List ids) { @Override public List doSimilaritySearch(SearchRequest request) { - return Collections.emptyList(); + return Collections.singletonList(new Document("secret result document")); } @Override diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/configuration/CoreConfiguration.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/configuration/CoreConfiguration.java index 50cf86f6cc..8807257077 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/configuration/CoreConfiguration.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/configuration/CoreConfiguration.java @@ -34,6 +34,8 @@ public interface CoreConfiguration { EventType getCaptureBody(); + int getLongFieldMaxLength(); + boolean isEnablePublicApiAnnotationInheritance(); List getSanitizeFieldNames(); diff --git a/docs/reference/config-ai.md b/docs/reference/config-ai.md new file mode 100644 index 0000000000..cf1915d31a --- /dev/null +++ b/docs/reference/config-ai.md @@ -0,0 +1,31 @@ +--- +navigation_title: "AI" +applies_to: + stack: + serverless: + observability: + product: + apm_agent_java: ga +--- + +# AI configuration options [config-ai] + +## `capture_ai_content` (experimental, security, performance) [config-capture-ai-content] + +```{applies_to} +apm_agent_java: ga 1.58.0 +``` + +Enables capture of Spring AI prompt and response messages, embedding inputs, vector store documents and queries, and tool arguments and results. + +Captured content can contain sensitive information and can significantly increase heap usage, network traffic, and storage requirements. Review your data handling requirements before enabling this option. Individual captured content values are truncated according to [`long_field_max_length`](/reference/config-core.md#config-long-field-max-length). + +[![dynamic config](images/dynamic-config.svg "") ](/reference/configuration.md#configuration-dynamic) + +| Default | Type | Dynamic | +| --- | --- | --- | +| `false` | Boolean | true | + +| Java System Properties | Property file | Environment | +| --- | --- | --- | +| `elastic.apm.capture_ai_content` | `capture_ai_content` | `ELASTIC_APM_CAPTURE_AI_CONTENT` | diff --git a/docs/reference/config-core.md b/docs/reference/config-core.md index 423d5f02f8..5bae1f35df 100644 --- a/docs/reference/config-core.md +++ b/docs/reference/config-core.md @@ -281,6 +281,7 @@ The following transaction, span, and error fields will be truncated at this numb * `transaction.context.request.body`, `error.context.request.body` * `transaction.context.message.body`, `error.context.message.body` * `span.context.db.statement` +* Captured AI content values Note that tracing data is limited at the upstream APM server to [`max_event_size`](docs-content://solutions/observability/apm/general-configuration-options.md#apm-max_event_size), which defaults to 300kB. If you configure `long_field_max_length` too large, it could result in transactions, spans, or errors that are rejected by APM server. diff --git a/docs/reference/config-reference-properties-file.md b/docs/reference/config-reference-properties-file.md index edf6be384b..5b7d04a442 100644 --- a/docs/reference/config-reference-properties-file.md +++ b/docs/reference/config-reference-properties-file.md @@ -12,6 +12,23 @@ applies_to: # Property file reference [config-reference-properties-file] ```properties +############################################ +# AI # +############################################ + +# Enables capture of Spring AI prompt and response messages, embedding inputs, vector store documents and queries, +# and tool arguments and results. +# +# Captured content can contain sensitive information and can significantly increase storage usage. +# Content capture is disabled by default. Individual captured content values are truncated according to +# `long_field_max_length`. +# +# This setting can be changed at runtime +# Type: Boolean +# Default value: false +# +# capture_ai_content=false + ############################################ # Circuit-Breaker # ############################################ @@ -294,6 +311,7 @@ applies_to: # - `transaction.context.request.body`, `error.context.request.body` # - `transaction.context.message.body`, `error.context.message.body` # - `span.context.db.statement` +# - captured AI content values # # Note that tracing data is limited at the upstream APM server to # {apm-guide-ref}/configuration-process.html#max_event_size[`max_event_size`], diff --git a/docs/reference/configuration.md b/docs/reference/configuration.md index 34a7283f2e..fe5ca72a0f 100644 --- a/docs/reference/configuration.md +++ b/docs/reference/configuration.md @@ -78,6 +78,10 @@ ELASTIC_APM_SERVER_URL=http://127.0.0.1:8200 This is a list of all configuration options grouped by their category. Click on a key to get more information. +* [AI](/reference/config-ai.md) + + * [`capture_ai_content` (experimental, security, performance [1.58.0])](/reference/config-ai.md#config-capture-ai-content) + * [Circuit-Breaker](/reference/config-circuit-breaker.md) * [`circuit_breaker_enabled` ([1.14.0] performance)](/reference/config-circuit-breaker.md#config-circuit-breaker-enabled) diff --git a/docs/reference/supported-technologies.md b/docs/reference/supported-technologies.md index 29eed072ae..0a2e4b12d8 100644 --- a/docs/reference/supported-technologies.md +++ b/docs/reference/supported-technologies.md @@ -133,7 +133,7 @@ The Elastic APM Java agent has generic support for the Servlet API 3+. However, | Framework | Supported versions | Description | Since | |-----------|--------------------|-------------|-------| -| Spring AI | 2.x | Creates spans for synchronous and streaming chat model calls, embedding model calls, vector store operations, and tool callbacks. Prompt, response, document, query, tool argument, and tool result content is not captured. This experimental instrumentation is disabled by default; enable it with [`enable_experimental_instrumentations`](/reference/config-core.md#config-enable-experimental-instrumentations). | 1.58.0 (experimental) | +| Spring AI | 2.x | Creates spans for synchronous and streaming chat model calls, embedding model calls, vector store operations, and tool callbacks. Prompt, response, document, query, tool argument, and tool result content is not captured by default; use [`capture_ai_content`](/reference/config-ai.md#config-capture-ai-content) to opt in. This experimental instrumentation is disabled by default; enable it with [`enable_experimental_instrumentations`](/reference/config-core.md#config-enable-experimental-instrumentations). | 1.58.0 (experimental) | ## Networking frameworks [supported-networking-frameworks] diff --git a/docs/reference/toc.yml b/docs/reference/toc.yml index 2f5cbc5e78..b75ea0e59b 100644 --- a/docs/reference/toc.yml +++ b/docs/reference/toc.yml @@ -11,6 +11,7 @@ toc: - file: supported-technologies.md - file: configuration.md children: + - file: config-ai.md - file: config-circuit-breaker.md - file: config-core.md - file: config-datastore.md @@ -45,4 +46,4 @@ toc: - file: community-plugins.md - file: upgrading.md - title: Troubleshooting - crosslink: docs-content://troubleshoot/observability/apm-agent-java/apm-java-agent.md \ No newline at end of file + crosslink: docs-content://troubleshoot/observability/apm-agent-java/apm-java-agent.md From 63b80216532dac47facdf5b573a76e920d9b085b Mon Sep 17 00:00:00 2001 From: hegerchr Date: Tue, 10 Nov 2026 17:33:56 +0100 Subject: [PATCH 03/15] Add Spring AI streaming time-to-first-chunk metric --- .../apm-spring-ai-plugin/pom.xml | 16 ++++ .../apm/agent/springai/SpringAiMetrics.java | 80 +++++++++++++++++++ .../agent/springai/SpringAiSpanHelper.java | 17 +++- .../springai/SpringAiInstrumentationTest.java | 73 ++++++++++++++++- 4 files changed, 180 insertions(+), 6 deletions(-) create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiMetrics.java diff --git a/apm-agent-plugins/apm-spring-ai-plugin/pom.xml b/apm-agent-plugins/apm-spring-ai-plugin/pom.xml index 64152ca9fa..9288c49dbb 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/pom.xml +++ b/apm-agent-plugins/apm-spring-ai-plugin/pom.xml @@ -22,6 +22,16 @@ + + ${project.groupId} + apm-agent-core + ${project.version} + + + ${project.groupId} + apm-opentelemetry-embedded-metrics-sdk + ${project.version} + org.springframework.ai spring-ai-model @@ -40,6 +50,12 @@ ${version.reactor} provided + + ${project.groupId} + apm-opentelemetry-metricsdk-plugin + ${project.version} + test + diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiMetrics.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiMetrics.java new file mode 100644 index 0000000000..0b0d86e025 --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiMetrics.java @@ -0,0 +1,80 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.embeddedotel.EmbeddedSdkManager; +import co.elastic.apm.agent.embeddedotel.proxy.ProxyAttributeKey; +import co.elastic.apm.agent.embeddedotel.proxy.ProxyAttributes; +import co.elastic.apm.agent.embeddedotel.proxy.ProxyAttributesBuilder; +import co.elastic.apm.agent.embeddedotel.proxy.ProxyDoubleHistogram; +import co.elastic.apm.agent.impl.ElasticApmTracer; +import co.elastic.apm.agent.tracer.GlobalTracer; +import co.elastic.apm.agent.tracer.Tracer; + +import javax.annotation.Nullable; + +final class SpringAiMetrics { + + static final String TIME_TO_FIRST_CHUNK = "gen_ai.client.inference.time_to_first_chunk"; + + private static final Tracer tracer = GlobalTracer.get(); + private static final ProxyAttributeKey OPERATION_NAME = + ProxyAttributeKey.stringKey("gen_ai.operation.name"); + private static final ProxyAttributeKey PROVIDER_NAME = + ProxyAttributeKey.stringKey("gen_ai.provider.name"); + private static final ProxyAttributeKey REQUEST_MODEL = + ProxyAttributeKey.stringKey("gen_ai.request.model"); + @Nullable + private static final ProxyDoubleHistogram timeToFirstChunk = createTimeToFirstChunkHistogram(); + + private SpringAiMetrics() { + } + + static void recordTimeToFirstChunk(String provider, @Nullable String requestModel, double seconds) { + if (timeToFirstChunk == null) { + return; + } + timeToFirstChunk.record(seconds, createAttributes(provider, requestModel)); + } + + private static ProxyAttributes createAttributes(String provider, @Nullable String requestModel) { + ProxyAttributesBuilder builder = ProxyAttributes.builder(); + builder.put(OPERATION_NAME, "chat"); + builder.put(PROVIDER_NAME, provider); + if (requestModel != null) { + builder.put(REQUEST_MODEL, requestModel); + } + return builder.build(); + } + + @Nullable + private static ProxyDoubleHistogram createTimeToFirstChunkHistogram() { + EmbeddedSdkManager sdkManager = tracer.require(ElasticApmTracer.class) + .getLifecycleListener(EmbeddedSdkManager.class); + if (sdkManager == null) { + return null; + } + return sdkManager.getMeterProvider() + .get("co.elastic.apm.spring-ai") + .histogramBuilder(TIME_TO_FIRST_CHUNK) + .setUnit("s") + .setDescription("Time to receive the first chunk of a streaming generative AI response") + .build(); + } +} diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java index 69cecce7c0..cfa6cf4d06 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java @@ -187,15 +187,28 @@ static Span startToolSpan(ToolCallback callback, String toolInput) { static Flux wrapChatFlux(final Object model, final Prompt prompt, final Flux source) { final Flux typedSource = (Flux) source; return Flux.defer(() -> { + final long startNanos = System.nanoTime(); final Span span = startModelSpan(model, "chat", prompt, true); + final AtomicBoolean firstChunkRecorded = new AtomicBoolean(); + final Flux timedSource = typedSource.doOnNext(value -> { + if (firstChunkRecorded.compareAndSet(false, true)) { + double elapsedSeconds = Math.max(0, System.nanoTime() - startNanos) / 1_000_000_000d; + if (span != null) { + span.withOtelAttribute("gen_ai.response.time_to_first_chunk", elapsedSeconds); + } + ChatOptions options = prompt.getOptions(); + SpringAiMetrics.recordTimeToFirstChunk(providerFor(model), + options == null ? null : options.getModel(), elapsedSeconds); + } + }); if (span == null) { - return typedSource; + return timedSource; } final AtomicReference lastResponse = new AtomicReference<>(); final AtomicReference failure = new AtomicReference<>(); final AtomicBoolean ended = new AtomicBoolean(); span.withSync(false); - return typedSource + return timedSource .doOnNext(value -> { if (value instanceof ChatResponse) { lastResponse.set((ChatResponse) value); diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java index 1fae5aa01b..4c9814cad4 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java @@ -19,9 +19,13 @@ package co.elastic.apm.agent.springai; import co.elastic.apm.agent.AbstractInstrumentationTest; +import co.elastic.apm.agent.embeddedotel.EmbeddedSdkManager; import co.elastic.apm.agent.impl.transaction.SpanImpl; import co.elastic.apm.agent.impl.transaction.TransactionImpl; import co.elastic.apm.agent.tracer.configuration.CoreConfiguration; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.opentelemetry.sdk.metrics.SdkMeterProvider; import org.junit.jupiter.api.Test; import org.springframework.ai.chat.messages.AssistantMessage; import org.springframework.ai.chat.metadata.ChatGenerationMetadata; @@ -47,6 +51,7 @@ import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext; import reactor.core.publisher.Flux; +import java.time.Duration; import java.util.Collections; import java.util.List; @@ -55,6 +60,8 @@ class SpringAiInstrumentationTest extends AbstractInstrumentationTest { + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + @Test void capturesChatCallAndSemanticAttributes() { TransactionImpl transaction = startTestRootTransaction(); @@ -84,19 +91,57 @@ void capturesChatCallAndSemanticAttributes() { } @Test - void capturesStreamingChatUntilFluxTerminates() { + void capturesStreamingChatUntilFluxTerminates() throws Exception { TransactionImpl transaction = startTestRootTransaction(); try { ChatOptions options = ChatOptions.builder().model("gpt-stream").build(); - new OpenAiTestChatModel().stream(new Prompt("secret stream prompt", options)).blockLast(); + Prompt prompt = new Prompt("secret stream prompt", options); + new OpenAiTestChatModel().stream(prompt).blockLast(); + new DelayedOpenAiTestChatModel(20).stream(prompt).blockLast(); + new DelayedOpenAiTestChatModel(200).stream(prompt).blockLast(); } finally { transaction.deactivate().end(); } - assertThat(reporter.getFirstSpan()) + SpanImpl span = reporter.getFirstSpan(); + assertThat(span) .hasName("chat gpt-stream") .hasOtelAttribute("gen_ai.request.stream", true) .hasOtelAttribute("gen_ai.usage.total_tokens", 15); + org.assertj.core.api.Assertions.assertThat( + ((Number) span.getOtelAttributes().get("gen_ai.response.time_to_first_chunk")).doubleValue()) + .isGreaterThanOrEqualTo(0); + + EmbeddedSdkManager sdkManager = tracer.getLifecycleListener(EmbeddedSdkManager.class); + ((SdkMeterProvider) sdkManager.getMeterProvider().getDelegate()).forceFlush().join(10, + java.util.concurrent.TimeUnit.SECONDS); + org.assertj.core.api.Assertions.assertThat(reporter.getBytes()).hasSize(1); + JsonNode metricSet = OBJECT_MAPPER.readTree(reporter.getBytes().get(0)).get("metricset"); + JsonNode sample = metricSet.get("samples").get(SpringAiMetrics.TIME_TO_FIRST_CHUNK); + org.assertj.core.api.Assertions.assertThat(sample.get("type").asText()).isEqualTo("histogram"); + long count = 0; + for (JsonNode bucketCount : sample.get("counts")) { + count += bucketCount.asLong(); + } + org.assertj.core.api.Assertions.assertThat(count).isEqualTo(3); + org.assertj.core.api.Assertions.assertThat(sample.get("values").size()).isGreaterThanOrEqualTo(2); + org.assertj.core.api.Assertions.assertThat(metricSet.get("tags").toString()) + .contains("gen_ai_operation_name", "chat", "gen_ai_provider_name", "openai", + "gen_ai_request_model", "gpt-stream"); + + } + + @Test + void doesNotRecordTimeToFirstChunkForEmptyStream() { + TransactionImpl transaction = startTestRootTransaction(); + try { + new EmptyOpenAiTestChatModel().stream(new Prompt("empty stream")).blockLast(); + } finally { + transaction.deactivate().end(); + } + + org.assertj.core.api.Assertions.assertThat(reporter.getFirstSpan().getOtelAttributes()) + .doesNotContainKey("gen_ai.response.time_to_first_chunk"); } @Test @@ -274,7 +319,7 @@ public Flux stream(Prompt prompt) { return Flux.just(response()); } - private static ChatResponse response() { + static ChatResponse response() { Generation generation = new Generation(new AssistantMessage("secret response"), ChatGenerationMetadata.builder().finishReason("stop").build()); ChatResponseMetadata metadata = ChatResponseMetadata.builder() @@ -286,6 +331,26 @@ private static ChatResponse response() { } } + static class DelayedOpenAiTestChatModel extends OpenAiTestChatModel { + private final long delayMillis; + + DelayedOpenAiTestChatModel(long delayMillis) { + this.delayMillis = delayMillis; + } + + @Override + public Flux stream(Prompt prompt) { + return Flux.just(response()).delaySubscription(Duration.ofMillis(delayMillis)); + } + } + + static class EmptyOpenAiTestChatModel extends OpenAiTestChatModel { + @Override + public Flux stream(Prompt prompt) { + return Flux.empty(); + } + } + static class OpenAiTestEmbeddingModel implements EmbeddingModel { @Override public EmbeddingResponse call(EmbeddingRequest request) { From e92ade2df808243a5f933168810d50f3aed7c883 Mon Sep 17 00:00:00 2001 From: hegerchr Date: Wed, 11 Nov 2026 09:52:56 +0100 Subject: [PATCH 04/15] Guard Spring AI instrumentation to version 2.x --- .../apm-spring-ai-plugin/pom.xml | 79 ++++++++++++++++ .../AbstractSpringAiInstrumentation.java | 4 +- .../SpringAiClassLoaderMatcherTest.java | 91 +++++++++++++++++++ 3 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiClassLoaderMatcherTest.java diff --git a/apm-agent-plugins/apm-spring-ai-plugin/pom.xml b/apm-agent-plugins/apm-spring-ai-plugin/pom.xml index 9288c49dbb..f509102459 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/pom.xml +++ b/apm-agent-plugins/apm-spring-ai-plugin/pom.xml @@ -17,6 +17,9 @@ 8 8 2.0.2 + 2.0.0 + 1.0.8 + 1.1.7 3.8.8 2.24.3 @@ -71,4 +74,80 @@ test + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-spring-ai-compatibility-artifacts + generate-test-resources + + copy + + + + + org.springframework.ai + spring-ai-model + ${version.spring-ai-1-0} + ${project.build.directory}/compatibility-libs/${version.spring-ai-1-0} + + + org.springframework.ai + spring-ai-commons + ${version.spring-ai-1-0} + ${project.build.directory}/compatibility-libs/${version.spring-ai-1-0} + + + org.springframework.ai + spring-ai-vector-store + ${version.spring-ai-1-0} + ${project.build.directory}/compatibility-libs/${version.spring-ai-1-0} + + + org.springframework.ai + spring-ai-model + ${version.spring-ai-1-1} + ${project.build.directory}/compatibility-libs/${version.spring-ai-1-1} + + + org.springframework.ai + spring-ai-commons + ${version.spring-ai-1-1} + ${project.build.directory}/compatibility-libs/${version.spring-ai-1-1} + + + org.springframework.ai + spring-ai-vector-store + ${version.spring-ai-1-1} + ${project.build.directory}/compatibility-libs/${version.spring-ai-1-1} + + + org.springframework.ai + spring-ai-model + ${version.spring-ai-2-min} + ${project.build.directory}/compatibility-libs/${version.spring-ai-2-min} + + + org.springframework.ai + spring-ai-commons + ${version.spring-ai-2-min} + ${project.build.directory}/compatibility-libs/${version.spring-ai-2-min} + + + org.springframework.ai + spring-ai-vector-store + ${version.spring-ai-2-min} + ${project.build.directory}/compatibility-libs/${version.spring-ai-2-min} + + + + + + + + diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/AbstractSpringAiInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/AbstractSpringAiInstrumentation.java index 83598e72dc..2d06a2adb7 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/AbstractSpringAiInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/AbstractSpringAiInstrumentation.java @@ -31,7 +31,9 @@ abstract class AbstractSpringAiInstrumentation extends ElasticApmInstrumentation @Override public ElementMatcher.Junction getClassLoaderMatcher() { return classLoaderCanLoadClass("org.springframework.ai.model.Model") - .and(classLoaderCanLoadClass("org.springframework.ai.observation.conventions.AiOperationType")); + .and(classLoaderCanLoadClass("org.springframework.ai.observation.conventions.AiOperationType")) + // DefaultEmbeddingOptions.Builder was introduced in Spring AI 2.0. + .and(classLoaderCanLoadClass("org.springframework.ai.embedding.DefaultEmbeddingOptions$Builder")); } @Override diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiClassLoaderMatcherTest.java b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiClassLoaderMatcherTest.java new file mode 100644 index 0000000000..fcb587d4f3 --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiClassLoaderMatcherTest.java @@ -0,0 +1,91 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import org.junit.jupiter.api.Test; + +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class SpringAiClassLoaderMatcherTest { + + private static final String MODEL_CLASS = "org/springframework/ai/model/Model.class"; + private static final String AI_OPERATION_TYPE_CLASS = + "org/springframework/ai/observation/conventions/AiOperationType.class"; + private static final String VECTOR_STORE_CLASS = + "org/springframework/ai/vectorstore/observation/AbstractObservationVectorStore.class"; + private static final String SPRING_AI_2_MARKER_CLASS = + "org/springframework/ai/embedding/DefaultEmbeddingOptions$Builder.class"; + + @Test + void rejectsUnsupportedSpringAi1xVersions() throws Exception { + assertUnsupported("1.0.8"); + assertUnsupported("1.1.7"); + } + + @Test + void acceptsMinimumSupportedSpringAi2xVersion() throws Exception { + try (URLClassLoader classLoader = springAiClassLoader("2.0.0")) { + assertCommonSpringAiClassesPresent(classLoader); + assertThat(classLoader.getResource(SPRING_AI_2_MARKER_CLASS)).isNotNull(); + assertThat(instrumentations()).allSatisfy(instrumentation -> + assertThat(instrumentation.getClassLoaderMatcher().matches(classLoader)).isTrue()); + } + } + + private static void assertUnsupported(String version) throws Exception { + try (URLClassLoader classLoader = springAiClassLoader(version)) { + assertCommonSpringAiClassesPresent(classLoader); + assertThat(classLoader.getResource(SPRING_AI_2_MARKER_CLASS)).isNull(); + assertThat(instrumentations()).allSatisfy(instrumentation -> + assertThat(instrumentation.getClassLoaderMatcher().matches(classLoader)).isFalse()); + } + } + + private static void assertCommonSpringAiClassesPresent(ClassLoader classLoader) { + assertThat(classLoader.getResource(MODEL_CLASS)).isNotNull(); + assertThat(classLoader.getResource(AI_OPERATION_TYPE_CLASS)).isNotNull(); + assertThat(classLoader.getResource(VECTOR_STORE_CLASS)).isNotNull(); + } + + private static List instrumentations() { + return Arrays.asList( + new ChatModelInstrumentation(), + new StreamingChatModelInstrumentation(), + new EmbeddingModelInstrumentation(), + new VectorStoreInstrumentation(), + new VectorStoreQueryInstrumentation(), + new ToolCallbackInstrumentation() + ); + } + + private static URLClassLoader springAiClassLoader(String version) throws Exception { + Path directory = Paths.get("target", "compatibility-libs", version); + URL model = directory.resolve("spring-ai-model-" + version + ".jar").toUri().toURL(); + URL commons = directory.resolve("spring-ai-commons-" + version + ".jar").toUri().toURL(); + URL vectorStore = directory.resolve("spring-ai-vector-store-" + version + ".jar").toUri().toURL(); + return new URLClassLoader(new URL[]{model, commons, vectorStore}, null); + } +} From 3064353e05778d2221d64921490da5cf8f24a341 Mon Sep 17 00:00:00 2001 From: hegerchr Date: Wed, 11 Nov 2026 12:40:17 +0100 Subject: [PATCH 05/15] Fix Spring AI streaming and tool content capture --- .../agent/springai/SpringAiSpanHelper.java | 83 ++++++++++++++++--- .../springai/SpringAiInstrumentationTest.java | 58 ++++++++++++- 2 files changed, 127 insertions(+), 14 deletions(-) diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java index cfa6cf4d06..69306efbd5 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java @@ -24,12 +24,15 @@ import co.elastic.apm.agent.tracer.TraceState; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.configuration.CoreConfiguration; +import org.springframework.ai.chat.messages.AssistantMessage; import org.springframework.ai.chat.messages.Message; +import org.springframework.ai.chat.messages.ToolResponseMessage; import org.springframework.ai.chat.metadata.ChatGenerationMetadata; import org.springframework.ai.chat.metadata.ChatResponseMetadata; import org.springframework.ai.chat.metadata.Usage; import org.springframework.ai.chat.model.ChatResponse; import org.springframework.ai.chat.model.Generation; +import org.springframework.ai.chat.model.MessageAggregator; import org.springframework.ai.chat.prompt.ChatOptions; import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.document.Document; @@ -185,12 +188,12 @@ static Span startToolSpan(ToolCallback callback, String toolInput) { @SuppressWarnings("unchecked") static Flux wrapChatFlux(final Object model, final Prompt prompt, final Flux source) { - final Flux typedSource = (Flux) source; + final Flux typedSource = (Flux) source; return Flux.defer(() -> { final long startNanos = System.nanoTime(); final Span span = startModelSpan(model, "chat", prompt, true); final AtomicBoolean firstChunkRecorded = new AtomicBoolean(); - final Flux timedSource = typedSource.doOnNext(value -> { + final Flux timedSource = typedSource.doOnNext(value -> { if (firstChunkRecorded.compareAndSet(false, true)) { double elapsedSeconds = Math.max(0, System.nanoTime() - startNanos) / 1_000_000_000d; if (span != null) { @@ -205,19 +208,20 @@ static Flux wrapChatFlux(final Object model, final Prompt prompt, final Flux< return timedSource; } final AtomicReference lastResponse = new AtomicReference<>(); + final AtomicReference aggregatedResponse = new AtomicReference<>(); final AtomicReference failure = new AtomicReference<>(); final AtomicBoolean ended = new AtomicBoolean(); span.withSync(false); - return timedSource - .doOnNext(value -> { - if (value instanceof ChatResponse) { - lastResponse.set((ChatResponse) value); - } - }) + Flux observedSource = config.isCaptureAiContent() + ? new MessageAggregator().aggregate(timedSource, aggregatedResponse::set) + : timedSource; + return observedSource + .doOnNext(lastResponse::set) .doOnError(failure::set) .doFinally(signal -> { if (ended.compareAndSet(false, true)) { - addChatResponseAttributes(span, lastResponse.get()); + ChatResponse response = aggregatedResponse.get(); + addChatResponseAttributes(span, response == null ? lastResponse.get() : response); endAsyncSpan(span, failure.get()); } }) @@ -358,13 +362,68 @@ private static String serializeMessages(List messages) { Message message = messages.get(i); result.append("{\"role\":"); appendJsonString(result, message.getMessageType().getValue()); - result.append(",\"parts\":[{\"type\":\"text\",\"content\":"); - appendJsonString(result, message.getText()); - result.append("}]}"); + result.append(",\"parts\":["); + boolean partAdded = false; + String text = message.getText(); + if (text != null && !text.isEmpty()) { + appendTextPart(result, text); + partAdded = true; + } + if (message instanceof AssistantMessage) { + for (AssistantMessage.ToolCall toolCall : ((AssistantMessage) message).getToolCalls()) { + appendPartSeparator(result, partAdded); + appendToolCallPart(result, toolCall); + partAdded = true; + } + } + if (message instanceof ToolResponseMessage) { + for (ToolResponseMessage.ToolResponse response : ((ToolResponseMessage) message).getResponses()) { + appendPartSeparator(result, partAdded); + appendToolResponsePart(result, response); + partAdded = true; + } + } + if (!partAdded) { + appendTextPart(result, text); + } + result.append("]}"); } return result.append(']').toString(); } + private static void appendPartSeparator(StringBuilder target, boolean partAdded) { + if (partAdded) { + target.append(','); + } + } + + private static void appendTextPart(StringBuilder target, @Nullable String content) { + target.append("{\"type\":\"text\",\"content\":"); + appendJsonString(target, content); + target.append('}'); + } + + private static void appendToolCallPart(StringBuilder target, AssistantMessage.ToolCall toolCall) { + target.append("{\"type\":\"tool_call\",\"id\":"); + appendJsonString(target, toolCall.id()); + target.append(",\"name\":"); + appendJsonString(target, toolCall.name()); + target.append(",\"arguments\":"); + appendJsonString(target, toolCall.arguments()); + target.append('}'); + } + + private static void appendToolResponsePart(StringBuilder target, + ToolResponseMessage.ToolResponse response) { + target.append("{\"type\":\"tool_call_response\",\"id\":"); + appendJsonString(target, response.id()); + target.append(",\"name\":"); + appendJsonString(target, response.name()); + target.append(",\"response\":"); + appendJsonString(target, response.responseData()); + target.append('}'); + } + private static String serializeGenerations(List generations) { List messages = new ArrayList<>(); for (Generation generation : generations) { diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java index 4c9814cad4..3cda67fd56 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java @@ -28,6 +28,9 @@ import io.opentelemetry.sdk.metrics.SdkMeterProvider; import org.junit.jupiter.api.Test; import org.springframework.ai.chat.messages.AssistantMessage; +import org.springframework.ai.chat.messages.SystemMessage; +import org.springframework.ai.chat.messages.ToolResponseMessage; +import org.springframework.ai.chat.messages.UserMessage; import org.springframework.ai.chat.metadata.ChatGenerationMetadata; import org.springframework.ai.chat.metadata.ChatResponseMetadata; import org.springframework.ai.chat.metadata.DefaultUsage; @@ -92,6 +95,7 @@ void capturesChatCallAndSemanticAttributes() { @Test void capturesStreamingChatUntilFluxTerminates() throws Exception { + enableContentCapture(); TransactionImpl transaction = startTestRootTransaction(); try { ChatOptions options = ChatOptions.builder().model("gpt-stream").build(); @@ -107,7 +111,9 @@ void capturesStreamingChatUntilFluxTerminates() throws Exception { assertThat(span) .hasName("chat gpt-stream") .hasOtelAttribute("gen_ai.request.stream", true) - .hasOtelAttribute("gen_ai.usage.total_tokens", 15); + .hasOtelAttribute("gen_ai.usage.total_tokens", 15) + .hasOtelAttribute("gen_ai.output.messages", + "[{\"role\":\"assistant\",\"parts\":[{\"type\":\"text\",\"content\":\"secret response\"}]}]"); org.assertj.core.api.Assertions.assertThat( ((Number) span.getOtelAttributes().get("gen_ai.response.time_to_first_chunk")).doubleValue()) .isGreaterThanOrEqualTo(0); @@ -229,6 +235,48 @@ void capturesChatContentWhenEnabled() { .allSatisfy(value -> org.assertj.core.api.Assertions.assertThat(value.toString()).contains("secret")); } + @Test + void capturesToolCallsAndResponsesInChatMessages() throws Exception { + enableContentCapture(); + AssistantMessage toolCall = AssistantMessage.builder() + .content("") + .toolCalls(Collections.singletonList(new AssistantMessage.ToolCall( + "call-1", "function", "echo", "{\"message\":\"Hello from Spring AI\"}"))) + .build(); + ToolResponseMessage toolResponse = ToolResponseMessage.builder() + .responses(Collections.singletonList(new ToolResponseMessage.ToolResponse( + "call-1", "echo", "[{\"text\":\"Hello from Spring AI\"}]"))) + .build(); + Prompt prompt = new Prompt(List.of( + new SystemMessage("Call the echo tool"), + new UserMessage("Echo this message"), + toolCall, + toolResponse + )); + + TransactionImpl transaction = startTestRootTransaction(); + try { + new OpenAiTestChatModel().call(prompt); + } finally { + transaction.deactivate().end(); + } + + String messages = reporter.getFirstSpan().getOtelAttributes().get("gen_ai.input.messages").toString(); + JsonNode parsed = OBJECT_MAPPER.readTree(messages); + org.assertj.core.api.Assertions.assertThat(parsed.get(2).get("role").asText()).isEqualTo("assistant"); + org.assertj.core.api.Assertions.assertThat(parsed.get(2).get("parts").get(0).get("type").asText()) + .isEqualTo("tool_call"); + org.assertj.core.api.Assertions.assertThat(parsed.get(2).get("parts").get(0).get("name").asText()) + .isEqualTo("echo"); + org.assertj.core.api.Assertions.assertThat(parsed.get(2).get("parts").get(0).get("arguments").asText()) + .contains("Hello from Spring AI"); + org.assertj.core.api.Assertions.assertThat(parsed.get(3).get("role").asText()).isEqualTo("tool"); + org.assertj.core.api.Assertions.assertThat(parsed.get(3).get("parts").get(0).get("type").asText()) + .isEqualTo("tool_call_response"); + org.assertj.core.api.Assertions.assertThat(parsed.get(3).get("parts").get(0).get("response").asText()) + .contains("Hello from Spring AI"); + } + @Test void capturesEmbeddingAndToolContentWhenEnabled() { enableContentCapture(); @@ -316,7 +364,13 @@ public ChatResponse call(Prompt prompt) { @Override public Flux stream(Prompt prompt) { - return Flux.just(response()); + return Flux.just( + new ChatResponse(Collections.singletonList( + new Generation(new AssistantMessage("secret ")))), + new ChatResponse(Collections.singletonList( + new Generation(new AssistantMessage("response")))), + new ChatResponse(Collections.emptyList(), response().getMetadata()) + ); } static ChatResponse response() { From ea290184117f1450fde0a176dcfede1b2a878a1b Mon Sep 17 00:00:00 2001 From: hegerchr Date: Wed, 11 Nov 2026 14:57:35 +0100 Subject: [PATCH 06/15] Clarify Kibana support for AI content capture --- docs/reference/config-ai.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/reference/config-ai.md b/docs/reference/config-ai.md index cf1915d31a..194ba6eeac 100644 --- a/docs/reference/config-ai.md +++ b/docs/reference/config-ai.md @@ -22,6 +22,8 @@ Captured content can contain sensitive information and can significantly increas [![dynamic config](images/dynamic-config.svg "") ](/reference/configuration.md#configuration-dynamic) +Support for configuring this option through Kibana central configuration will be added in a future Kibana version. + | Default | Type | Dynamic | | --- | --- | --- | | `false` | Boolean | true | From badc8b020fedd45e8fdd7e77a3bc9efb69c72416 Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Thu, 12 Nov 2026 11:39:03 +0100 Subject: [PATCH 07/15] do review changes directly --- .../apm/agent/test/AgentTestContainer.java | 2 +- .../springai/SpringAiInstrumentationTest.java | 58 +++++++++---------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/apm-agent-common/src/test/java/co/elastic/apm/agent/test/AgentTestContainer.java b/apm-agent-common/src/test/java/co/elastic/apm/agent/test/AgentTestContainer.java index e73a391afe..251333b246 100644 --- a/apm-agent-common/src/test/java/co/elastic/apm/agent/test/AgentTestContainer.java +++ b/apm-agent-common/src/test/java/co/elastic/apm/agent/test/AgentTestContainer.java @@ -212,7 +212,7 @@ public AppServer withHttpPort(int port) { } public String getBaseUrl() { - return String.format("http://%s:%d", getContainerIpAddress(), getMappedPort(webPort)); + return String.format("http://%s:%d", getHost(), getMappedPort(webPort)); } public int getHttpPort() { diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java index 3cda67fd56..7d6cc6b2c1 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java @@ -59,6 +59,7 @@ import java.util.List; import static co.elastic.apm.agent.testutils.assertions.Assertions.assertThat; +import static java.util.concurrent.TimeUnit.SECONDS; import static org.mockito.Mockito.doReturn; class SpringAiInstrumentationTest extends AbstractInstrumentationTest { @@ -89,7 +90,7 @@ void capturesChatCallAndSemanticAttributes() { .hasOtelAttribute("gen_ai.usage.input_tokens", 10) .hasOtelAttribute("gen_ai.usage.output_tokens", 5) .hasOtelAttribute("gen_ai.usage.total_tokens", 15); - org.assertj.core.api.Assertions.assertThat(span.getOtelAttributes()) + assertThat(span.getOtelAttributes()) .doesNotContainKeys("gen_ai.input.messages", "gen_ai.output.messages"); } @@ -114,24 +115,23 @@ void capturesStreamingChatUntilFluxTerminates() throws Exception { .hasOtelAttribute("gen_ai.usage.total_tokens", 15) .hasOtelAttribute("gen_ai.output.messages", "[{\"role\":\"assistant\",\"parts\":[{\"type\":\"text\",\"content\":\"secret response\"}]}]"); - org.assertj.core.api.Assertions.assertThat( - ((Number) span.getOtelAttributes().get("gen_ai.response.time_to_first_chunk")).doubleValue()) + assertThat( + ((Number) span.getOtelAttributes().get("gen_ai.response.time_to_first_chunk")).doubleValue()) .isGreaterThanOrEqualTo(0); EmbeddedSdkManager sdkManager = tracer.getLifecycleListener(EmbeddedSdkManager.class); - ((SdkMeterProvider) sdkManager.getMeterProvider().getDelegate()).forceFlush().join(10, - java.util.concurrent.TimeUnit.SECONDS); - org.assertj.core.api.Assertions.assertThat(reporter.getBytes()).hasSize(1); + ((SdkMeterProvider) sdkManager.getMeterProvider().getDelegate()).forceFlush().join(10, SECONDS); + assertThat(reporter.getBytes()).hasSize(1); JsonNode metricSet = OBJECT_MAPPER.readTree(reporter.getBytes().get(0)).get("metricset"); JsonNode sample = metricSet.get("samples").get(SpringAiMetrics.TIME_TO_FIRST_CHUNK); - org.assertj.core.api.Assertions.assertThat(sample.get("type").asText()).isEqualTo("histogram"); + assertThat(sample.get("type").asText()).isEqualTo("histogram"); long count = 0; for (JsonNode bucketCount : sample.get("counts")) { count += bucketCount.asLong(); } - org.assertj.core.api.Assertions.assertThat(count).isEqualTo(3); - org.assertj.core.api.Assertions.assertThat(sample.get("values").size()).isGreaterThanOrEqualTo(2); - org.assertj.core.api.Assertions.assertThat(metricSet.get("tags").toString()) + assertThat(count).isEqualTo(3); + assertThat(sample.get("values").size()).isGreaterThanOrEqualTo(2); + assertThat(metricSet.get("tags").toString()) .contains("gen_ai_operation_name", "chat", "gen_ai_provider_name", "openai", "gen_ai_request_model", "gpt-stream"); @@ -146,7 +146,7 @@ void doesNotRecordTimeToFirstChunkForEmptyStream() { transaction.deactivate().end(); } - org.assertj.core.api.Assertions.assertThat(reporter.getFirstSpan().getOtelAttributes()) + assertThat(reporter.getFirstSpan().getOtelAttributes()) .doesNotContainKey("gen_ai.response.time_to_first_chunk"); } @@ -166,7 +166,7 @@ void capturesEmbeddingCall() { .hasAction("embedding") .hasOtelAttribute("gen_ai.request.embedding.dimensions", 3) .hasOtelAttribute("gen_ai.usage.input_tokens", 4); - org.assertj.core.api.Assertions.assertThat(span.getOtelAttributes()) + assertThat(span.getOtelAttributes()) .doesNotContainKey("spring.ai.embedding.request.inputs"); } @@ -186,7 +186,7 @@ void capturesToolDefinitionButNotArgumentsOrResult() { .hasSubType("internal") .hasAction("execute_tool") .hasOtelAttribute("spring.ai.tool.definition.name", "weather"); - org.assertj.core.api.Assertions.assertThat(span.getOtelAttributes()) + assertThat(span.getOtelAttributes()) .doesNotContainKeys("spring.ai.tool.call.arguments", "spring.ai.tool.call.result"); } @@ -215,7 +215,7 @@ void capturesVectorStoreQueryWithoutQueryText() { .hasOtelAttribute("db.operation.name", "query") .hasOtelAttribute("db.vector.query.top_k", 3) .hasOtelAttribute("db.vector.query.similarity_threshold", 0.5); - org.assertj.core.api.Assertions.assertThat(span.getOtelAttributes()) + assertThat(span.getOtelAttributes()) .doesNotContainKeys("db.query.text", "db.vector.query.content", "db.vector.query.filter", "db.vector.query.response.documents"); } @@ -230,9 +230,9 @@ void capturesChatContentWhenEnabled() { transaction.deactivate().end(); } - org.assertj.core.api.Assertions.assertThat(reporter.getFirstSpan().getOtelAttributes()) + assertThat(reporter.getFirstSpan().getOtelAttributes()) .extractingByKeys("gen_ai.input.messages", "gen_ai.output.messages") - .allSatisfy(value -> org.assertj.core.api.Assertions.assertThat(value.toString()).contains("secret")); + .allSatisfy(value -> assertThat(value.toString()).contains("secret")); } @Test @@ -263,17 +263,17 @@ void capturesToolCallsAndResponsesInChatMessages() throws Exception { String messages = reporter.getFirstSpan().getOtelAttributes().get("gen_ai.input.messages").toString(); JsonNode parsed = OBJECT_MAPPER.readTree(messages); - org.assertj.core.api.Assertions.assertThat(parsed.get(2).get("role").asText()).isEqualTo("assistant"); - org.assertj.core.api.Assertions.assertThat(parsed.get(2).get("parts").get(0).get("type").asText()) + assertThat(parsed.get(2).get("role").asText()).isEqualTo("assistant"); + assertThat(parsed.get(2).get("parts").get(0).get("type").asText()) .isEqualTo("tool_call"); - org.assertj.core.api.Assertions.assertThat(parsed.get(2).get("parts").get(0).get("name").asText()) + assertThat(parsed.get(2).get("parts").get(0).get("name").asText()) .isEqualTo("echo"); - org.assertj.core.api.Assertions.assertThat(parsed.get(2).get("parts").get(0).get("arguments").asText()) + assertThat(parsed.get(2).get("parts").get(0).get("arguments").asText()) .contains("Hello from Spring AI"); - org.assertj.core.api.Assertions.assertThat(parsed.get(3).get("role").asText()).isEqualTo("tool"); - org.assertj.core.api.Assertions.assertThat(parsed.get(3).get("parts").get(0).get("type").asText()) + assertThat(parsed.get(3).get("role").asText()).isEqualTo("tool"); + assertThat(parsed.get(3).get("parts").get(0).get("type").asText()) .isEqualTo("tool_call_response"); - org.assertj.core.api.Assertions.assertThat(parsed.get(3).get("parts").get(0).get("response").asText()) + assertThat(parsed.get(3).get("parts").get(0).get("response").asText()) .contains("Hello from Spring AI"); } @@ -312,8 +312,8 @@ void capturesVectorDocumentsAndQueryContentWhenEnabled() { } finally { transaction.deactivate().end(); } - org.assertj.core.api.Assertions.assertThat( - reporter.getFirstSpan().getOtelAttributes().get("db.vector.documents").toString()) + assertThat( + reporter.getFirstSpan().getOtelAttributes().get("db.vector.documents").toString()) .contains("secret stored document"); reporter.reset(); @@ -328,12 +328,12 @@ void capturesVectorDocumentsAndQueryContentWhenEnabled() { } assertThat(reporter.getFirstSpan()) .hasOtelAttribute("db.vector.query.content", "secret search query"); - org.assertj.core.api.Assertions.assertThat( - reporter.getFirstSpan().getOtelAttributes()) + assertThat( + reporter.getFirstSpan().getOtelAttributes()) .hasEntrySatisfying("db.vector.query.filter", - value -> org.assertj.core.api.Assertions.assertThat(value.toString()).contains("secret")) + value -> assertThat(value.toString()).contains("secret")) .hasEntrySatisfying("db.vector.query.response.documents", - value -> org.assertj.core.api.Assertions.assertThat(value.toString()).contains("secret result document")); + value -> assertThat(value.toString()).contains("secret result document")); } @Test From 1d86562701031167ece1b58687fb458d3d07fedc Mon Sep 17 00:00:00 2001 From: hegerchr Date: Thu, 12 Nov 2026 11:54:27 +0100 Subject: [PATCH 08/15] test: add Spring AI integration coverage --- integration-tests/pom.xml | 1 + integration-tests/spring-ai-test/pom.xml | 100 ++++ .../apm/springai/testapp/ChatController.java | 84 +++ .../testapp/EmbeddingVectorController.java | 134 +++++ .../springai/testapp/McpToolController.java | 127 +++++ .../testapp/SpringAiTestApplication.java | 30 ++ .../testapp/TestDataConfiguration.java | 238 +++++++++ .../src/main/resources/application.properties | 4 + .../src/main/resources/mcp-test-server.py | 86 ++++ .../springai/SpringAiInstrumentationIT.java | 482 ++++++++++++++++++ pom.xml | 2 +- 11 files changed, 1287 insertions(+), 1 deletion(-) create mode 100644 integration-tests/spring-ai-test/pom.xml create mode 100644 integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/ChatController.java create mode 100644 integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/EmbeddingVectorController.java create mode 100644 integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/McpToolController.java create mode 100644 integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/SpringAiTestApplication.java create mode 100644 integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/TestDataConfiguration.java create mode 100644 integration-tests/spring-ai-test/src/main/resources/application.properties create mode 100644 integration-tests/spring-ai-test/src/main/resources/mcp-test-server.py create mode 100644 integration-tests/spring-ai-test/src/test/java/co/elastic/apm/springai/SpringAiInstrumentationIT.java diff --git a/integration-tests/pom.xml b/integration-tests/pom.xml index 64bf54cfcd..05ba70899f 100644 --- a/integration-tests/pom.xml +++ b/integration-tests/pom.xml @@ -30,6 +30,7 @@ main-app-test spring-boot-3 aws-lambda-test + spring-ai-test diff --git a/integration-tests/spring-ai-test/pom.xml b/integration-tests/spring-ai-test/pom.xml new file mode 100644 index 0000000000..cc0c8888ba --- /dev/null +++ b/integration-tests/spring-ai-test/pom.xml @@ -0,0 +1,100 @@ + + + 4.0.0 + + + co.elastic.apm + integration-tests + 1.57.2-SNAPSHOT + + + spring-ai-test + ${project.groupId}:${project.artifactId} + + + ${project.basedir}/../.. + true + 17 + 17 + 17 + 17 + 2.0.2 + 4.0.6 + + + + + + org.springframework.boot + spring-boot-dependencies + ${version.spring-boot} + pom + import + + + org.springframework.ai + spring-ai-bom + ${version.spring-ai} + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.ai + spring-ai-model + + + org.springframework.ai + spring-ai-client-chat + + + org.springframework.ai + spring-ai-starter-mcp-client + + + + com.networknt + json-schema-validator + 3.0.6 + + + org.springframework.ai + spring-ai-vector-store + + + co.elastic.apm + apm-agent-common + ${project.version} + test-jar + test + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + ${version.spring-boot} + + co.elastic.apm.springai.testapp.SpringAiTestApplication + + + + + repackage + + + + + + + diff --git a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/ChatController.java b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/ChatController.java new file mode 100644 index 0000000000..35fbf93120 --- /dev/null +++ b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/ChatController.java @@ -0,0 +1,84 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.springai.testapp; + +import org.springframework.ai.chat.model.ChatModel; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.prompt.ChatOptions; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; +import reactor.core.publisher.Flux; + +@RestController +@RequestMapping +public class ChatController { + + private final ChatModel chatModel; + + public ChatController(ChatModel chatModel) { + this.chatModel = chatModel; + } + + @GetMapping(value = "/health", produces = MediaType.TEXT_PLAIN_VALUE) + public String health() { + return "ok"; + } + + @PostMapping(value = "/chat", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public ChatReply chat(@RequestBody ChatRequest request) { + ChatResponse response = chatModel.call(toPrompt(request)); + return new ChatReply(response.getResult().getOutput().getText()); + } + + @PostMapping(value = "/chat/stream", consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.TEXT_EVENT_STREAM_VALUE) + public Flux stream(@RequestBody ChatRequest request) { + return chatModel.stream(toPrompt(request)) + .mapNotNull(response -> { + if (response.getResult() == null || response.getResult().getOutput() == null) { + return null; + } + String text = response.getResult().getOutput().getText(); + return text == null || text.isEmpty() ? null : text; + }); + } + + private static Prompt toPrompt(ChatRequest request) { + if (request == null || request.prompt() == null || request.prompt().isBlank()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "prompt must not be blank"); + } + String model = request.model() == null || request.model().isBlank() + ? TestDataConfiguration.DeterministicChatModel.MODEL_NAME + : request.model(); + return new Prompt(request.prompt(), ChatOptions.builder().model(model).build()); + } + + public record ChatRequest(String prompt, String model) { + } + + public record ChatReply(String content) { + } +} diff --git a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/EmbeddingVectorController.java b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/EmbeddingVectorController.java new file mode 100644 index 0000000000..e076a7333e --- /dev/null +++ b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/EmbeddingVectorController.java @@ -0,0 +1,134 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.springai.testapp; + +import org.springframework.ai.document.Document; +import org.springframework.ai.embedding.Embedding; +import org.springframework.ai.embedding.EmbeddingModel; +import org.springframework.ai.embedding.EmbeddingOptions; +import org.springframework.ai.embedding.EmbeddingRequest; +import org.springframework.ai.embedding.EmbeddingResponse; +import org.springframework.ai.vectorstore.SearchRequest; +import org.springframework.ai.vectorstore.VectorStore; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) +public class EmbeddingVectorController { + + private final EmbeddingModel embeddingModel; + private final VectorStore vectorStore; + + public EmbeddingVectorController(EmbeddingModel embeddingModel, VectorStore vectorStore) { + this.embeddingModel = embeddingModel; + this.vectorStore = vectorStore; + } + + @PostMapping(value = "/embeddings", consumes = MediaType.APPLICATION_JSON_VALUE) + public EmbeddingReply embed(@RequestBody EmbeddingRequestBody request) { + if (request == null || request.inputs() == null || request.inputs().isEmpty()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "inputs must not be empty"); + } + int dimensions = request.dimensions() == null + ? TestDataConfiguration.DeterministicEmbeddingModel.DEFAULT_DIMENSIONS + : request.dimensions(); + String model = request.model() == null || request.model().isBlank() + ? TestDataConfiguration.DeterministicEmbeddingModel.MODEL_NAME + : request.model(); + EmbeddingOptions options = EmbeddingOptions.builder().model(model).dimensions(dimensions).build(); + EmbeddingResponse response = embeddingModel.call(new EmbeddingRequest(request.inputs(), options)); + List vectors = response.getResults().stream().map(Embedding::getOutput).toList(); + return new EmbeddingReply(model, dimensions, vectors); + } + + @PostMapping(value = "/vector-store/documents", consumes = MediaType.APPLICATION_JSON_VALUE) + public DocumentMutationReply addDocuments(@RequestBody AddDocumentsRequest request) { + if (request == null || request.documents() == null || request.documents().isEmpty()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "documents must not be empty"); + } + List documents = request.documents().stream() + .map(input -> Document.builder().id(input.id()).text(input.text()) + .metadata(input.metadata() == null ? Map.of() : input.metadata()).build()) + .toList(); + vectorStore.add(documents); + return new DocumentMutationReply(documents.size()); + } + + @DeleteMapping(value = "/vector-store/documents", consumes = MediaType.APPLICATION_JSON_VALUE) + public DocumentMutationReply deleteDocuments(@RequestBody DeleteDocumentsRequest request) { + if (request == null || request.ids() == null || request.ids().isEmpty()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "ids must not be empty"); + } + vectorStore.delete(request.ids()); + return new DocumentMutationReply(request.ids().size()); + } + + @PostMapping(value = "/vector-store/search", consumes = MediaType.APPLICATION_JSON_VALUE) + public SearchReply search(@RequestBody VectorSearchRequest request) { + if (request == null || request.query() == null || request.query().isBlank()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "query must not be blank"); + } + SearchRequest.Builder builder = SearchRequest.builder() + .query(request.query()) + .topK(request.topK() == null ? SearchRequest.DEFAULT_TOP_K : request.topK()) + .similarityThreshold(request.similarityThreshold() == null + ? SearchRequest.SIMILARITY_THRESHOLD_ACCEPT_ALL + : request.similarityThreshold()); + if (request.filter() != null && !request.filter().isBlank()) { + builder.filterExpression(request.filter()); + } + SearchRequest searchRequest = builder.build(); + List documents = vectorStore.similaritySearch(searchRequest).stream().map(Document::getText).toList(); + return new SearchReply(documents); + } + + public record EmbeddingRequestBody(List inputs, String model, Integer dimensions) { + } + + public record EmbeddingReply(String model, int dimensions, List embeddings) { + } + + public record DocumentInput(String id, String text, Map metadata) { + } + + public record AddDocumentsRequest(List documents) { + } + + public record DeleteDocumentsRequest(List ids) { + } + + public record DocumentMutationReply(int count) { + } + + public record VectorSearchRequest(String query, Integer topK, Double similarityThreshold, String filter) { + } + + public record SearchReply(List documents) { + } +} diff --git a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/McpToolController.java b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/McpToolController.java new file mode 100644 index 0000000000..f75e32d6c0 --- /dev/null +++ b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/McpToolController.java @@ -0,0 +1,127 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.springai.testapp; + +import org.springframework.ai.chat.messages.SystemMessage; +import org.springframework.ai.chat.messages.UserMessage; +import org.springframework.ai.chat.model.ChatModel; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.mcp.SyncMcpToolCallbackProvider; +import org.springframework.ai.model.tool.ToolCallingChatOptions; +import org.springframework.ai.model.tool.ToolCallingManager; +import org.springframework.ai.model.tool.ToolExecutionResult; +import org.springframework.ai.tool.ToolCallback; +import org.springframework.ai.tool.definition.ToolDefinition; +import org.springframework.ai.tool.metadata.ToolMetadata; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +import java.util.Arrays; +import java.util.List; + +@RestController +@RequestMapping("/mcp") +public class McpToolController { + + private final ChatModel chatModel; + private final ToolCallback echoTool; + private final ToolCallingManager toolCallingManager; + + public McpToolController(ChatModel chatModel, SyncMcpToolCallbackProvider mcpTools) { + this.chatModel = chatModel; + ToolCallback[] tracedMcpTools = Arrays.stream(mcpTools.getToolCallbacks()) + .map(McpTracingToolCallback::new) + .toArray(ToolCallback[]::new); + echoTool = Arrays.stream(tracedMcpTools) + .filter(callback -> callback.getToolDefinition().name().endsWith("echo")) + .findFirst() + .orElseThrow(() -> new IllegalStateException("MCP echo tool was not discovered")); + if (chatModel instanceof TestDataConfiguration.DeterministicChatModel deterministicChatModel) { + deterministicChatModel.setToolName(echoTool.getToolDefinition().name()); + } + toolCallingManager = ToolCallingManager.builder().build(); + } + + @PostMapping(value = "/tools/echo", consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE) + public McpToolReply invokeEchoTool(@RequestBody String arguments) { + if (arguments == null || arguments.isBlank()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "tool arguments must not be blank"); + } + return new McpToolReply(echoTool.getToolDefinition().name(), echoTool.call(arguments)); + } + + @PostMapping(value = "/chat", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public McpChatReply chatWithEchoTool(@RequestBody McpChatRequest request) { + if (request == null || request.message() == null || request.message().isBlank()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "message must not be blank"); + } + ToolCallingChatOptions options = ToolCallingChatOptions.builder() + .model("tool-test") + .toolCallbacks(echoTool) + .build(); + Prompt initialPrompt = new Prompt(List.of( + new SystemMessage("Call the echo tool exactly once and use its result in the answer."), + new UserMessage("Call the echo tool with this exact message and report its result: " + + request.message().trim())), options); + ChatResponse toolCallResponse = chatModel.call(initialPrompt); + ToolExecutionResult toolResult = toolCallingManager.executeToolCalls(initialPrompt, toolCallResponse); + ChatResponse finalResponse = chatModel.call(new Prompt(toolResult.conversationHistory(), options)); + return new McpChatReply(finalResponse.getResult().getOutput().getText()); + } + + public record McpChatRequest(String message) { + } + + public record McpChatReply(String content) { + } + + public record McpToolReply(String tool, String result) { + } + + private static final class McpTracingToolCallback implements ToolCallback { + + private final ToolCallback delegate; + + private McpTracingToolCallback(ToolCallback delegate) { + this.delegate = delegate; + } + + @Override + public ToolDefinition getToolDefinition() { + return delegate.getToolDefinition(); + } + + @Override + public ToolMetadata getToolMetadata() { + return delegate.getToolMetadata(); + } + + @Override + public String call(String toolInput) { + return delegate.call(toolInput, null); + } + } +} diff --git a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/SpringAiTestApplication.java b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/SpringAiTestApplication.java new file mode 100644 index 0000000000..6c2e0d581a --- /dev/null +++ b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/SpringAiTestApplication.java @@ -0,0 +1,30 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.springai.testapp; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringAiTestApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringAiTestApplication.class, args); + } +} diff --git a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/TestDataConfiguration.java b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/TestDataConfiguration.java new file mode 100644 index 0000000000..21f07111a0 --- /dev/null +++ b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/TestDataConfiguration.java @@ -0,0 +1,238 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.springai.testapp; + +import org.springframework.ai.chat.messages.AssistantMessage; +import org.springframework.ai.chat.messages.ToolResponseMessage; +import org.springframework.ai.chat.client.ChatClient; +import org.springframework.ai.chat.metadata.ChatGenerationMetadata; +import org.springframework.ai.chat.metadata.ChatResponseMetadata; +import org.springframework.ai.chat.metadata.DefaultUsage; +import org.springframework.ai.chat.model.ChatModel; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.model.Generation; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.document.Document; +import org.springframework.ai.embedding.Embedding; +import org.springframework.ai.embedding.EmbeddingModel; +import org.springframework.ai.embedding.EmbeddingRequest; +import org.springframework.ai.embedding.EmbeddingResponse; +import org.springframework.ai.embedding.EmbeddingResponseMetadata; +import org.springframework.ai.tool.ToolCallback; +import org.springframework.ai.tool.definition.ToolDefinition; +import org.springframework.ai.vectorstore.SimpleVectorStore; +import org.springframework.ai.vectorstore.VectorStore; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import reactor.core.publisher.Flux; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Locale; + +@Configuration +public class TestDataConfiguration { + + @Bean + ChatModel deterministicChatModel() { + return new DeterministicChatModel(); + } + + @Bean + ChatClient.Builder chatClientBuilder(ChatModel chatModel) { + return ChatClient.builder(chatModel); + } + + @Bean + EmbeddingModel deterministicEmbeddingModel() { + return new DeterministicEmbeddingModel(); + } + + @Bean + VectorStore testVectorStore(EmbeddingModel embeddingModel) { + return SimpleVectorStore.builder(embeddingModel).build(); + } + + @Bean + ToolCallback echoToolCallback() { + return new EchoToolCallback(); + } + + static final class DeterministicChatModel implements ChatModel { + + static final String MODEL_NAME = "deterministic-chat"; + private volatile String toolName = "echo"; + + @Override + public ChatResponse call(Prompt prompt) { + OptionalToolResponse toolResponse = findToolResponse(prompt); + if (toolResponse.present()) { + return response("I called the echo tool. Tool result: " + toolResponse.response()); + } + String userText = prompt.getUserMessage().getText(); + if (userText.startsWith("Call the echo tool with this exact message")) { + String requestedMessage = userText.substring(userText.lastIndexOf(':') + 1).trim(); + String arguments = "{\"message\":\"" + jsonEscape(requestedMessage) + "\",\"repeat\":1}"; + AssistantMessage output = AssistantMessage.builder() + .content("") + .toolCalls(List.of(new AssistantMessage.ToolCall("integration-call-1", "function", toolName, arguments))) + .build(); + return new ChatResponse(Collections.singletonList(new Generation(output)), responseMetadata()); + } + String promptText = prompt.getInstructions().get(0).getText(); + return response("Echo: " + promptText); + } + + void setToolName(String toolName) { + this.toolName = toolName; + } + + @Override + public Flux stream(Prompt prompt) { + String promptText = prompt.getInstructions().get(0).getText(); + ChatResponse finalResponse = response("Echo: " + promptText); + return Flux.just( + contentChunk("Echo: "), + contentChunk(promptText), + new ChatResponse(Collections.emptyList(), finalResponse.getMetadata()) + ) + .delayElements(Duration.ofMillis(20)); + } + + private static ChatResponse contentChunk(String content) { + Generation generation = new Generation(new AssistantMessage(content), + ChatGenerationMetadata.builder().finishReason("stop").build()); + return new ChatResponse(Collections.singletonList(generation)); + } + + private static ChatResponse response(String content) { + Generation generation = new Generation(new AssistantMessage(content), + ChatGenerationMetadata.builder().finishReason("stop").build()); + return new ChatResponse(Collections.singletonList(generation), responseMetadata()); + } + + private static ChatResponseMetadata responseMetadata() { + return ChatResponseMetadata.builder() + .id("deterministic-response") + .model(MODEL_NAME) + .usage(new DefaultUsage(3, 2, 5)) + .build(); + } + + private static OptionalToolResponse findToolResponse(Prompt prompt) { + return prompt.getInstructions().stream() + .filter(ToolResponseMessage.class::isInstance) + .map(ToolResponseMessage.class::cast) + .flatMap(message -> message.getResponses().stream()) + .findFirst() + .map(response -> new OptionalToolResponse(true, response.responseData())) + .orElseGet(() -> new OptionalToolResponse(false, "")); + } + + private static String jsonEscape(String value) { + return value.replace("\\", "\\\\").replace("\"", "\\\""); + } + + private record OptionalToolResponse(boolean present, String response) { + } + } + + static final class DeterministicEmbeddingModel implements EmbeddingModel { + + static final String MODEL_NAME = "deterministic-embedding"; + static final int DEFAULT_DIMENSIONS = 8; + + @Override + public EmbeddingResponse call(EmbeddingRequest request) { + int dimensions = request.getOptions() == null || request.getOptions().getDimensions() == null + ? DEFAULT_DIMENSIONS + : request.getOptions().getDimensions(); + List embeddings = new ArrayList<>(); + int tokenCount = 0; + for (int index = 0; index < request.getInstructions().size(); index++) { + String input = request.getInstructions().get(index); + embeddings.add(new Embedding(toVector(input, dimensions), index)); + tokenCount += tokens(input).length; + } + String model = request.getOptions() == null || request.getOptions().getModel() == null + ? MODEL_NAME + : request.getOptions().getModel(); + return new EmbeddingResponse(embeddings, + new EmbeddingResponseMetadata(model, new DefaultUsage(tokenCount, 0, tokenCount))); + } + + @Override + public float[] embed(Document document) { + return embed(getEmbeddingContent(document)); + } + + @Override + public int dimensions() { + return DEFAULT_DIMENSIONS; + } + + private static float[] toVector(String input, int dimensions) { + float[] vector = new float[dimensions]; + for (String token : tokens(input)) { + int hash = token.hashCode(); + vector[Math.floorMod(hash, dimensions)] += (hash & 1) == 0 ? 1.0f : -1.0f; + } + double magnitude = 0; + for (float value : vector) { + magnitude += value * value; + } + if (magnitude == 0) { + vector[0] = 1.0f; + return vector; + } + float divisor = (float) Math.sqrt(magnitude); + for (int index = 0; index < vector.length; index++) { + vector[index] /= divisor; + } + return vector; + } + + private static String[] tokens(String input) { + if (input == null || input.isBlank()) { + return new String[0]; + } + String normalized = input.toLowerCase(Locale.ROOT).replaceAll("[^a-z0-9]+", " ").trim(); + return normalized.isEmpty() ? new String[0] : normalized.split("\\s+"); + } + } + + static final class EchoToolCallback implements ToolCallback { + + @Override + public ToolDefinition getToolDefinition() { + return ToolDefinition.builder() + .name("echo") + .description("Returns the supplied message") + .inputSchema("{\"type\":\"object\",\"properties\":{\"message\":{\"type\":\"string\"}}}") + .build(); + } + + @Override + public String call(String toolInput) { + return "Echo result: " + toolInput; + } + } +} diff --git a/integration-tests/spring-ai-test/src/main/resources/application.properties b/integration-tests/spring-ai-test/src/main/resources/application.properties new file mode 100644 index 0000000000..87be227460 --- /dev/null +++ b/integration-tests/spring-ai-test/src/main/resources/application.properties @@ -0,0 +1,4 @@ +spring.application.name=spring-ai-integration-test +spring.main.banner-mode=off +spring.ai.mcp.client.stdio.connections.test-tools.command=${MCP_SERVER_COMMAND:python3} +spring.ai.mcp.client.stdio.connections.test-tools.args[0]=${MCP_SERVER_SCRIPT:mcp-test-server.py} diff --git a/integration-tests/spring-ai-test/src/main/resources/mcp-test-server.py b/integration-tests/spring-ai-test/src/main/resources/mcp-test-server.py new file mode 100644 index 0000000000..402ae90091 --- /dev/null +++ b/integration-tests/spring-ai-test/src/main/resources/mcp-test-server.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 +# Licensed to Elasticsearch B.V. under one or more contributor license agreements. +# See the NOTICE file distributed with this work for additional information +# regarding copyright ownership. Elasticsearch B.V. licenses this file to you +# under the Apache License, Version 2.0 (the "License"); you may not use this +# file except in compliance with the License. You may obtain a copy of the +# License at http://www.apache.org/licenses/LICENSE-2.0 +"""Minimal deterministic MCP stdio server for Spring AI integration tests.""" + +import json +import sys + + +TOOL = { + "name": "echo", + "description": "Echo a test message, optionally repeated.", + "inputSchema": { + "type": "object", + "properties": { + "message": {"type": "string"}, + "repeat": {"type": "integer", "minimum": 1, "maximum": 10, "default": 1}, + }, + "required": ["message"], + "additionalProperties": False, + }, +} + + +def send(message): + sys.stdout.write(json.dumps(message, separators=(",", ":")) + "\n") + sys.stdout.flush() + + +def result(request_id, payload): + send({"jsonrpc": "2.0", "id": request_id, "result": payload}) + + +def error(request_id, code, message): + send({"jsonrpc": "2.0", "id": request_id, "error": {"code": code, "message": message}}) + + +def handle(request): + method = request.get("method") + request_id = request.get("id") + params = request.get("params") or {} + if method == "initialize": + result( + request_id, + { + "protocolVersion": params.get("protocolVersion", "2025-11-25"), + "capabilities": {"tools": {"listChanged": False}}, + "serverInfo": {"name": "spring-ai-integration-tools", "version": "1.0.0"}, + }, + ) + elif method == "ping": + result(request_id, {}) + elif method == "tools/list": + result(request_id, {"tools": [TOOL]}) + elif method == "tools/call": + arguments = params.get("arguments") or {} + message = arguments.get("message") + repeat = arguments.get("repeat", 1) + if params.get("name") != "echo" or not isinstance(message, str): + error(request_id, -32602, "invalid echo request") + return + result( + request_id, + { + "content": [{"type": "text", "text": " ".join([message] * repeat)}], + "isError": False, + }, + ) + elif request_id is not None: + error(request_id, -32601, "method not found") + + +def main(): + for line in sys.stdin: + try: + handle(json.loads(line)) + except Exception as exception: + print(f"MCP test server error: {exception}", file=sys.stderr, flush=True) + + +if __name__ == "__main__": + main() diff --git a/integration-tests/spring-ai-test/src/test/java/co/elastic/apm/springai/SpringAiInstrumentationIT.java b/integration-tests/spring-ai-test/src/test/java/co/elastic/apm/springai/SpringAiInstrumentationIT.java new file mode 100644 index 0000000000..6c4587c665 --- /dev/null +++ b/integration-tests/spring-ai-test/src/test/java/co/elastic/apm/springai/SpringAiInstrumentationIT.java @@ -0,0 +1,482 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.springai; + +import co.elastic.apm.agent.test.AgentFileAccessor; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Duration; +import java.util.List; +import java.util.Locale; +import java.util.Optional; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.function.Predicate; +import java.util.zip.GZIPInputStream; +import java.util.zip.InflaterInputStream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; + +class SpringAiInstrumentationIT { + + private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final HttpClient httpClient = HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(2)) + .build(); + + private static FakeApmServer apmServer; + private static Process application; + private static URI applicationUri; + private static Path applicationLog; + + @BeforeAll + static void startApplication() throws Exception { + apmServer = new FakeApmServer(); + int applicationPort = availablePort(); + applicationUri = URI.create("http://127.0.0.1:" + applicationPort); + applicationLog = Path.of("target", "spring-ai-test-app.log").toAbsolutePath(); + Files.deleteIfExists(applicationLog); + + Path agentJar = AgentFileAccessor.getPathToJavaagent(); + Path applicationJar = Path.of("target", "spring-ai-test.jar").toAbsolutePath(); + Path mcpServerScript = Path.of("target", "classes", "mcp-test-server.py").toAbsolutePath(); + assertThat(mcpServerScript).exists(); + List command = List.of( + javaBinary(), + "-javaagent:" + agentJar, + "-Delastic.apm.server_url=" + apmServer.getUrl(), + "-Delastic.apm.service_name=spring-ai-integration-test", + "-Delastic.apm.application_packages=co.elastic.apm.springai.testapp", + "-Delastic.apm.enable_experimental_instrumentations=true", + "-Delastic.apm.capture_ai_content=true", + "-Delastic.apm.central_config=false", + "-Delastic.apm.report_sync=true", + "-Delastic.apm.metrics_interval=1s", + "-Delastic.apm.transaction_ignore_urls=/health", + "-Delastic.apm.span_compression_enabled=false", + "-DMCP_SERVER_COMMAND=python3", + "-DMCP_SERVER_SCRIPT=" + mcpServerScript, + "-jar", + applicationJar.toString(), + "--server.port=" + applicationPort + ); + application = new ProcessBuilder(command) + .redirectErrorStream(true) + .redirectOutput(applicationLog.toFile()) + .start(); + waitForApplication(); + } + + @AfterAll + static void stopApplication() throws Exception { + if (application != null && application.isAlive()) { + application.destroy(); + if (!application.waitFor(10, java.util.concurrent.TimeUnit.SECONDS)) { + application.destroyForcibly(); + } + } + if (apmServer != null) { + apmServer.close(); + } + } + + @Test + void capturesSpringAiOperationsFromPackagedApplication() throws Exception { + assertSynchronousChat(); + assertStreamingChat(); + assertEmbedding(); + assertVectorStore(); + assertMcpToolPaths(); + } + + private static void assertSynchronousChat() throws Exception { + apmServer.clear(); + HttpResponse response = post("/chat", "{\"prompt\":\"sync integration prompt\",\"model\":\"chat-test\"}"); + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.body()).contains("Echo: sync integration prompt"); + + Trace trace = awaitTrace("ChatController#chat", span -> "chat chat-test".equals(span.path("name").asText())); + assertSpanBelongsToTransaction(trace); + JsonNode attributes = otelAttributes(trace.span()); + assertThat(attributes.path("gen_ai.operation.name").asText()).isEqualTo("chat"); + assertThat(attributes.path("gen_ai.request.stream").asBoolean()).isFalse(); + assertThat(attributes.path("gen_ai.input.messages").asText()).contains("sync integration prompt"); + assertThat(attributes.path("gen_ai.output.messages").asText()).contains("Echo: sync integration prompt"); + assertThat(attributes.path("gen_ai.usage.total_tokens").asInt()).isEqualTo(5); + } + + private static void assertStreamingChat() throws Exception { + apmServer.clear(); + HttpResponse response = post("/chat/stream", + "{\"prompt\":\"stream integration prompt\",\"model\":\"stream-test\"}"); + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.body()).contains("Echo:", "stream integration prompt"); + + Trace trace = awaitTrace("ChatController#stream", span -> "chat stream-test".equals(span.path("name").asText())); + assertSpanBelongsToTransaction(trace); + JsonNode attributes = otelAttributes(trace.span()); + assertThat(attributes.path("gen_ai.request.stream").asBoolean()).isTrue(); + assertThat(attributes.path("gen_ai.response.time_to_first_chunk").isNumber()).isTrue(); + assertThat(attributes.path("gen_ai.response.time_to_first_chunk").asDouble()).isGreaterThanOrEqualTo(0); + assertThat(attributes.path("gen_ai.output.messages").asText()).contains("Echo: stream integration prompt"); + assertThat(attributes.path("gen_ai.usage.total_tokens").asInt()).isEqualTo(5); + + JsonNode histogram = awaitMetric("gen_ai.client.inference.time_to_first_chunk"); + assertThat(histogram.path("values")).isNotEmpty(); + assertThat(histogram.path("counts")).isNotEmpty(); + int observationCount = 0; + for (JsonNode count : histogram.path("counts")) { + observationCount += count.asInt(); + } + assertThat(observationCount).isGreaterThanOrEqualTo(1); + } + + private static void assertEmbedding() throws Exception { + apmServer.clear(); + HttpResponse response = post("/embeddings", + "{\"inputs\":[\"integration embedding input\"],\"model\":\"embedding-test\",\"dimensions\":8}"); + assertThat(response.statusCode()).isEqualTo(200); + + Trace trace = awaitTrace("EmbeddingVectorController#embed", + span -> "embedding embedding-test".equals(span.path("name").asText())); + assertSpanBelongsToTransaction(trace); + JsonNode attributes = otelAttributes(trace.span()); + assertThat(attributes.path("gen_ai.operation.name").asText()).isEqualTo("embedding"); + assertThat(attributes.path("spring.ai.embedding.request.inputs").asText()).contains("integration embedding input"); + assertThat(attributes.path("gen_ai.request.embedding.dimensions").asInt()).isEqualTo(8); + } + + private static void assertVectorStore() throws Exception { + apmServer.clear(); + HttpResponse addResponse = post("/vector-store/documents", + "{\"documents\":[" + + "{\"id\":\"observability\",\"text\":\"distributed tracing integration document\"," + + "\"metadata\":{\"category\":\"observability\"}}," + + "{\"id\":\"gardening\",\"text\":\"growing tomatoes in a sunny garden\"," + + "\"metadata\":{\"category\":\"gardening\"}}]}"); + assertThat(addResponse.statusCode()).isEqualTo(200); + + Trace addTrace = awaitTrace("EmbeddingVectorController#addDocuments", + span -> "add".equals(otelAttributes(span).path("db.operation.name").asText())); + assertSpanBelongsToTransaction(addTrace); + assertThat(otelAttributes(addTrace.span()).path("db.vector.documents").asText()) + .contains("distributed tracing integration document"); + + apmServer.clear(); + HttpResponse searchResponse = post("/vector-store/search", + "{\"query\":\"distributed tracing\",\"topK\":3,\"similarityThreshold\":0.0," + + "\"filter\":\"category == 'observability'\"}"); + assertThat(searchResponse.statusCode()).isEqualTo(200); + assertThat(searchResponse.body()).contains("distributed tracing integration document"); + + Trace searchTrace = awaitTrace("EmbeddingVectorController#search", + span -> "query".equals(otelAttributes(span).path("db.operation.name").asText())); + assertSpanBelongsToTransaction(searchTrace); + JsonNode attributes = otelAttributes(searchTrace.span()); + assertThat(attributes.path("db.vector.query.content").asText()).isEqualTo("distributed tracing"); + assertThat(attributes.path("db.vector.query.filter").asText()).contains("observability"); + assertThat(attributes.path("db.vector.query.response.documents").asText()) + .contains("distributed tracing integration document"); + + apmServer.clear(); + HttpResponse deleteResponse = delete("/vector-store/documents", "{\"ids\":[\"observability\"]}"); + assertThat(deleteResponse.statusCode()).isEqualTo(200); + Trace deleteTrace = awaitTrace("EmbeddingVectorController#deleteDocuments", + span -> "delete".equals(otelAttributes(span).path("db.operation.name").asText())); + assertSpanBelongsToTransaction(deleteTrace); + + apmServer.clear(); + HttpResponse afterDelete = post("/vector-store/search", + "{\"query\":\"distributed tracing\",\"topK\":3,\"similarityThreshold\":0.0}"); + assertThat(afterDelete.statusCode()).isEqualTo(200); + assertThat(afterDelete.body()).doesNotContain("distributed tracing integration document"); + } + + private static void assertMcpToolPaths() throws Exception { + apmServer.clear(); + HttpResponse response = post("/mcp/tools/echo", + "{\"message\":\"Hello from direct MCP integration test\",\"repeat\":1}"); + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.body()).contains("Hello from direct MCP integration test"); + + Trace trace = awaitTrace("McpToolController#invokeEchoTool", + span -> span.path("name").asText().startsWith("execute_tool ")); + assertSpanBelongsToTransaction(trace); + JsonNode attributes = otelAttributes(trace.span()); + assertThat(attributes.path("spring.ai.tool.definition.name").asText()).endsWith("echo"); + assertThat(attributes.path("spring.ai.tool.call.arguments").asText()) + .contains("Hello from direct MCP integration test"); + assertThat(attributes.path("spring.ai.tool.call.result").asText()) + .contains("Hello from direct MCP integration test"); + + apmServer.clear(); + HttpResponse chatResponse = post("/mcp/chat", "{\"message\":\"Hello from MCP chat integration test\"}"); + assertThat(chatResponse.statusCode()).isEqualTo(200); + assertThat(chatResponse.body()).contains("Hello from MCP chat integration test"); + + await().atMost(Duration.ofSeconds(15)) + .until(() -> findTransaction("McpToolController#chatWithEchoTool").isPresent()); + ObjectNode transaction = findTransaction("McpToolController#chatWithEchoTool").orElseThrow(); + String transactionId = transaction.path("id").asText(); + await().atMost(Duration.ofSeconds(15)).until(() -> spans(transactionId, + span -> "chat tool-test".equals(span.path("name").asText())).size() >= 2); + + List chatSpans = spans(transactionId, + span -> "chat tool-test".equals(span.path("name").asText())); + assertThat(chatSpans).anySatisfy(span -> + assertThat(otelAttributes(span).path("gen_ai.output.messages").asText()) + .contains("tool_call", "Hello from MCP chat integration test")); + assertThat(chatSpans).anySatisfy(span -> { + assertThat(otelAttributes(span).path("gen_ai.input.messages").asText()) + .contains("tool_call_response", "Hello from MCP chat integration test"); + assertThat(otelAttributes(span).path("gen_ai.output.messages").asText()) + .contains("I called the echo tool", "Hello from MCP chat integration test"); + }); + + await().atMost(Duration.ofSeconds(15)).until(() -> spans(transactionId, + span -> span.path("name").asText().startsWith("execute_tool ")).size() == 1); + ObjectNode toolSpan = spans(transactionId, + span -> span.path("name").asText().startsWith("execute_tool ")).get(0); + assertThat(otelAttributes(toolSpan).path("spring.ai.tool.definition.name").asText()).endsWith("echo"); + assertThat(otelAttributes(toolSpan).path("spring.ai.tool.call.arguments").asText()) + .contains("Hello from MCP chat integration test"); + } + + private static void assertSpanBelongsToTransaction(Trace trace) { + assertThat(trace.span().path("transaction_id").asText()).isEqualTo(trace.transaction().path("id").asText()); + assertThat(trace.span().path("trace_id").asText()).isEqualTo(trace.transaction().path("trace_id").asText()); + assertThat(trace.span().path("parent_id").asText()).isEqualTo(trace.transaction().path("id").asText()); + assertThat(trace.transaction().path("span_count").path("started").asInt()).isGreaterThanOrEqualTo(1); + } + + private static JsonNode otelAttributes(ObjectNode span) { + return span.path("otel").path("attributes"); + } + + private static Trace awaitTrace(String transactionName, Predicate spanPredicate) { + await().atMost(Duration.ofSeconds(15)).until(() -> findTransaction(transactionName).isPresent()); + ObjectNode transaction = findTransaction(transactionName).orElseThrow(); + String transactionId = transaction.path("id").asText(); + await().atMost(Duration.ofSeconds(15)).until(() -> findSpan(transactionId, spanPredicate).isPresent()); + ObjectNode span = findSpan(transactionId, spanPredicate).orElseThrow(); + return new Trace(transaction, span); + } + + private static Optional findTransaction(String name) { + return apmServer.events("transaction").stream() + .filter(event -> name.equals(event.path("name").asText())) + .findFirst(); + } + + private static Optional findSpan(String transactionId, Predicate predicate) { + return apmServer.events("span").stream() + .filter(event -> transactionId.equals(event.path("transaction_id").asText())) + .filter(predicate) + .findFirst(); + } + + private static List spans(String transactionId, Predicate predicate) { + return apmServer.events("span").stream() + .filter(event -> transactionId.equals(event.path("transaction_id").asText())) + .filter(predicate) + .toList(); + } + + private static JsonNode awaitMetric(String name) { + await().atMost(Duration.ofSeconds(15)).until(() -> findMetric(name).isPresent()); + return findMetric(name).orElseThrow(); + } + + private static Optional findMetric(String name) { + return apmServer.events("metricset").stream() + .map(metricset -> metricset.path("samples").path(name)) + .filter(JsonNode::isObject) + .findFirst(); + } + + private static HttpResponse post(String path, String body) throws IOException, InterruptedException { + return request("POST", path, body); + } + + private static HttpResponse delete(String path, String body) throws IOException, InterruptedException { + return request("DELETE", path, body); + } + + private static HttpResponse request(String method, String path, String body) + throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder(applicationUri.resolve(path)) + .header("Content-Type", "application/json") + .timeout(Duration.ofSeconds(15)) + .method(method, HttpRequest.BodyPublishers.ofString(body)) + .build(); + return httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + } + + private static void waitForApplication() { + await().atMost(Duration.ofSeconds(30)).pollInterval(Duration.ofMillis(200)).until(() -> { + if (!application.isAlive()) { + throw new IllegalStateException("Spring AI test application stopped during startup:\n" + applicationLogs()); + } + try { + HttpRequest request = HttpRequest.newBuilder(applicationUri.resolve("/health")) + .timeout(Duration.ofSeconds(1)) + .GET() + .build(); + return httpClient.send(request, HttpResponse.BodyHandlers.ofString()).statusCode() == 200; + } catch (Exception ignored) { + return false; + } + }); + } + + private static String applicationLogs() { + try { + return Files.exists(applicationLog) ? Files.readString(applicationLog) : ""; + } catch (IOException e) { + return ""; + } + } + + private static int availablePort() throws IOException { + try (java.net.ServerSocket socket = new java.net.ServerSocket(0)) { + return socket.getLocalPort(); + } + } + + private static String javaBinary() { + String executable = System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win") ? "java.exe" : "java"; + return Path.of(System.getProperty("java.home"), "bin", executable).toString(); + } + + private record Trace(ObjectNode transaction, ObjectNode span) { + } + + private static final class FakeApmServer implements AutoCloseable { + + private final HttpServer server; + private final ExecutorService executor = Executors.newCachedThreadPool(); + private final List receivedEvents = new CopyOnWriteArrayList<>(); + + private FakeApmServer() throws IOException { + server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); + server.createContext("/", this::handle); + server.setExecutor(executor); + server.start(); + } + + private String getUrl() { + return "http://127.0.0.1:" + server.getAddress().getPort(); + } + + private void clear() { + receivedEvents.clear(); + } + + private List events(String type) { + return receivedEvents.stream() + .filter(event -> event.has(type)) + .map(event -> (ObjectNode) event.get(type)) + .toList(); + } + + private void handle(HttpExchange exchange) throws IOException { + try { + String path = exchange.getRequestURI().getPath(); + if ("GET".equals(exchange.getRequestMethod()) && "/".equals(path)) { + respond(exchange, 200, "{\"version\":\"8.17.0\"}"); + } else if ("POST".equals(exchange.getRequestMethod()) && "/intake/v2/events".equals(path)) { + readEvents(exchange); + respond(exchange, 202, ""); + } else if ("POST".equals(exchange.getRequestMethod()) && "/config/v1/agents".equals(path)) { + respond(exchange, 200, "{}"); + } else { + respond(exchange, 404, ""); + } + } finally { + exchange.close(); + } + } + + private void readEvents(HttpExchange exchange) throws IOException { + try (InputStream decoded = decode(exchange); + BufferedReader reader = new BufferedReader(new InputStreamReader(decoded, StandardCharsets.UTF_8))) { + String line; + while ((line = reader.readLine()) != null) { + if (!line.isBlank()) { + receivedEvents.add((ObjectNode) objectMapper.readTree(line)); + } + } + } + } + + private static InputStream decode(HttpExchange exchange) throws IOException { + InputStream body = exchange.getRequestBody(); + String encoding = exchange.getRequestHeaders().getFirst("Content-Encoding"); + if (encoding == null) { + return body; + } + if (encoding.toLowerCase(Locale.ROOT).contains("gzip")) { + return new GZIPInputStream(body); + } + if (encoding.toLowerCase(Locale.ROOT).contains("deflate")) { + return new InflaterInputStream(body); + } + return body; + } + + private static void respond(HttpExchange exchange, int status, String body) throws IOException { + byte[] bytes = body.getBytes(StandardCharsets.UTF_8); + exchange.getResponseHeaders().set("Content-Type", "application/json"); + exchange.sendResponseHeaders(status, bytes.length == 0 ? -1 : bytes.length); + if (bytes.length > 0) { + try (OutputStream output = exchange.getResponseBody()) { + output.write(bytes); + } + } + } + + @Override + public void close() { + server.stop(0); + executor.shutdownNow(); + } + } +} diff --git a/pom.xml b/pom.xml index 2fbe7f9128..d74735b7d0 100644 --- a/pom.xml +++ b/pom.xml @@ -361,7 +361,7 @@ central true - apm-agent-common,apm-agent-cached-lookup-key,apm-agent-bootstrap,apm-agent-core,elastic-apm-agent-premain,apm-agent-plugins,apm-httpclient-core,apm-apache-httpclient,apm-apache-httpclient3-plugin,apm-apache-httpclient-common,apm-apache-httpclient4-plugin,apm-apache-httpclient5-plugin,apm-api-plugin,apm-asynchttpclient-plugin,apm-java-concurrent-plugin,apm-cassandra,apm-cassandra-core-plugin,apm-cassandra3-plugin,apm-cassandra4-plugin,apm-okhttp-plugin,apm-dubbo-plugin,apm-es-restclient-plugin,apm-es-restclient-plugin-common,apm-es-restclient-plugin-5_6,apm-es-restclient-plugin-6_4,apm-es-restclient-plugin-7_x,apm-es-restclient-plugin-8_x,apm-grails-plugin,apm-grpc,apm-grpc-plugin,apm-hibernate-search-plugin,apm-hibernate-search-plugin-common,apm-hibernate-search-plugin-5_x,apm-hibernate-search-plugin-6_x,apm-httpserver-core,apm-servlet-plugin,apm-javalin-plugin,apm-jaxrs-plugin,apm-jaxws-plugin,apm-jdbc-plugin,apm-jdk-httpclient-plugin,apm-jdk-httpserver-plugin,apm-redis-plugin,apm-redis-common,apm-jedis-plugin,apm-jedis-4-plugin,apm-jms-plugin,apm-jms-plugin-base,apm-jms-javax,apm-jms-jakarta,apm-jmx-plugin,apm-jsf-plugin,apm-kafka-plugin,apm-kafka-base-plugin,apm-kafka-headers-plugin,apm-kafka-spring-plugin,apm-lettuce-plugin,apm-logging-plugin,apm-logging-plugin-common,apm-log4j1-plugin,apm-log4j2-plugin,apm-opentelemetry,apm-opentelemetry-embedded-metrics-sdk,apm-opentelemetry-metrics-bridge-parent,apm-opentelemetry-metrics-bridge-common,apm-opentelemetry-metrics-bridge-v1_14,apm-opentelemetry-metrics-bridge-latest,apm-opentelemetry-metricsdk-plugin,apm-opentelemetry-plugin,apm-slf4j-plugin,apm-logback-plugin,apm-logback-plugin-impl,apm-jboss-logging-plugin,apm-jul-plugin,apm-tomcat-logging-plugin,apm-micrometer-plugin,apm-mongodb,apm-mongodb-common,apm-mongodb3-plugin,apm-mongodb4-plugin,apm-opentracing-plugin,apm-process-plugin,apm-profiling-plugin,apm-quartz,apm-quartz-common,apm-quartz-plugin-1,apm-quartz-plugin-2,apm-rabbitmq,apm-rabbitmq-plugin,apm-rabbitmq-spring5,apm-reactor-plugin,apm-redisson-plugin,apm-scala-concurrent-plugin,apm-scheduled-annotation-plugin,apm-sparkjava-plugin,apm-urlconnection-plugin,apm-spring-ai-plugin,apm-spring-resttemplate,apm-spring-resttemplate-plugin,apm-spring-webflux,apm-spring-webflux-common-spring5,apm-spring-webflux-common,apm-spring-webclient-plugin,apm-spring-webflux-testapp,apm-spring-webflux-testapp-spring7,apm-spring-webflux-spring5,apm-spring-webflux-plugin,apm-spring-webflux-plugin-spring7,apm-spring-webmvc,apm-spring-webmvc-spring5,apm-spring-webmvc-plugin,apm-struts-plugin,apm-vertx,apm-vertx-common,apm-vertx3-plugin,apm-vertx4-plugin,apm-awslambda-plugin,apm-jakarta-websocket-plugin,apm-ecs-logging-plugin,apm-aws-sdk,apm-aws-sdk-common,apm-aws-sdk-1-plugin,apm-aws-sdk-2-plugin,apm-finagle-httpclient-plugin,apm-java-ldap-plugin,apm-agent-builds,apm-agent,apm-agent-lambda-layer,apm-agent-java8,apm-agent-benchmarks,apm-spring-resttemplate-test,apm-spring-restclient-test,apm-logback-plugin-legacy-tests,apm-log4j2-plugin-tests,apm-jms-spring-plugin,apm-jedis-2-tests,apm-jedis-3-tests,apm-jedis-5-tests,apm-lettuce-3-tests,apm-grpc-test-1.6.1,apm-grpc-test-latest,apm-rabbitmq-test-3,apm-rabbitmq-test-4,apm-rabbitmq-test-spring6,apm-okhttp-test,apm-opentelemetry-test,apm-vertx3-test-latest,apm-servlet-jakarta-test,apm-jaxws-plugin-jakartaee-test,apm-jaxrs-plugin-jakartaee-test,apm-scheduled-annotation-plugin-jakartaee-test,integration-tests,simple-webapp,jakartaee-simple-webapp,jsf-app,jsf-app-dependent,jsf-app-standalone,jakartaee-jsf-app,jakartaee-jsf-app-dependent,jakartaee-jsf-app-standalone,cdi-app,cdi-app-dependent,cdi-app-standalone,soap-test,external-plugin-test,plugin-instrumentation-target,external-plugin-app,external-plugin-jakarta-app,external-plugin,application-server-integration-tests,spring-boot-1-5,spring-boot-2,spring-boot-2-base,spring-boot-2-jetty,spring-boot-2-tomcat,spring-boot-2-undertow,cdi-jakartaee-app,cdi-jakartaee-app-dependent,cdi-jakartaee-app-standalone,quarkus,quarkus-jaxrs-base,quarkus-jaxrs-undertow,external-plugin-otel-test,external-plugin-otel-test-app,external-plugin-otel-test-plugin1,external-plugin-otel-test-plugin2,runtime-attach,runtime-attach-app,runtime-attach-test,main-app-test,spring-boot-3,spring-boot-3-jetty,spring-boot-3-tomcat,spring-boot-3-undertow,aws-lambda-test,spring-boot-4,spring-boot-4-base,spring-boot-4-jetty,spring-boot-4-tomcat,apm-spring-webmvc-spring7-test + apm-agent-common,apm-agent-cached-lookup-key,apm-agent-bootstrap,apm-agent-core,elastic-apm-agent-premain,apm-agent-plugins,apm-httpclient-core,apm-apache-httpclient,apm-apache-httpclient3-plugin,apm-apache-httpclient-common,apm-apache-httpclient4-plugin,apm-apache-httpclient5-plugin,apm-api-plugin,apm-asynchttpclient-plugin,apm-java-concurrent-plugin,apm-cassandra,apm-cassandra-core-plugin,apm-cassandra3-plugin,apm-cassandra4-plugin,apm-okhttp-plugin,apm-dubbo-plugin,apm-es-restclient-plugin,apm-es-restclient-plugin-common,apm-es-restclient-plugin-5_6,apm-es-restclient-plugin-6_4,apm-es-restclient-plugin-7_x,apm-es-restclient-plugin-8_x,apm-grails-plugin,apm-grpc,apm-grpc-plugin,apm-hibernate-search-plugin,apm-hibernate-search-plugin-common,apm-hibernate-search-plugin-5_x,apm-hibernate-search-plugin-6_x,apm-httpserver-core,apm-servlet-plugin,apm-javalin-plugin,apm-jaxrs-plugin,apm-jaxws-plugin,apm-jdbc-plugin,apm-jdk-httpclient-plugin,apm-jdk-httpserver-plugin,apm-redis-plugin,apm-redis-common,apm-jedis-plugin,apm-jedis-4-plugin,apm-jms-plugin,apm-jms-plugin-base,apm-jms-javax,apm-jms-jakarta,apm-jmx-plugin,apm-jsf-plugin,apm-kafka-plugin,apm-kafka-base-plugin,apm-kafka-headers-plugin,apm-kafka-spring-plugin,apm-lettuce-plugin,apm-logging-plugin,apm-logging-plugin-common,apm-log4j1-plugin,apm-log4j2-plugin,apm-opentelemetry,apm-opentelemetry-embedded-metrics-sdk,apm-opentelemetry-metrics-bridge-parent,apm-opentelemetry-metrics-bridge-common,apm-opentelemetry-metrics-bridge-v1_14,apm-opentelemetry-metrics-bridge-latest,apm-opentelemetry-metricsdk-plugin,apm-opentelemetry-plugin,apm-slf4j-plugin,apm-logback-plugin,apm-logback-plugin-impl,apm-jboss-logging-plugin,apm-jul-plugin,apm-tomcat-logging-plugin,apm-micrometer-plugin,apm-mongodb,apm-mongodb-common,apm-mongodb3-plugin,apm-mongodb4-plugin,apm-opentracing-plugin,apm-process-plugin,apm-profiling-plugin,apm-quartz,apm-quartz-common,apm-quartz-plugin-1,apm-quartz-plugin-2,apm-rabbitmq,apm-rabbitmq-plugin,apm-rabbitmq-spring5,apm-reactor-plugin,apm-redisson-plugin,apm-scala-concurrent-plugin,apm-scheduled-annotation-plugin,apm-sparkjava-plugin,apm-urlconnection-plugin,apm-spring-ai-plugin,apm-spring-resttemplate,apm-spring-resttemplate-plugin,apm-spring-webflux,apm-spring-webflux-common-spring5,apm-spring-webflux-common,apm-spring-webclient-plugin,apm-spring-webflux-testapp,apm-spring-webflux-testapp-spring7,apm-spring-webflux-spring5,apm-spring-webflux-plugin,apm-spring-webflux-plugin-spring7,apm-spring-webmvc,apm-spring-webmvc-spring5,apm-spring-webmvc-plugin,apm-struts-plugin,apm-vertx,apm-vertx-common,apm-vertx3-plugin,apm-vertx4-plugin,apm-awslambda-plugin,apm-jakarta-websocket-plugin,apm-ecs-logging-plugin,apm-aws-sdk,apm-aws-sdk-common,apm-aws-sdk-1-plugin,apm-aws-sdk-2-plugin,apm-finagle-httpclient-plugin,apm-java-ldap-plugin,apm-agent-builds,apm-agent,apm-agent-lambda-layer,apm-agent-java8,apm-agent-benchmarks,apm-spring-resttemplate-test,apm-spring-restclient-test,apm-logback-plugin-legacy-tests,apm-log4j2-plugin-tests,apm-jms-spring-plugin,apm-jedis-2-tests,apm-jedis-3-tests,apm-jedis-5-tests,apm-lettuce-3-tests,apm-grpc-test-1.6.1,apm-grpc-test-latest,apm-rabbitmq-test-3,apm-rabbitmq-test-4,apm-rabbitmq-test-spring6,apm-okhttp-test,apm-opentelemetry-test,apm-vertx3-test-latest,apm-servlet-jakarta-test,apm-jaxws-plugin-jakartaee-test,apm-jaxrs-plugin-jakartaee-test,apm-scheduled-annotation-plugin-jakartaee-test,integration-tests,simple-webapp,jakartaee-simple-webapp,jsf-app,jsf-app-dependent,jsf-app-standalone,jakartaee-jsf-app,jakartaee-jsf-app-dependent,jakartaee-jsf-app-standalone,cdi-app,cdi-app-dependent,cdi-app-standalone,soap-test,external-plugin-test,plugin-instrumentation-target,external-plugin-app,external-plugin-jakarta-app,external-plugin,application-server-integration-tests,spring-boot-1-5,spring-boot-2,spring-boot-2-base,spring-boot-2-jetty,spring-boot-2-tomcat,spring-boot-2-undertow,cdi-jakartaee-app,cdi-jakartaee-app-dependent,cdi-jakartaee-app-standalone,quarkus,quarkus-jaxrs-base,quarkus-jaxrs-undertow,external-plugin-otel-test,external-plugin-otel-test-app,external-plugin-otel-test-plugin1,external-plugin-otel-test-plugin2,runtime-attach,runtime-attach-app,runtime-attach-test,main-app-test,spring-boot-3,spring-boot-3-jetty,spring-boot-3-tomcat,spring-boot-3-undertow,aws-lambda-test,spring-ai-test,spring-boot-4,spring-boot-4-base,spring-boot-4-jetty,spring-boot-4-tomcat,apm-spring-webmvc-spring7-test From f6b40750bdadb8a975bf486206b9957db7663255 Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Thu, 12 Nov 2026 11:44:46 +0100 Subject: [PATCH 09/15] add missing package-info --- .../apm/agent/springai/package-info.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/package-info.java diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/package-info.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/package-info.java new file mode 100644 index 0000000000..ad73cf12f8 --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/package-info.java @@ -0,0 +1,22 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +@NonnullApi +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.sdk.NonnullApi; From a62669ea8d175f223ac3df4b59055ba55ea2881a Mon Sep 17 00:00:00 2001 From: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com> Date: Thu, 12 Nov 2026 11:58:07 +0100 Subject: [PATCH 10/15] fix few warnings --- .../co/elastic/apm/agent/springai/ChatModelInstrumentation.java | 2 ++ .../apm/agent/springai/EmbeddingModelInstrumentation.java | 2 ++ .../elastic/apm/agent/springai/ToolCallbackInstrumentation.java | 2 ++ .../elastic/apm/agent/springai/VectorStoreInstrumentation.java | 2 ++ .../apm/agent/springai/VectorStoreQueryInstrumentation.java | 2 ++ 5 files changed, 10 insertions(+) diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java index ac5abc4a7c..78c10dd1cd 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java @@ -61,6 +61,8 @@ public String getAdviceClassName() { } public static class ChatModelAdvice { + + @Nullable @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static Object onEnter(@Advice.This Object model, @Advice.Argument(0) Prompt prompt) { Span span = SpringAiSpanHelper.startModelSpan(model, "chat", prompt, false); diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java index f6491a07e0..b4dd6caacc 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java @@ -55,6 +55,8 @@ public String getAdviceClassName() { } public static class EmbeddingModelAdvice { + + @Nullable @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static Object onEnter(@Advice.This Object model, @Advice.Argument(0) EmbeddingRequest request) { Span span = SpringAiSpanHelper.startEmbeddingSpan(model, request); diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java index 2e4db12671..f1a09ea4ec 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java @@ -53,6 +53,8 @@ public String getAdviceClassName() { } public static class ToolCallbackAdvice { + + @Nullable @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static Object onEnter(@Advice.This ToolCallback callback, @Advice.Argument(0) String toolInput) { diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java index 3d65880215..6b40839023 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java @@ -61,6 +61,8 @@ public String getAdviceClassName() { } public static class VectorStoreAdvice { + + @Nullable @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static Object onEnter(@Advice.This AbstractObservationVectorStore vectorStore, @Advice.Origin("#m") String method, diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java index 60750f8020..7919265a86 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java @@ -69,6 +69,8 @@ public String getAdviceClassName() { } public static class VectorStoreQueryAdvice { + + @Nullable @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static Object onEnter(@Advice.This AbstractObservationVectorStore vectorStore, @Advice.Argument(0) SearchRequest request) { From 2dff099dba361c803c6105ac9aa39a2f75e4148d Mon Sep 17 00:00:00 2001 From: hegerchr Date: Thu, 12 Nov 2026 17:18:18 +0100 Subject: [PATCH 11/15] Fix Spring AI semantic attributes --- .../springai/ChatModelInstrumentation.java | 4 +- .../EmbeddingModelInstrumentation.java | 4 +- .../apm/agent/springai/SpringAiMetrics.java | 9 +-- .../agent/springai/SpringAiSpanHelper.java | 57 +++++-------------- .../StreamingChatModelInstrumentation.java | 5 +- .../springai/SpringAiInstrumentationTest.java | 8 +-- 6 files changed, 26 insertions(+), 61 deletions(-) diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java index 78c10dd1cd..1bef082a15 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ChatModelInstrumentation.java @@ -64,8 +64,8 @@ public static class ChatModelAdvice { @Nullable @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) - public static Object onEnter(@Advice.This Object model, @Advice.Argument(0) Prompt prompt) { - Span span = SpringAiSpanHelper.startModelSpan(model, "chat", prompt, false); + public static Object onEnter(@Advice.Argument(0) Prompt prompt) { + Span span = SpringAiSpanHelper.startModelSpan("chat", prompt, false); return span == null ? null : span.activate(); } diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java index b4dd6caacc..35b01edc68 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java @@ -58,8 +58,8 @@ public static class EmbeddingModelAdvice { @Nullable @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) - public static Object onEnter(@Advice.This Object model, @Advice.Argument(0) EmbeddingRequest request) { - Span span = SpringAiSpanHelper.startEmbeddingSpan(model, request); + public static Object onEnter(@Advice.Argument(0) EmbeddingRequest request) { + Span span = SpringAiSpanHelper.startEmbeddingSpan(request); return span == null ? null : span.activate(); } diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiMetrics.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiMetrics.java index 0b0d86e025..e10221cb73 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiMetrics.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiMetrics.java @@ -36,8 +36,6 @@ final class SpringAiMetrics { private static final Tracer tracer = GlobalTracer.get(); private static final ProxyAttributeKey OPERATION_NAME = ProxyAttributeKey.stringKey("gen_ai.operation.name"); - private static final ProxyAttributeKey PROVIDER_NAME = - ProxyAttributeKey.stringKey("gen_ai.provider.name"); private static final ProxyAttributeKey REQUEST_MODEL = ProxyAttributeKey.stringKey("gen_ai.request.model"); @Nullable @@ -46,17 +44,16 @@ final class SpringAiMetrics { private SpringAiMetrics() { } - static void recordTimeToFirstChunk(String provider, @Nullable String requestModel, double seconds) { + static void recordTimeToFirstChunk(@Nullable String requestModel, double seconds) { if (timeToFirstChunk == null) { return; } - timeToFirstChunk.record(seconds, createAttributes(provider, requestModel)); + timeToFirstChunk.record(seconds, createAttributes(requestModel)); } - private static ProxyAttributes createAttributes(String provider, @Nullable String requestModel) { + private static ProxyAttributes createAttributes(@Nullable String requestModel) { ProxyAttributesBuilder builder = ProxyAttributes.builder(); builder.put(OPERATION_NAME, "chat"); - builder.put(PROVIDER_NAME, provider); if (requestModel != null) { builder.put(REQUEST_MODEL, requestModel); } diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java index 69306efbd5..f804577989 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java @@ -64,7 +64,7 @@ private SpringAiSpanHelper() { } @Nullable - static Span startModelSpan(Object model, String operation, Prompt prompt, boolean streaming) { + static Span startModelSpan(String operation, Prompt prompt, boolean streaming) { Span span = tracer.currentContext().createSpan(); if (span == null) { return null; @@ -72,13 +72,11 @@ static Span startModelSpan(Object model, String operation, Prompt prompt, boo ChatOptions options = prompt.getOptions(); String requestModel = options == null ? null : options.getModel(); - String provider = providerFor(model); span.withName(spanName(operation, requestModel)) .withType("app") .withSubtype("internal") .withAction(operation) .withOtelAttribute("gen_ai.operation.name", operation) - .withOtelAttribute("gen_ai.system", provider) .withOtelAttribute("gen_ai.request.model", requestModel) .withOtelAttribute("gen_ai.request.stream", streaming); @@ -98,20 +96,18 @@ static Span startModelSpan(Object model, String operation, Prompt prompt, boo } @Nullable - static Span startEmbeddingSpan(Object model, EmbeddingRequest request) { + static Span startEmbeddingSpan(EmbeddingRequest request) { EmbeddingOptions options = request.getOptions(); Span span = tracer.currentContext().createSpan(); if (span == null) { return null; } String requestModel = options == null ? null : options.getModel(); - String provider = providerFor(model); span.withName(spanName("embedding", requestModel)) .withType("app") .withSubtype("internal") .withAction("embedding") .withOtelAttribute("gen_ai.operation.name", "embedding") - .withOtelAttribute("gen_ai.system", provider) .withOtelAttribute("gen_ai.request.model", requestModel) .withOtelAttribute("gen_ai.request.embedding.dimensions", options == null ? null : options.getDimensions()); if (config.isCaptureAiContent()) { @@ -133,14 +129,13 @@ static Span startVectorStoreSpan(AbstractObservationVectorStore vectorStore, builder.queryRequest(searchRequest); } VectorStoreObservationContext context = builder.build(); - String rawDbSystem = context.getDatabaseSystem(); - String dbSystem = rawDbSystem == null || rawDbSystem.isEmpty() ? "unknown" : rawDbSystem; - span.withName(dbSystem + " " + operation) + String dbSystem = context.getDatabaseSystem(); + boolean hasDbSystem = dbSystem != null && !dbSystem.isEmpty(); + span.withName((hasDbSystem ? dbSystem : "vector_store") + " " + operation) .withType("db") - .withSubtype(dbSubtype(dbSystem)) + .withSubtype("unknown") .withAction(operation) .withOtelAttribute("spring.ai.kind", "vector_store") - .withOtelAttribute("db.system", dbSystem) .withOtelAttribute("db.operation.name", operation) .withOtelAttribute("db.collection.name", context.getCollectionName()) .withOtelAttribute("db.namespace", context.getNamespace()) @@ -159,7 +154,10 @@ static Span startVectorStoreSpan(AbstractObservationVectorStore vectorStore, captureStructuredContent(span, "db.vector.documents", serializeDocuments((List) argument)); } } - span.getContext().getServiceTarget().withType(dbSystem).withName(context.getCollectionName()); + if (hasDbSystem) { + span.withOtelAttribute("db.system", dbSystem); + span.getContext().getServiceTarget().withType(dbSystem).withName(context.getCollectionName()); + } return span; } @@ -176,7 +174,6 @@ static Span startToolSpan(ToolCallback callback, String toolInput) { .withSubtype("internal") .withAction("execute_tool") .withOtelAttribute("gen_ai.operation.name", "execute_tool") - .withOtelAttribute("gen_ai.system", "spring_ai") .withOtelAttribute("spring.ai.kind", "tool_call") .withOtelAttribute("spring.ai.tool.type", "function") .withOtelAttribute("spring.ai.tool.definition.name", name) @@ -187,11 +184,11 @@ static Span startToolSpan(ToolCallback callback, String toolInput) { } @SuppressWarnings("unchecked") - static Flux wrapChatFlux(final Object model, final Prompt prompt, final Flux source) { + static Flux wrapChatFlux(final Prompt prompt, final Flux source) { final Flux typedSource = (Flux) source; return Flux.defer(() -> { final long startNanos = System.nanoTime(); - final Span span = startModelSpan(model, "chat", prompt, true); + final Span span = startModelSpan("chat", prompt, true); final AtomicBoolean firstChunkRecorded = new AtomicBoolean(); final Flux timedSource = typedSource.doOnNext(value -> { if (firstChunkRecorded.compareAndSet(false, true)) { @@ -200,7 +197,7 @@ static Flux wrapChatFlux(final Object model, final Prompt prompt, final Flux< span.withOtelAttribute("gen_ai.response.time_to_first_chunk", elapsedSeconds); } ChatOptions options = prompt.getOptions(); - SpringAiMetrics.recordTimeToFirstChunk(providerFor(model), + SpringAiMetrics.recordTimeToFirstChunk( options == null ? null : options.getModel(), elapsedSeconds); } }); @@ -321,21 +318,6 @@ private static String stringify(@Nullable List values) { return values == null || values.isEmpty() ? null : values.toString(); } - private static String providerFor(Object model) { - String className = model.getClass().getName().toLowerCase(Locale.ROOT); - if (className.contains("anthropic")) return "anthropic"; - if (className.contains("bedrock")) return "bedrock_converse"; - if (className.contains("deepseek")) return "deepseek"; - if (className.contains("vertex")) return "vertex_ai"; - if (className.contains("google") || className.contains("gemini")) return "google_genai"; - if (className.contains("mistral")) return "mistral_ai"; - if (className.contains("ollama")) return "ollama"; - if (className.contains("onnx")) return "onnx"; - if (className.contains("openai") || className.contains("azure")) return "openai"; - if (className.contains("stability")) return "stability_ai"; - return "spring_ai"; - } - private static void captureContent(Span span, String key, @Nullable String content) { if (config.isCaptureAiContent() && content != null) { span.withOtelAttribute(key, truncate(content, coreConfig.getLongFieldMaxLength())); @@ -494,17 +476,4 @@ private static void appendJsonString(StringBuilder target, @Nullable String valu target.append('"'); } - private static String dbSubtype(@Nullable String databaseSystem) { - if (databaseSystem == null) { - return "unknown"; - } - String normalized = databaseSystem.toLowerCase(Locale.ROOT); - if (normalized.contains("postgres") || normalized.contains("pg_vector")) return "postgresql"; - if (normalized.contains("mongo")) return "mongodb"; - if (normalized.contains("elastic")) return "elasticsearch"; - if (normalized.contains("redis")) return "redis"; - if (normalized.contains("cassandra")) return "cassandra"; - if (normalized.contains("cosmos")) return "cosmosdb"; - return "unknown"; - } } diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/StreamingChatModelInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/StreamingChatModelInstrumentation.java index cf7ea1906f..c0d9d84f47 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/StreamingChatModelInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/StreamingChatModelInstrumentation.java @@ -63,12 +63,11 @@ public static class StreamingChatModelAdvice { @Nullable @Advice.AssignReturned.ToReturned @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false) - public static Flux onExit(@Advice.This Object model, - @Advice.Argument(0) Prompt prompt, + public static Flux onExit(@Advice.Argument(0) Prompt prompt, @Advice.Return @Nullable Flux flux, @Advice.Thrown @Nullable Throwable thrown) { if (thrown == null && flux != null) { - return SpringAiSpanHelper.wrapChatFlux(model, prompt, flux); + return SpringAiSpanHelper.wrapChatFlux(prompt, flux); } return flux; } diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java index 7d6cc6b2c1..afad5e263b 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java @@ -84,14 +84,14 @@ void capturesChatCallAndSemanticAttributes() { .hasAction("chat") .hasOutcome(co.elastic.apm.agent.tracer.Outcome.SUCCESS) .hasOtelAttribute("gen_ai.operation.name", "chat") - .hasOtelAttribute("gen_ai.system", "openai") .hasOtelAttribute("gen_ai.request.model", "gpt-test") .hasOtelAttribute("gen_ai.response.model", "gpt-test-result") .hasOtelAttribute("gen_ai.usage.input_tokens", 10) .hasOtelAttribute("gen_ai.usage.output_tokens", 5) .hasOtelAttribute("gen_ai.usage.total_tokens", 15); assertThat(span.getOtelAttributes()) - .doesNotContainKeys("gen_ai.input.messages", "gen_ai.output.messages"); + .doesNotContainKeys("gen_ai.system", "gen_ai.provider.name", + "gen_ai.input.messages", "gen_ai.output.messages"); } @Test @@ -132,8 +132,8 @@ void capturesStreamingChatUntilFluxTerminates() throws Exception { assertThat(count).isEqualTo(3); assertThat(sample.get("values").size()).isGreaterThanOrEqualTo(2); assertThat(metricSet.get("tags").toString()) - .contains("gen_ai_operation_name", "chat", "gen_ai_provider_name", "openai", - "gen_ai_request_model", "gpt-stream"); + .contains("gen_ai_operation_name", "chat", "gen_ai_request_model", "gpt-stream") + .doesNotContain("gen_ai_provider_name"); } From cc022981b917042d34810bcc8177dfd557c10441 Mon Sep 17 00:00:00 2001 From: hegerchr Date: Fri, 13 Nov 2026 07:38:34 +0100 Subject: [PATCH 12/15] Remove Spring AI integration test --- integration-tests/pom.xml | 1 - integration-tests/spring-ai-test/pom.xml | 100 ---- .../apm/springai/testapp/ChatController.java | 84 --- .../testapp/EmbeddingVectorController.java | 134 ----- .../springai/testapp/McpToolController.java | 127 ----- .../testapp/SpringAiTestApplication.java | 30 -- .../testapp/TestDataConfiguration.java | 238 --------- .../src/main/resources/application.properties | 4 - .../src/main/resources/mcp-test-server.py | 86 ---- .../springai/SpringAiInstrumentationIT.java | 482 ------------------ pom.xml | 2 +- 11 files changed, 1 insertion(+), 1287 deletions(-) delete mode 100644 integration-tests/spring-ai-test/pom.xml delete mode 100644 integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/ChatController.java delete mode 100644 integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/EmbeddingVectorController.java delete mode 100644 integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/McpToolController.java delete mode 100644 integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/SpringAiTestApplication.java delete mode 100644 integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/TestDataConfiguration.java delete mode 100644 integration-tests/spring-ai-test/src/main/resources/application.properties delete mode 100644 integration-tests/spring-ai-test/src/main/resources/mcp-test-server.py delete mode 100644 integration-tests/spring-ai-test/src/test/java/co/elastic/apm/springai/SpringAiInstrumentationIT.java diff --git a/integration-tests/pom.xml b/integration-tests/pom.xml index 05ba70899f..64bf54cfcd 100644 --- a/integration-tests/pom.xml +++ b/integration-tests/pom.xml @@ -30,7 +30,6 @@ main-app-test spring-boot-3 aws-lambda-test - spring-ai-test diff --git a/integration-tests/spring-ai-test/pom.xml b/integration-tests/spring-ai-test/pom.xml deleted file mode 100644 index cc0c8888ba..0000000000 --- a/integration-tests/spring-ai-test/pom.xml +++ /dev/null @@ -1,100 +0,0 @@ - - - 4.0.0 - - - co.elastic.apm - integration-tests - 1.57.2-SNAPSHOT - - - spring-ai-test - ${project.groupId}:${project.artifactId} - - - ${project.basedir}/../.. - true - 17 - 17 - 17 - 17 - 2.0.2 - 4.0.6 - - - - - - org.springframework.boot - spring-boot-dependencies - ${version.spring-boot} - pom - import - - - org.springframework.ai - spring-ai-bom - ${version.spring-ai} - pom - import - - - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.ai - spring-ai-model - - - org.springframework.ai - spring-ai-client-chat - - - org.springframework.ai - spring-ai-starter-mcp-client - - - - com.networknt - json-schema-validator - 3.0.6 - - - org.springframework.ai - spring-ai-vector-store - - - co.elastic.apm - apm-agent-common - ${project.version} - test-jar - test - - - - - ${project.artifactId} - - - org.springframework.boot - spring-boot-maven-plugin - ${version.spring-boot} - - co.elastic.apm.springai.testapp.SpringAiTestApplication - - - - - repackage - - - - - - - diff --git a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/ChatController.java b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/ChatController.java deleted file mode 100644 index 35fbf93120..0000000000 --- a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/ChatController.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package co.elastic.apm.springai.testapp; - -import org.springframework.ai.chat.model.ChatModel; -import org.springframework.ai.chat.model.ChatResponse; -import org.springframework.ai.chat.prompt.ChatOptions; -import org.springframework.ai.chat.prompt.Prompt; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.server.ResponseStatusException; -import reactor.core.publisher.Flux; - -@RestController -@RequestMapping -public class ChatController { - - private final ChatModel chatModel; - - public ChatController(ChatModel chatModel) { - this.chatModel = chatModel; - } - - @GetMapping(value = "/health", produces = MediaType.TEXT_PLAIN_VALUE) - public String health() { - return "ok"; - } - - @PostMapping(value = "/chat", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - public ChatReply chat(@RequestBody ChatRequest request) { - ChatResponse response = chatModel.call(toPrompt(request)); - return new ChatReply(response.getResult().getOutput().getText()); - } - - @PostMapping(value = "/chat/stream", consumes = MediaType.APPLICATION_JSON_VALUE, - produces = MediaType.TEXT_EVENT_STREAM_VALUE) - public Flux stream(@RequestBody ChatRequest request) { - return chatModel.stream(toPrompt(request)) - .mapNotNull(response -> { - if (response.getResult() == null || response.getResult().getOutput() == null) { - return null; - } - String text = response.getResult().getOutput().getText(); - return text == null || text.isEmpty() ? null : text; - }); - } - - private static Prompt toPrompt(ChatRequest request) { - if (request == null || request.prompt() == null || request.prompt().isBlank()) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "prompt must not be blank"); - } - String model = request.model() == null || request.model().isBlank() - ? TestDataConfiguration.DeterministicChatModel.MODEL_NAME - : request.model(); - return new Prompt(request.prompt(), ChatOptions.builder().model(model).build()); - } - - public record ChatRequest(String prompt, String model) { - } - - public record ChatReply(String content) { - } -} diff --git a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/EmbeddingVectorController.java b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/EmbeddingVectorController.java deleted file mode 100644 index e076a7333e..0000000000 --- a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/EmbeddingVectorController.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package co.elastic.apm.springai.testapp; - -import org.springframework.ai.document.Document; -import org.springframework.ai.embedding.Embedding; -import org.springframework.ai.embedding.EmbeddingModel; -import org.springframework.ai.embedding.EmbeddingOptions; -import org.springframework.ai.embedding.EmbeddingRequest; -import org.springframework.ai.embedding.EmbeddingResponse; -import org.springframework.ai.vectorstore.SearchRequest; -import org.springframework.ai.vectorstore.VectorStore; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.server.ResponseStatusException; - -import java.util.List; -import java.util.Map; - -@RestController -@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) -public class EmbeddingVectorController { - - private final EmbeddingModel embeddingModel; - private final VectorStore vectorStore; - - public EmbeddingVectorController(EmbeddingModel embeddingModel, VectorStore vectorStore) { - this.embeddingModel = embeddingModel; - this.vectorStore = vectorStore; - } - - @PostMapping(value = "/embeddings", consumes = MediaType.APPLICATION_JSON_VALUE) - public EmbeddingReply embed(@RequestBody EmbeddingRequestBody request) { - if (request == null || request.inputs() == null || request.inputs().isEmpty()) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "inputs must not be empty"); - } - int dimensions = request.dimensions() == null - ? TestDataConfiguration.DeterministicEmbeddingModel.DEFAULT_DIMENSIONS - : request.dimensions(); - String model = request.model() == null || request.model().isBlank() - ? TestDataConfiguration.DeterministicEmbeddingModel.MODEL_NAME - : request.model(); - EmbeddingOptions options = EmbeddingOptions.builder().model(model).dimensions(dimensions).build(); - EmbeddingResponse response = embeddingModel.call(new EmbeddingRequest(request.inputs(), options)); - List vectors = response.getResults().stream().map(Embedding::getOutput).toList(); - return new EmbeddingReply(model, dimensions, vectors); - } - - @PostMapping(value = "/vector-store/documents", consumes = MediaType.APPLICATION_JSON_VALUE) - public DocumentMutationReply addDocuments(@RequestBody AddDocumentsRequest request) { - if (request == null || request.documents() == null || request.documents().isEmpty()) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "documents must not be empty"); - } - List documents = request.documents().stream() - .map(input -> Document.builder().id(input.id()).text(input.text()) - .metadata(input.metadata() == null ? Map.of() : input.metadata()).build()) - .toList(); - vectorStore.add(documents); - return new DocumentMutationReply(documents.size()); - } - - @DeleteMapping(value = "/vector-store/documents", consumes = MediaType.APPLICATION_JSON_VALUE) - public DocumentMutationReply deleteDocuments(@RequestBody DeleteDocumentsRequest request) { - if (request == null || request.ids() == null || request.ids().isEmpty()) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "ids must not be empty"); - } - vectorStore.delete(request.ids()); - return new DocumentMutationReply(request.ids().size()); - } - - @PostMapping(value = "/vector-store/search", consumes = MediaType.APPLICATION_JSON_VALUE) - public SearchReply search(@RequestBody VectorSearchRequest request) { - if (request == null || request.query() == null || request.query().isBlank()) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "query must not be blank"); - } - SearchRequest.Builder builder = SearchRequest.builder() - .query(request.query()) - .topK(request.topK() == null ? SearchRequest.DEFAULT_TOP_K : request.topK()) - .similarityThreshold(request.similarityThreshold() == null - ? SearchRequest.SIMILARITY_THRESHOLD_ACCEPT_ALL - : request.similarityThreshold()); - if (request.filter() != null && !request.filter().isBlank()) { - builder.filterExpression(request.filter()); - } - SearchRequest searchRequest = builder.build(); - List documents = vectorStore.similaritySearch(searchRequest).stream().map(Document::getText).toList(); - return new SearchReply(documents); - } - - public record EmbeddingRequestBody(List inputs, String model, Integer dimensions) { - } - - public record EmbeddingReply(String model, int dimensions, List embeddings) { - } - - public record DocumentInput(String id, String text, Map metadata) { - } - - public record AddDocumentsRequest(List documents) { - } - - public record DeleteDocumentsRequest(List ids) { - } - - public record DocumentMutationReply(int count) { - } - - public record VectorSearchRequest(String query, Integer topK, Double similarityThreshold, String filter) { - } - - public record SearchReply(List documents) { - } -} diff --git a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/McpToolController.java b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/McpToolController.java deleted file mode 100644 index f75e32d6c0..0000000000 --- a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/McpToolController.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package co.elastic.apm.springai.testapp; - -import org.springframework.ai.chat.messages.SystemMessage; -import org.springframework.ai.chat.messages.UserMessage; -import org.springframework.ai.chat.model.ChatModel; -import org.springframework.ai.chat.model.ChatResponse; -import org.springframework.ai.chat.prompt.Prompt; -import org.springframework.ai.mcp.SyncMcpToolCallbackProvider; -import org.springframework.ai.model.tool.ToolCallingChatOptions; -import org.springframework.ai.model.tool.ToolCallingManager; -import org.springframework.ai.model.tool.ToolExecutionResult; -import org.springframework.ai.tool.ToolCallback; -import org.springframework.ai.tool.definition.ToolDefinition; -import org.springframework.ai.tool.metadata.ToolMetadata; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.server.ResponseStatusException; - -import java.util.Arrays; -import java.util.List; - -@RestController -@RequestMapping("/mcp") -public class McpToolController { - - private final ChatModel chatModel; - private final ToolCallback echoTool; - private final ToolCallingManager toolCallingManager; - - public McpToolController(ChatModel chatModel, SyncMcpToolCallbackProvider mcpTools) { - this.chatModel = chatModel; - ToolCallback[] tracedMcpTools = Arrays.stream(mcpTools.getToolCallbacks()) - .map(McpTracingToolCallback::new) - .toArray(ToolCallback[]::new); - echoTool = Arrays.stream(tracedMcpTools) - .filter(callback -> callback.getToolDefinition().name().endsWith("echo")) - .findFirst() - .orElseThrow(() -> new IllegalStateException("MCP echo tool was not discovered")); - if (chatModel instanceof TestDataConfiguration.DeterministicChatModel deterministicChatModel) { - deterministicChatModel.setToolName(echoTool.getToolDefinition().name()); - } - toolCallingManager = ToolCallingManager.builder().build(); - } - - @PostMapping(value = "/tools/echo", consumes = MediaType.APPLICATION_JSON_VALUE, - produces = MediaType.APPLICATION_JSON_VALUE) - public McpToolReply invokeEchoTool(@RequestBody String arguments) { - if (arguments == null || arguments.isBlank()) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "tool arguments must not be blank"); - } - return new McpToolReply(echoTool.getToolDefinition().name(), echoTool.call(arguments)); - } - - @PostMapping(value = "/chat", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - public McpChatReply chatWithEchoTool(@RequestBody McpChatRequest request) { - if (request == null || request.message() == null || request.message().isBlank()) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "message must not be blank"); - } - ToolCallingChatOptions options = ToolCallingChatOptions.builder() - .model("tool-test") - .toolCallbacks(echoTool) - .build(); - Prompt initialPrompt = new Prompt(List.of( - new SystemMessage("Call the echo tool exactly once and use its result in the answer."), - new UserMessage("Call the echo tool with this exact message and report its result: " - + request.message().trim())), options); - ChatResponse toolCallResponse = chatModel.call(initialPrompt); - ToolExecutionResult toolResult = toolCallingManager.executeToolCalls(initialPrompt, toolCallResponse); - ChatResponse finalResponse = chatModel.call(new Prompt(toolResult.conversationHistory(), options)); - return new McpChatReply(finalResponse.getResult().getOutput().getText()); - } - - public record McpChatRequest(String message) { - } - - public record McpChatReply(String content) { - } - - public record McpToolReply(String tool, String result) { - } - - private static final class McpTracingToolCallback implements ToolCallback { - - private final ToolCallback delegate; - - private McpTracingToolCallback(ToolCallback delegate) { - this.delegate = delegate; - } - - @Override - public ToolDefinition getToolDefinition() { - return delegate.getToolDefinition(); - } - - @Override - public ToolMetadata getToolMetadata() { - return delegate.getToolMetadata(); - } - - @Override - public String call(String toolInput) { - return delegate.call(toolInput, null); - } - } -} diff --git a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/SpringAiTestApplication.java b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/SpringAiTestApplication.java deleted file mode 100644 index 6c2e0d581a..0000000000 --- a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/SpringAiTestApplication.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package co.elastic.apm.springai.testapp; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class SpringAiTestApplication { - - public static void main(String[] args) { - SpringApplication.run(SpringAiTestApplication.class, args); - } -} diff --git a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/TestDataConfiguration.java b/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/TestDataConfiguration.java deleted file mode 100644 index 21f07111a0..0000000000 --- a/integration-tests/spring-ai-test/src/main/java/co/elastic/apm/springai/testapp/TestDataConfiguration.java +++ /dev/null @@ -1,238 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package co.elastic.apm.springai.testapp; - -import org.springframework.ai.chat.messages.AssistantMessage; -import org.springframework.ai.chat.messages.ToolResponseMessage; -import org.springframework.ai.chat.client.ChatClient; -import org.springframework.ai.chat.metadata.ChatGenerationMetadata; -import org.springframework.ai.chat.metadata.ChatResponseMetadata; -import org.springframework.ai.chat.metadata.DefaultUsage; -import org.springframework.ai.chat.model.ChatModel; -import org.springframework.ai.chat.model.ChatResponse; -import org.springframework.ai.chat.model.Generation; -import org.springframework.ai.chat.prompt.Prompt; -import org.springframework.ai.document.Document; -import org.springframework.ai.embedding.Embedding; -import org.springframework.ai.embedding.EmbeddingModel; -import org.springframework.ai.embedding.EmbeddingRequest; -import org.springframework.ai.embedding.EmbeddingResponse; -import org.springframework.ai.embedding.EmbeddingResponseMetadata; -import org.springframework.ai.tool.ToolCallback; -import org.springframework.ai.tool.definition.ToolDefinition; -import org.springframework.ai.vectorstore.SimpleVectorStore; -import org.springframework.ai.vectorstore.VectorStore; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import reactor.core.publisher.Flux; - -import java.time.Duration; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Locale; - -@Configuration -public class TestDataConfiguration { - - @Bean - ChatModel deterministicChatModel() { - return new DeterministicChatModel(); - } - - @Bean - ChatClient.Builder chatClientBuilder(ChatModel chatModel) { - return ChatClient.builder(chatModel); - } - - @Bean - EmbeddingModel deterministicEmbeddingModel() { - return new DeterministicEmbeddingModel(); - } - - @Bean - VectorStore testVectorStore(EmbeddingModel embeddingModel) { - return SimpleVectorStore.builder(embeddingModel).build(); - } - - @Bean - ToolCallback echoToolCallback() { - return new EchoToolCallback(); - } - - static final class DeterministicChatModel implements ChatModel { - - static final String MODEL_NAME = "deterministic-chat"; - private volatile String toolName = "echo"; - - @Override - public ChatResponse call(Prompt prompt) { - OptionalToolResponse toolResponse = findToolResponse(prompt); - if (toolResponse.present()) { - return response("I called the echo tool. Tool result: " + toolResponse.response()); - } - String userText = prompt.getUserMessage().getText(); - if (userText.startsWith("Call the echo tool with this exact message")) { - String requestedMessage = userText.substring(userText.lastIndexOf(':') + 1).trim(); - String arguments = "{\"message\":\"" + jsonEscape(requestedMessage) + "\",\"repeat\":1}"; - AssistantMessage output = AssistantMessage.builder() - .content("") - .toolCalls(List.of(new AssistantMessage.ToolCall("integration-call-1", "function", toolName, arguments))) - .build(); - return new ChatResponse(Collections.singletonList(new Generation(output)), responseMetadata()); - } - String promptText = prompt.getInstructions().get(0).getText(); - return response("Echo: " + promptText); - } - - void setToolName(String toolName) { - this.toolName = toolName; - } - - @Override - public Flux stream(Prompt prompt) { - String promptText = prompt.getInstructions().get(0).getText(); - ChatResponse finalResponse = response("Echo: " + promptText); - return Flux.just( - contentChunk("Echo: "), - contentChunk(promptText), - new ChatResponse(Collections.emptyList(), finalResponse.getMetadata()) - ) - .delayElements(Duration.ofMillis(20)); - } - - private static ChatResponse contentChunk(String content) { - Generation generation = new Generation(new AssistantMessage(content), - ChatGenerationMetadata.builder().finishReason("stop").build()); - return new ChatResponse(Collections.singletonList(generation)); - } - - private static ChatResponse response(String content) { - Generation generation = new Generation(new AssistantMessage(content), - ChatGenerationMetadata.builder().finishReason("stop").build()); - return new ChatResponse(Collections.singletonList(generation), responseMetadata()); - } - - private static ChatResponseMetadata responseMetadata() { - return ChatResponseMetadata.builder() - .id("deterministic-response") - .model(MODEL_NAME) - .usage(new DefaultUsage(3, 2, 5)) - .build(); - } - - private static OptionalToolResponse findToolResponse(Prompt prompt) { - return prompt.getInstructions().stream() - .filter(ToolResponseMessage.class::isInstance) - .map(ToolResponseMessage.class::cast) - .flatMap(message -> message.getResponses().stream()) - .findFirst() - .map(response -> new OptionalToolResponse(true, response.responseData())) - .orElseGet(() -> new OptionalToolResponse(false, "")); - } - - private static String jsonEscape(String value) { - return value.replace("\\", "\\\\").replace("\"", "\\\""); - } - - private record OptionalToolResponse(boolean present, String response) { - } - } - - static final class DeterministicEmbeddingModel implements EmbeddingModel { - - static final String MODEL_NAME = "deterministic-embedding"; - static final int DEFAULT_DIMENSIONS = 8; - - @Override - public EmbeddingResponse call(EmbeddingRequest request) { - int dimensions = request.getOptions() == null || request.getOptions().getDimensions() == null - ? DEFAULT_DIMENSIONS - : request.getOptions().getDimensions(); - List embeddings = new ArrayList<>(); - int tokenCount = 0; - for (int index = 0; index < request.getInstructions().size(); index++) { - String input = request.getInstructions().get(index); - embeddings.add(new Embedding(toVector(input, dimensions), index)); - tokenCount += tokens(input).length; - } - String model = request.getOptions() == null || request.getOptions().getModel() == null - ? MODEL_NAME - : request.getOptions().getModel(); - return new EmbeddingResponse(embeddings, - new EmbeddingResponseMetadata(model, new DefaultUsage(tokenCount, 0, tokenCount))); - } - - @Override - public float[] embed(Document document) { - return embed(getEmbeddingContent(document)); - } - - @Override - public int dimensions() { - return DEFAULT_DIMENSIONS; - } - - private static float[] toVector(String input, int dimensions) { - float[] vector = new float[dimensions]; - for (String token : tokens(input)) { - int hash = token.hashCode(); - vector[Math.floorMod(hash, dimensions)] += (hash & 1) == 0 ? 1.0f : -1.0f; - } - double magnitude = 0; - for (float value : vector) { - magnitude += value * value; - } - if (magnitude == 0) { - vector[0] = 1.0f; - return vector; - } - float divisor = (float) Math.sqrt(magnitude); - for (int index = 0; index < vector.length; index++) { - vector[index] /= divisor; - } - return vector; - } - - private static String[] tokens(String input) { - if (input == null || input.isBlank()) { - return new String[0]; - } - String normalized = input.toLowerCase(Locale.ROOT).replaceAll("[^a-z0-9]+", " ").trim(); - return normalized.isEmpty() ? new String[0] : normalized.split("\\s+"); - } - } - - static final class EchoToolCallback implements ToolCallback { - - @Override - public ToolDefinition getToolDefinition() { - return ToolDefinition.builder() - .name("echo") - .description("Returns the supplied message") - .inputSchema("{\"type\":\"object\",\"properties\":{\"message\":{\"type\":\"string\"}}}") - .build(); - } - - @Override - public String call(String toolInput) { - return "Echo result: " + toolInput; - } - } -} diff --git a/integration-tests/spring-ai-test/src/main/resources/application.properties b/integration-tests/spring-ai-test/src/main/resources/application.properties deleted file mode 100644 index 87be227460..0000000000 --- a/integration-tests/spring-ai-test/src/main/resources/application.properties +++ /dev/null @@ -1,4 +0,0 @@ -spring.application.name=spring-ai-integration-test -spring.main.banner-mode=off -spring.ai.mcp.client.stdio.connections.test-tools.command=${MCP_SERVER_COMMAND:python3} -spring.ai.mcp.client.stdio.connections.test-tools.args[0]=${MCP_SERVER_SCRIPT:mcp-test-server.py} diff --git a/integration-tests/spring-ai-test/src/main/resources/mcp-test-server.py b/integration-tests/spring-ai-test/src/main/resources/mcp-test-server.py deleted file mode 100644 index 402ae90091..0000000000 --- a/integration-tests/spring-ai-test/src/main/resources/mcp-test-server.py +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env python3 -# Licensed to Elasticsearch B.V. under one or more contributor license agreements. -# See the NOTICE file distributed with this work for additional information -# regarding copyright ownership. Elasticsearch B.V. licenses this file to you -# under the Apache License, Version 2.0 (the "License"); you may not use this -# file except in compliance with the License. You may obtain a copy of the -# License at http://www.apache.org/licenses/LICENSE-2.0 -"""Minimal deterministic MCP stdio server for Spring AI integration tests.""" - -import json -import sys - - -TOOL = { - "name": "echo", - "description": "Echo a test message, optionally repeated.", - "inputSchema": { - "type": "object", - "properties": { - "message": {"type": "string"}, - "repeat": {"type": "integer", "minimum": 1, "maximum": 10, "default": 1}, - }, - "required": ["message"], - "additionalProperties": False, - }, -} - - -def send(message): - sys.stdout.write(json.dumps(message, separators=(",", ":")) + "\n") - sys.stdout.flush() - - -def result(request_id, payload): - send({"jsonrpc": "2.0", "id": request_id, "result": payload}) - - -def error(request_id, code, message): - send({"jsonrpc": "2.0", "id": request_id, "error": {"code": code, "message": message}}) - - -def handle(request): - method = request.get("method") - request_id = request.get("id") - params = request.get("params") or {} - if method == "initialize": - result( - request_id, - { - "protocolVersion": params.get("protocolVersion", "2025-11-25"), - "capabilities": {"tools": {"listChanged": False}}, - "serverInfo": {"name": "spring-ai-integration-tools", "version": "1.0.0"}, - }, - ) - elif method == "ping": - result(request_id, {}) - elif method == "tools/list": - result(request_id, {"tools": [TOOL]}) - elif method == "tools/call": - arguments = params.get("arguments") or {} - message = arguments.get("message") - repeat = arguments.get("repeat", 1) - if params.get("name") != "echo" or not isinstance(message, str): - error(request_id, -32602, "invalid echo request") - return - result( - request_id, - { - "content": [{"type": "text", "text": " ".join([message] * repeat)}], - "isError": False, - }, - ) - elif request_id is not None: - error(request_id, -32601, "method not found") - - -def main(): - for line in sys.stdin: - try: - handle(json.loads(line)) - except Exception as exception: - print(f"MCP test server error: {exception}", file=sys.stderr, flush=True) - - -if __name__ == "__main__": - main() diff --git a/integration-tests/spring-ai-test/src/test/java/co/elastic/apm/springai/SpringAiInstrumentationIT.java b/integration-tests/spring-ai-test/src/test/java/co/elastic/apm/springai/SpringAiInstrumentationIT.java deleted file mode 100644 index 6c4587c665..0000000000 --- a/integration-tests/spring-ai-test/src/test/java/co/elastic/apm/springai/SpringAiInstrumentationIT.java +++ /dev/null @@ -1,482 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package co.elastic.apm.springai; - -import co.elastic.apm.agent.test.AgentFileAccessor; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpServer; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.InetSocketAddress; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.time.Duration; -import java.util.List; -import java.util.Locale; -import java.util.Optional; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.function.Predicate; -import java.util.zip.GZIPInputStream; -import java.util.zip.InflaterInputStream; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.awaitility.Awaitility.await; - -class SpringAiInstrumentationIT { - - private static final ObjectMapper objectMapper = new ObjectMapper(); - private static final HttpClient httpClient = HttpClient.newBuilder() - .connectTimeout(Duration.ofSeconds(2)) - .build(); - - private static FakeApmServer apmServer; - private static Process application; - private static URI applicationUri; - private static Path applicationLog; - - @BeforeAll - static void startApplication() throws Exception { - apmServer = new FakeApmServer(); - int applicationPort = availablePort(); - applicationUri = URI.create("http://127.0.0.1:" + applicationPort); - applicationLog = Path.of("target", "spring-ai-test-app.log").toAbsolutePath(); - Files.deleteIfExists(applicationLog); - - Path agentJar = AgentFileAccessor.getPathToJavaagent(); - Path applicationJar = Path.of("target", "spring-ai-test.jar").toAbsolutePath(); - Path mcpServerScript = Path.of("target", "classes", "mcp-test-server.py").toAbsolutePath(); - assertThat(mcpServerScript).exists(); - List command = List.of( - javaBinary(), - "-javaagent:" + agentJar, - "-Delastic.apm.server_url=" + apmServer.getUrl(), - "-Delastic.apm.service_name=spring-ai-integration-test", - "-Delastic.apm.application_packages=co.elastic.apm.springai.testapp", - "-Delastic.apm.enable_experimental_instrumentations=true", - "-Delastic.apm.capture_ai_content=true", - "-Delastic.apm.central_config=false", - "-Delastic.apm.report_sync=true", - "-Delastic.apm.metrics_interval=1s", - "-Delastic.apm.transaction_ignore_urls=/health", - "-Delastic.apm.span_compression_enabled=false", - "-DMCP_SERVER_COMMAND=python3", - "-DMCP_SERVER_SCRIPT=" + mcpServerScript, - "-jar", - applicationJar.toString(), - "--server.port=" + applicationPort - ); - application = new ProcessBuilder(command) - .redirectErrorStream(true) - .redirectOutput(applicationLog.toFile()) - .start(); - waitForApplication(); - } - - @AfterAll - static void stopApplication() throws Exception { - if (application != null && application.isAlive()) { - application.destroy(); - if (!application.waitFor(10, java.util.concurrent.TimeUnit.SECONDS)) { - application.destroyForcibly(); - } - } - if (apmServer != null) { - apmServer.close(); - } - } - - @Test - void capturesSpringAiOperationsFromPackagedApplication() throws Exception { - assertSynchronousChat(); - assertStreamingChat(); - assertEmbedding(); - assertVectorStore(); - assertMcpToolPaths(); - } - - private static void assertSynchronousChat() throws Exception { - apmServer.clear(); - HttpResponse response = post("/chat", "{\"prompt\":\"sync integration prompt\",\"model\":\"chat-test\"}"); - assertThat(response.statusCode()).isEqualTo(200); - assertThat(response.body()).contains("Echo: sync integration prompt"); - - Trace trace = awaitTrace("ChatController#chat", span -> "chat chat-test".equals(span.path("name").asText())); - assertSpanBelongsToTransaction(trace); - JsonNode attributes = otelAttributes(trace.span()); - assertThat(attributes.path("gen_ai.operation.name").asText()).isEqualTo("chat"); - assertThat(attributes.path("gen_ai.request.stream").asBoolean()).isFalse(); - assertThat(attributes.path("gen_ai.input.messages").asText()).contains("sync integration prompt"); - assertThat(attributes.path("gen_ai.output.messages").asText()).contains("Echo: sync integration prompt"); - assertThat(attributes.path("gen_ai.usage.total_tokens").asInt()).isEqualTo(5); - } - - private static void assertStreamingChat() throws Exception { - apmServer.clear(); - HttpResponse response = post("/chat/stream", - "{\"prompt\":\"stream integration prompt\",\"model\":\"stream-test\"}"); - assertThat(response.statusCode()).isEqualTo(200); - assertThat(response.body()).contains("Echo:", "stream integration prompt"); - - Trace trace = awaitTrace("ChatController#stream", span -> "chat stream-test".equals(span.path("name").asText())); - assertSpanBelongsToTransaction(trace); - JsonNode attributes = otelAttributes(trace.span()); - assertThat(attributes.path("gen_ai.request.stream").asBoolean()).isTrue(); - assertThat(attributes.path("gen_ai.response.time_to_first_chunk").isNumber()).isTrue(); - assertThat(attributes.path("gen_ai.response.time_to_first_chunk").asDouble()).isGreaterThanOrEqualTo(0); - assertThat(attributes.path("gen_ai.output.messages").asText()).contains("Echo: stream integration prompt"); - assertThat(attributes.path("gen_ai.usage.total_tokens").asInt()).isEqualTo(5); - - JsonNode histogram = awaitMetric("gen_ai.client.inference.time_to_first_chunk"); - assertThat(histogram.path("values")).isNotEmpty(); - assertThat(histogram.path("counts")).isNotEmpty(); - int observationCount = 0; - for (JsonNode count : histogram.path("counts")) { - observationCount += count.asInt(); - } - assertThat(observationCount).isGreaterThanOrEqualTo(1); - } - - private static void assertEmbedding() throws Exception { - apmServer.clear(); - HttpResponse response = post("/embeddings", - "{\"inputs\":[\"integration embedding input\"],\"model\":\"embedding-test\",\"dimensions\":8}"); - assertThat(response.statusCode()).isEqualTo(200); - - Trace trace = awaitTrace("EmbeddingVectorController#embed", - span -> "embedding embedding-test".equals(span.path("name").asText())); - assertSpanBelongsToTransaction(trace); - JsonNode attributes = otelAttributes(trace.span()); - assertThat(attributes.path("gen_ai.operation.name").asText()).isEqualTo("embedding"); - assertThat(attributes.path("spring.ai.embedding.request.inputs").asText()).contains("integration embedding input"); - assertThat(attributes.path("gen_ai.request.embedding.dimensions").asInt()).isEqualTo(8); - } - - private static void assertVectorStore() throws Exception { - apmServer.clear(); - HttpResponse addResponse = post("/vector-store/documents", - "{\"documents\":[" - + "{\"id\":\"observability\",\"text\":\"distributed tracing integration document\"," - + "\"metadata\":{\"category\":\"observability\"}}," - + "{\"id\":\"gardening\",\"text\":\"growing tomatoes in a sunny garden\"," - + "\"metadata\":{\"category\":\"gardening\"}}]}"); - assertThat(addResponse.statusCode()).isEqualTo(200); - - Trace addTrace = awaitTrace("EmbeddingVectorController#addDocuments", - span -> "add".equals(otelAttributes(span).path("db.operation.name").asText())); - assertSpanBelongsToTransaction(addTrace); - assertThat(otelAttributes(addTrace.span()).path("db.vector.documents").asText()) - .contains("distributed tracing integration document"); - - apmServer.clear(); - HttpResponse searchResponse = post("/vector-store/search", - "{\"query\":\"distributed tracing\",\"topK\":3,\"similarityThreshold\":0.0," - + "\"filter\":\"category == 'observability'\"}"); - assertThat(searchResponse.statusCode()).isEqualTo(200); - assertThat(searchResponse.body()).contains("distributed tracing integration document"); - - Trace searchTrace = awaitTrace("EmbeddingVectorController#search", - span -> "query".equals(otelAttributes(span).path("db.operation.name").asText())); - assertSpanBelongsToTransaction(searchTrace); - JsonNode attributes = otelAttributes(searchTrace.span()); - assertThat(attributes.path("db.vector.query.content").asText()).isEqualTo("distributed tracing"); - assertThat(attributes.path("db.vector.query.filter").asText()).contains("observability"); - assertThat(attributes.path("db.vector.query.response.documents").asText()) - .contains("distributed tracing integration document"); - - apmServer.clear(); - HttpResponse deleteResponse = delete("/vector-store/documents", "{\"ids\":[\"observability\"]}"); - assertThat(deleteResponse.statusCode()).isEqualTo(200); - Trace deleteTrace = awaitTrace("EmbeddingVectorController#deleteDocuments", - span -> "delete".equals(otelAttributes(span).path("db.operation.name").asText())); - assertSpanBelongsToTransaction(deleteTrace); - - apmServer.clear(); - HttpResponse afterDelete = post("/vector-store/search", - "{\"query\":\"distributed tracing\",\"topK\":3,\"similarityThreshold\":0.0}"); - assertThat(afterDelete.statusCode()).isEqualTo(200); - assertThat(afterDelete.body()).doesNotContain("distributed tracing integration document"); - } - - private static void assertMcpToolPaths() throws Exception { - apmServer.clear(); - HttpResponse response = post("/mcp/tools/echo", - "{\"message\":\"Hello from direct MCP integration test\",\"repeat\":1}"); - assertThat(response.statusCode()).isEqualTo(200); - assertThat(response.body()).contains("Hello from direct MCP integration test"); - - Trace trace = awaitTrace("McpToolController#invokeEchoTool", - span -> span.path("name").asText().startsWith("execute_tool ")); - assertSpanBelongsToTransaction(trace); - JsonNode attributes = otelAttributes(trace.span()); - assertThat(attributes.path("spring.ai.tool.definition.name").asText()).endsWith("echo"); - assertThat(attributes.path("spring.ai.tool.call.arguments").asText()) - .contains("Hello from direct MCP integration test"); - assertThat(attributes.path("spring.ai.tool.call.result").asText()) - .contains("Hello from direct MCP integration test"); - - apmServer.clear(); - HttpResponse chatResponse = post("/mcp/chat", "{\"message\":\"Hello from MCP chat integration test\"}"); - assertThat(chatResponse.statusCode()).isEqualTo(200); - assertThat(chatResponse.body()).contains("Hello from MCP chat integration test"); - - await().atMost(Duration.ofSeconds(15)) - .until(() -> findTransaction("McpToolController#chatWithEchoTool").isPresent()); - ObjectNode transaction = findTransaction("McpToolController#chatWithEchoTool").orElseThrow(); - String transactionId = transaction.path("id").asText(); - await().atMost(Duration.ofSeconds(15)).until(() -> spans(transactionId, - span -> "chat tool-test".equals(span.path("name").asText())).size() >= 2); - - List chatSpans = spans(transactionId, - span -> "chat tool-test".equals(span.path("name").asText())); - assertThat(chatSpans).anySatisfy(span -> - assertThat(otelAttributes(span).path("gen_ai.output.messages").asText()) - .contains("tool_call", "Hello from MCP chat integration test")); - assertThat(chatSpans).anySatisfy(span -> { - assertThat(otelAttributes(span).path("gen_ai.input.messages").asText()) - .contains("tool_call_response", "Hello from MCP chat integration test"); - assertThat(otelAttributes(span).path("gen_ai.output.messages").asText()) - .contains("I called the echo tool", "Hello from MCP chat integration test"); - }); - - await().atMost(Duration.ofSeconds(15)).until(() -> spans(transactionId, - span -> span.path("name").asText().startsWith("execute_tool ")).size() == 1); - ObjectNode toolSpan = spans(transactionId, - span -> span.path("name").asText().startsWith("execute_tool ")).get(0); - assertThat(otelAttributes(toolSpan).path("spring.ai.tool.definition.name").asText()).endsWith("echo"); - assertThat(otelAttributes(toolSpan).path("spring.ai.tool.call.arguments").asText()) - .contains("Hello from MCP chat integration test"); - } - - private static void assertSpanBelongsToTransaction(Trace trace) { - assertThat(trace.span().path("transaction_id").asText()).isEqualTo(trace.transaction().path("id").asText()); - assertThat(trace.span().path("trace_id").asText()).isEqualTo(trace.transaction().path("trace_id").asText()); - assertThat(trace.span().path("parent_id").asText()).isEqualTo(trace.transaction().path("id").asText()); - assertThat(trace.transaction().path("span_count").path("started").asInt()).isGreaterThanOrEqualTo(1); - } - - private static JsonNode otelAttributes(ObjectNode span) { - return span.path("otel").path("attributes"); - } - - private static Trace awaitTrace(String transactionName, Predicate spanPredicate) { - await().atMost(Duration.ofSeconds(15)).until(() -> findTransaction(transactionName).isPresent()); - ObjectNode transaction = findTransaction(transactionName).orElseThrow(); - String transactionId = transaction.path("id").asText(); - await().atMost(Duration.ofSeconds(15)).until(() -> findSpan(transactionId, spanPredicate).isPresent()); - ObjectNode span = findSpan(transactionId, spanPredicate).orElseThrow(); - return new Trace(transaction, span); - } - - private static Optional findTransaction(String name) { - return apmServer.events("transaction").stream() - .filter(event -> name.equals(event.path("name").asText())) - .findFirst(); - } - - private static Optional findSpan(String transactionId, Predicate predicate) { - return apmServer.events("span").stream() - .filter(event -> transactionId.equals(event.path("transaction_id").asText())) - .filter(predicate) - .findFirst(); - } - - private static List spans(String transactionId, Predicate predicate) { - return apmServer.events("span").stream() - .filter(event -> transactionId.equals(event.path("transaction_id").asText())) - .filter(predicate) - .toList(); - } - - private static JsonNode awaitMetric(String name) { - await().atMost(Duration.ofSeconds(15)).until(() -> findMetric(name).isPresent()); - return findMetric(name).orElseThrow(); - } - - private static Optional findMetric(String name) { - return apmServer.events("metricset").stream() - .map(metricset -> metricset.path("samples").path(name)) - .filter(JsonNode::isObject) - .findFirst(); - } - - private static HttpResponse post(String path, String body) throws IOException, InterruptedException { - return request("POST", path, body); - } - - private static HttpResponse delete(String path, String body) throws IOException, InterruptedException { - return request("DELETE", path, body); - } - - private static HttpResponse request(String method, String path, String body) - throws IOException, InterruptedException { - HttpRequest request = HttpRequest.newBuilder(applicationUri.resolve(path)) - .header("Content-Type", "application/json") - .timeout(Duration.ofSeconds(15)) - .method(method, HttpRequest.BodyPublishers.ofString(body)) - .build(); - return httpClient.send(request, HttpResponse.BodyHandlers.ofString()); - } - - private static void waitForApplication() { - await().atMost(Duration.ofSeconds(30)).pollInterval(Duration.ofMillis(200)).until(() -> { - if (!application.isAlive()) { - throw new IllegalStateException("Spring AI test application stopped during startup:\n" + applicationLogs()); - } - try { - HttpRequest request = HttpRequest.newBuilder(applicationUri.resolve("/health")) - .timeout(Duration.ofSeconds(1)) - .GET() - .build(); - return httpClient.send(request, HttpResponse.BodyHandlers.ofString()).statusCode() == 200; - } catch (Exception ignored) { - return false; - } - }); - } - - private static String applicationLogs() { - try { - return Files.exists(applicationLog) ? Files.readString(applicationLog) : ""; - } catch (IOException e) { - return ""; - } - } - - private static int availablePort() throws IOException { - try (java.net.ServerSocket socket = new java.net.ServerSocket(0)) { - return socket.getLocalPort(); - } - } - - private static String javaBinary() { - String executable = System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win") ? "java.exe" : "java"; - return Path.of(System.getProperty("java.home"), "bin", executable).toString(); - } - - private record Trace(ObjectNode transaction, ObjectNode span) { - } - - private static final class FakeApmServer implements AutoCloseable { - - private final HttpServer server; - private final ExecutorService executor = Executors.newCachedThreadPool(); - private final List receivedEvents = new CopyOnWriteArrayList<>(); - - private FakeApmServer() throws IOException { - server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); - server.createContext("/", this::handle); - server.setExecutor(executor); - server.start(); - } - - private String getUrl() { - return "http://127.0.0.1:" + server.getAddress().getPort(); - } - - private void clear() { - receivedEvents.clear(); - } - - private List events(String type) { - return receivedEvents.stream() - .filter(event -> event.has(type)) - .map(event -> (ObjectNode) event.get(type)) - .toList(); - } - - private void handle(HttpExchange exchange) throws IOException { - try { - String path = exchange.getRequestURI().getPath(); - if ("GET".equals(exchange.getRequestMethod()) && "/".equals(path)) { - respond(exchange, 200, "{\"version\":\"8.17.0\"}"); - } else if ("POST".equals(exchange.getRequestMethod()) && "/intake/v2/events".equals(path)) { - readEvents(exchange); - respond(exchange, 202, ""); - } else if ("POST".equals(exchange.getRequestMethod()) && "/config/v1/agents".equals(path)) { - respond(exchange, 200, "{}"); - } else { - respond(exchange, 404, ""); - } - } finally { - exchange.close(); - } - } - - private void readEvents(HttpExchange exchange) throws IOException { - try (InputStream decoded = decode(exchange); - BufferedReader reader = new BufferedReader(new InputStreamReader(decoded, StandardCharsets.UTF_8))) { - String line; - while ((line = reader.readLine()) != null) { - if (!line.isBlank()) { - receivedEvents.add((ObjectNode) objectMapper.readTree(line)); - } - } - } - } - - private static InputStream decode(HttpExchange exchange) throws IOException { - InputStream body = exchange.getRequestBody(); - String encoding = exchange.getRequestHeaders().getFirst("Content-Encoding"); - if (encoding == null) { - return body; - } - if (encoding.toLowerCase(Locale.ROOT).contains("gzip")) { - return new GZIPInputStream(body); - } - if (encoding.toLowerCase(Locale.ROOT).contains("deflate")) { - return new InflaterInputStream(body); - } - return body; - } - - private static void respond(HttpExchange exchange, int status, String body) throws IOException { - byte[] bytes = body.getBytes(StandardCharsets.UTF_8); - exchange.getResponseHeaders().set("Content-Type", "application/json"); - exchange.sendResponseHeaders(status, bytes.length == 0 ? -1 : bytes.length); - if (bytes.length > 0) { - try (OutputStream output = exchange.getResponseBody()) { - output.write(bytes); - } - } - } - - @Override - public void close() { - server.stop(0); - executor.shutdownNow(); - } - } -} diff --git a/pom.xml b/pom.xml index d74735b7d0..2fbe7f9128 100644 --- a/pom.xml +++ b/pom.xml @@ -361,7 +361,7 @@ central true - apm-agent-common,apm-agent-cached-lookup-key,apm-agent-bootstrap,apm-agent-core,elastic-apm-agent-premain,apm-agent-plugins,apm-httpclient-core,apm-apache-httpclient,apm-apache-httpclient3-plugin,apm-apache-httpclient-common,apm-apache-httpclient4-plugin,apm-apache-httpclient5-plugin,apm-api-plugin,apm-asynchttpclient-plugin,apm-java-concurrent-plugin,apm-cassandra,apm-cassandra-core-plugin,apm-cassandra3-plugin,apm-cassandra4-plugin,apm-okhttp-plugin,apm-dubbo-plugin,apm-es-restclient-plugin,apm-es-restclient-plugin-common,apm-es-restclient-plugin-5_6,apm-es-restclient-plugin-6_4,apm-es-restclient-plugin-7_x,apm-es-restclient-plugin-8_x,apm-grails-plugin,apm-grpc,apm-grpc-plugin,apm-hibernate-search-plugin,apm-hibernate-search-plugin-common,apm-hibernate-search-plugin-5_x,apm-hibernate-search-plugin-6_x,apm-httpserver-core,apm-servlet-plugin,apm-javalin-plugin,apm-jaxrs-plugin,apm-jaxws-plugin,apm-jdbc-plugin,apm-jdk-httpclient-plugin,apm-jdk-httpserver-plugin,apm-redis-plugin,apm-redis-common,apm-jedis-plugin,apm-jedis-4-plugin,apm-jms-plugin,apm-jms-plugin-base,apm-jms-javax,apm-jms-jakarta,apm-jmx-plugin,apm-jsf-plugin,apm-kafka-plugin,apm-kafka-base-plugin,apm-kafka-headers-plugin,apm-kafka-spring-plugin,apm-lettuce-plugin,apm-logging-plugin,apm-logging-plugin-common,apm-log4j1-plugin,apm-log4j2-plugin,apm-opentelemetry,apm-opentelemetry-embedded-metrics-sdk,apm-opentelemetry-metrics-bridge-parent,apm-opentelemetry-metrics-bridge-common,apm-opentelemetry-metrics-bridge-v1_14,apm-opentelemetry-metrics-bridge-latest,apm-opentelemetry-metricsdk-plugin,apm-opentelemetry-plugin,apm-slf4j-plugin,apm-logback-plugin,apm-logback-plugin-impl,apm-jboss-logging-plugin,apm-jul-plugin,apm-tomcat-logging-plugin,apm-micrometer-plugin,apm-mongodb,apm-mongodb-common,apm-mongodb3-plugin,apm-mongodb4-plugin,apm-opentracing-plugin,apm-process-plugin,apm-profiling-plugin,apm-quartz,apm-quartz-common,apm-quartz-plugin-1,apm-quartz-plugin-2,apm-rabbitmq,apm-rabbitmq-plugin,apm-rabbitmq-spring5,apm-reactor-plugin,apm-redisson-plugin,apm-scala-concurrent-plugin,apm-scheduled-annotation-plugin,apm-sparkjava-plugin,apm-urlconnection-plugin,apm-spring-ai-plugin,apm-spring-resttemplate,apm-spring-resttemplate-plugin,apm-spring-webflux,apm-spring-webflux-common-spring5,apm-spring-webflux-common,apm-spring-webclient-plugin,apm-spring-webflux-testapp,apm-spring-webflux-testapp-spring7,apm-spring-webflux-spring5,apm-spring-webflux-plugin,apm-spring-webflux-plugin-spring7,apm-spring-webmvc,apm-spring-webmvc-spring5,apm-spring-webmvc-plugin,apm-struts-plugin,apm-vertx,apm-vertx-common,apm-vertx3-plugin,apm-vertx4-plugin,apm-awslambda-plugin,apm-jakarta-websocket-plugin,apm-ecs-logging-plugin,apm-aws-sdk,apm-aws-sdk-common,apm-aws-sdk-1-plugin,apm-aws-sdk-2-plugin,apm-finagle-httpclient-plugin,apm-java-ldap-plugin,apm-agent-builds,apm-agent,apm-agent-lambda-layer,apm-agent-java8,apm-agent-benchmarks,apm-spring-resttemplate-test,apm-spring-restclient-test,apm-logback-plugin-legacy-tests,apm-log4j2-plugin-tests,apm-jms-spring-plugin,apm-jedis-2-tests,apm-jedis-3-tests,apm-jedis-5-tests,apm-lettuce-3-tests,apm-grpc-test-1.6.1,apm-grpc-test-latest,apm-rabbitmq-test-3,apm-rabbitmq-test-4,apm-rabbitmq-test-spring6,apm-okhttp-test,apm-opentelemetry-test,apm-vertx3-test-latest,apm-servlet-jakarta-test,apm-jaxws-plugin-jakartaee-test,apm-jaxrs-plugin-jakartaee-test,apm-scheduled-annotation-plugin-jakartaee-test,integration-tests,simple-webapp,jakartaee-simple-webapp,jsf-app,jsf-app-dependent,jsf-app-standalone,jakartaee-jsf-app,jakartaee-jsf-app-dependent,jakartaee-jsf-app-standalone,cdi-app,cdi-app-dependent,cdi-app-standalone,soap-test,external-plugin-test,plugin-instrumentation-target,external-plugin-app,external-plugin-jakarta-app,external-plugin,application-server-integration-tests,spring-boot-1-5,spring-boot-2,spring-boot-2-base,spring-boot-2-jetty,spring-boot-2-tomcat,spring-boot-2-undertow,cdi-jakartaee-app,cdi-jakartaee-app-dependent,cdi-jakartaee-app-standalone,quarkus,quarkus-jaxrs-base,quarkus-jaxrs-undertow,external-plugin-otel-test,external-plugin-otel-test-app,external-plugin-otel-test-plugin1,external-plugin-otel-test-plugin2,runtime-attach,runtime-attach-app,runtime-attach-test,main-app-test,spring-boot-3,spring-boot-3-jetty,spring-boot-3-tomcat,spring-boot-3-undertow,aws-lambda-test,spring-ai-test,spring-boot-4,spring-boot-4-base,spring-boot-4-jetty,spring-boot-4-tomcat,apm-spring-webmvc-spring7-test + apm-agent-common,apm-agent-cached-lookup-key,apm-agent-bootstrap,apm-agent-core,elastic-apm-agent-premain,apm-agent-plugins,apm-httpclient-core,apm-apache-httpclient,apm-apache-httpclient3-plugin,apm-apache-httpclient-common,apm-apache-httpclient4-plugin,apm-apache-httpclient5-plugin,apm-api-plugin,apm-asynchttpclient-plugin,apm-java-concurrent-plugin,apm-cassandra,apm-cassandra-core-plugin,apm-cassandra3-plugin,apm-cassandra4-plugin,apm-okhttp-plugin,apm-dubbo-plugin,apm-es-restclient-plugin,apm-es-restclient-plugin-common,apm-es-restclient-plugin-5_6,apm-es-restclient-plugin-6_4,apm-es-restclient-plugin-7_x,apm-es-restclient-plugin-8_x,apm-grails-plugin,apm-grpc,apm-grpc-plugin,apm-hibernate-search-plugin,apm-hibernate-search-plugin-common,apm-hibernate-search-plugin-5_x,apm-hibernate-search-plugin-6_x,apm-httpserver-core,apm-servlet-plugin,apm-javalin-plugin,apm-jaxrs-plugin,apm-jaxws-plugin,apm-jdbc-plugin,apm-jdk-httpclient-plugin,apm-jdk-httpserver-plugin,apm-redis-plugin,apm-redis-common,apm-jedis-plugin,apm-jedis-4-plugin,apm-jms-plugin,apm-jms-plugin-base,apm-jms-javax,apm-jms-jakarta,apm-jmx-plugin,apm-jsf-plugin,apm-kafka-plugin,apm-kafka-base-plugin,apm-kafka-headers-plugin,apm-kafka-spring-plugin,apm-lettuce-plugin,apm-logging-plugin,apm-logging-plugin-common,apm-log4j1-plugin,apm-log4j2-plugin,apm-opentelemetry,apm-opentelemetry-embedded-metrics-sdk,apm-opentelemetry-metrics-bridge-parent,apm-opentelemetry-metrics-bridge-common,apm-opentelemetry-metrics-bridge-v1_14,apm-opentelemetry-metrics-bridge-latest,apm-opentelemetry-metricsdk-plugin,apm-opentelemetry-plugin,apm-slf4j-plugin,apm-logback-plugin,apm-logback-plugin-impl,apm-jboss-logging-plugin,apm-jul-plugin,apm-tomcat-logging-plugin,apm-micrometer-plugin,apm-mongodb,apm-mongodb-common,apm-mongodb3-plugin,apm-mongodb4-plugin,apm-opentracing-plugin,apm-process-plugin,apm-profiling-plugin,apm-quartz,apm-quartz-common,apm-quartz-plugin-1,apm-quartz-plugin-2,apm-rabbitmq,apm-rabbitmq-plugin,apm-rabbitmq-spring5,apm-reactor-plugin,apm-redisson-plugin,apm-scala-concurrent-plugin,apm-scheduled-annotation-plugin,apm-sparkjava-plugin,apm-urlconnection-plugin,apm-spring-ai-plugin,apm-spring-resttemplate,apm-spring-resttemplate-plugin,apm-spring-webflux,apm-spring-webflux-common-spring5,apm-spring-webflux-common,apm-spring-webclient-plugin,apm-spring-webflux-testapp,apm-spring-webflux-testapp-spring7,apm-spring-webflux-spring5,apm-spring-webflux-plugin,apm-spring-webflux-plugin-spring7,apm-spring-webmvc,apm-spring-webmvc-spring5,apm-spring-webmvc-plugin,apm-struts-plugin,apm-vertx,apm-vertx-common,apm-vertx3-plugin,apm-vertx4-plugin,apm-awslambda-plugin,apm-jakarta-websocket-plugin,apm-ecs-logging-plugin,apm-aws-sdk,apm-aws-sdk-common,apm-aws-sdk-1-plugin,apm-aws-sdk-2-plugin,apm-finagle-httpclient-plugin,apm-java-ldap-plugin,apm-agent-builds,apm-agent,apm-agent-lambda-layer,apm-agent-java8,apm-agent-benchmarks,apm-spring-resttemplate-test,apm-spring-restclient-test,apm-logback-plugin-legacy-tests,apm-log4j2-plugin-tests,apm-jms-spring-plugin,apm-jedis-2-tests,apm-jedis-3-tests,apm-jedis-5-tests,apm-lettuce-3-tests,apm-grpc-test-1.6.1,apm-grpc-test-latest,apm-rabbitmq-test-3,apm-rabbitmq-test-4,apm-rabbitmq-test-spring6,apm-okhttp-test,apm-opentelemetry-test,apm-vertx3-test-latest,apm-servlet-jakarta-test,apm-jaxws-plugin-jakartaee-test,apm-jaxrs-plugin-jakartaee-test,apm-scheduled-annotation-plugin-jakartaee-test,integration-tests,simple-webapp,jakartaee-simple-webapp,jsf-app,jsf-app-dependent,jsf-app-standalone,jakartaee-jsf-app,jakartaee-jsf-app-dependent,jakartaee-jsf-app-standalone,cdi-app,cdi-app-dependent,cdi-app-standalone,soap-test,external-plugin-test,plugin-instrumentation-target,external-plugin-app,external-plugin-jakarta-app,external-plugin,application-server-integration-tests,spring-boot-1-5,spring-boot-2,spring-boot-2-base,spring-boot-2-jetty,spring-boot-2-tomcat,spring-boot-2-undertow,cdi-jakartaee-app,cdi-jakartaee-app-dependent,cdi-jakartaee-app-standalone,quarkus,quarkus-jaxrs-base,quarkus-jaxrs-undertow,external-plugin-otel-test,external-plugin-otel-test-app,external-plugin-otel-test-plugin1,external-plugin-otel-test-plugin2,runtime-attach,runtime-attach-app,runtime-attach-test,main-app-test,spring-boot-3,spring-boot-3-jetty,spring-boot-3-tomcat,spring-boot-3-undertow,aws-lambda-test,spring-boot-4,spring-boot-4-base,spring-boot-4-jetty,spring-boot-4-tomcat,apm-spring-webmvc-spring7-test From 37883e5cda71addfb3fbb3d7b3c4575c49fc2d5d Mon Sep 17 00:00:00 2001 From: hegerchr Date: Fri, 13 Nov 2026 08:21:59 +0100 Subject: [PATCH 13/15] Clean up Spring AI classloader matchers --- .../elastic/apm/agent/springai/VectorStoreInstrumentation.java | 3 ++- .../apm/agent/springai/VectorStoreQueryInstrumentation.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java index 6b40839023..ad2900b111 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java @@ -28,6 +28,7 @@ import javax.annotation.Nullable; +import static co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass; import static net.bytebuddy.matcher.ElementMatchers.*; public class VectorStoreInstrumentation extends AbstractSpringAiInstrumentation { @@ -35,7 +36,7 @@ public class VectorStoreInstrumentation extends AbstractSpringAiInstrumentation @Override public ElementMatcher.Junction getClassLoaderMatcher() { return super.getClassLoaderMatcher() - .and(co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass( + .and(classLoaderCanLoadClass( "org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore")); } diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java index 7919265a86..0b84df38ef 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java @@ -29,6 +29,7 @@ import javax.annotation.Nullable; +import static co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.isInterface; import static net.bytebuddy.matcher.ElementMatchers.nameContains; @@ -42,7 +43,7 @@ public class VectorStoreQueryInstrumentation extends AbstractSpringAiInstrumenta @Override public ElementMatcher.Junction getClassLoaderMatcher() { return super.getClassLoaderMatcher() - .and(co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass( + .and(classLoaderCanLoadClass( "org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore")); } From ba9ff72671ea510817ccfe82c7cffb8cf0908933 Mon Sep 17 00:00:00 2001 From: hegerchr Date: Fri, 13 Nov 2026 12:19:33 +0100 Subject: [PATCH 14/15] Address Spring AI instrumentation review feedback --- .../apm-spring-ai-plugin/pom.xml | 83 ++----------------- .../EmbeddingModelInstrumentation.java | 8 +- .../agent/springai/SpringAiConfiguration.java | 8 +- .../agent/springai/SpringAiSpanHelper.java | 18 ++-- .../springai/ToolCallbackInstrumentation.java | 7 +- .../springai/VectorStoreInstrumentation.java | 16 ++-- .../VectorStoreQueryInstrumentation.java | 8 -- ...SpringAiClassLoaderMatcherAssertions.java} | 42 ++-------- .../springai/SpringAiInstrumentationTest.java | 14 +--- .../SpringAiSupportedVersionTest.java | 36 ++++++++ .../SpringAiUnsupportedVersionTest.java | 36 ++++++++ .../apm/agent/springai/SpringAiVersionIT.java | 50 +++++++++++ docs/reference/config-ai.md | 4 +- .../config-reference-properties-file.md | 2 +- docs/reference/configuration.md | 2 +- docs/reference/supported-technologies.md | 2 +- 16 files changed, 174 insertions(+), 162 deletions(-) rename apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/{SpringAiClassLoaderMatcherTest.java => SpringAiClassLoaderMatcherAssertions.java} (58%) create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiSupportedVersionTest.java create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiUnsupportedVersionTest.java create mode 100644 apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiVersionIT.java diff --git a/apm-agent-plugins/apm-spring-ai-plugin/pom.xml b/apm-agent-plugins/apm-spring-ai-plugin/pom.xml index f509102459..7a987240a0 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/pom.xml +++ b/apm-agent-plugins/apm-spring-ai-plugin/pom.xml @@ -17,9 +17,6 @@ 8 8 2.0.2 - 2.0.0 - 1.0.8 - 1.1.7 3.8.8 2.24.3 @@ -59,6 +56,11 @@ ${project.version} test + + org.apache.ivy + ivy + test + @@ -75,79 +77,4 @@ - - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-spring-ai-compatibility-artifacts - generate-test-resources - - copy - - - - - org.springframework.ai - spring-ai-model - ${version.spring-ai-1-0} - ${project.build.directory}/compatibility-libs/${version.spring-ai-1-0} - - - org.springframework.ai - spring-ai-commons - ${version.spring-ai-1-0} - ${project.build.directory}/compatibility-libs/${version.spring-ai-1-0} - - - org.springframework.ai - spring-ai-vector-store - ${version.spring-ai-1-0} - ${project.build.directory}/compatibility-libs/${version.spring-ai-1-0} - - - org.springframework.ai - spring-ai-model - ${version.spring-ai-1-1} - ${project.build.directory}/compatibility-libs/${version.spring-ai-1-1} - - - org.springframework.ai - spring-ai-commons - ${version.spring-ai-1-1} - ${project.build.directory}/compatibility-libs/${version.spring-ai-1-1} - - - org.springframework.ai - spring-ai-vector-store - ${version.spring-ai-1-1} - ${project.build.directory}/compatibility-libs/${version.spring-ai-1-1} - - - org.springframework.ai - spring-ai-model - ${version.spring-ai-2-min} - ${project.build.directory}/compatibility-libs/${version.spring-ai-2-min} - - - org.springframework.ai - spring-ai-commons - ${version.spring-ai-2-min} - ${project.build.directory}/compatibility-libs/${version.spring-ai-2-min} - - - org.springframework.ai - spring-ai-vector-store - ${version.spring-ai-2-min} - ${project.build.directory}/compatibility-libs/${version.spring-ai-2-min} - - - - - - - - diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java index 35b01edc68..08c540a3ac 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/EmbeddingModelInstrumentation.java @@ -29,7 +29,13 @@ import javax.annotation.Nullable; -import static net.bytebuddy.matcher.ElementMatchers.*; +import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; +import static net.bytebuddy.matcher.ElementMatchers.isInterface; +import static net.bytebuddy.matcher.ElementMatchers.nameContains; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.not; +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; public class EmbeddingModelInstrumentation extends AbstractSpringAiInstrumentation { diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiConfiguration.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiConfiguration.java index cf0d522d44..64c43e353a 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiConfiguration.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiConfiguration.java @@ -23,8 +23,8 @@ public class SpringAiConfiguration extends ConfigurationOptionProvider { - private final ConfigurationOption captureAiContent = ConfigurationOption.booleanOption() - .key("capture_ai_content") + private final ConfigurationOption captureSpringAiContent = ConfigurationOption.booleanOption() + .key("capture_spring_ai_content") .configurationCategory("AI") .tags("security", "performance", "experimental", "added[1.58.0]") .description("Enables capture of Spring AI prompt and response messages, embedding inputs, vector store documents and queries, and tool arguments and results.\n\n" + @@ -33,7 +33,7 @@ public class SpringAiConfiguration extends ConfigurationOptionProvider { .dynamic(true) .buildWithDefault(false); - public boolean isCaptureAiContent() { - return captureAiContent.get(); + public boolean isCaptureSpringAiContent() { + return captureSpringAiContent.get(); } } diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java index f804577989..0c74332ba5 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/SpringAiSpanHelper.java @@ -75,7 +75,6 @@ static Span startModelSpan(String operation, Prompt prompt, boolean streaming span.withName(spanName(operation, requestModel)) .withType("app") .withSubtype("internal") - .withAction(operation) .withOtelAttribute("gen_ai.operation.name", operation) .withOtelAttribute("gen_ai.request.model", requestModel) .withOtelAttribute("gen_ai.request.stream", streaming); @@ -89,7 +88,7 @@ static Span startModelSpan(String operation, Prompt prompt, boolean streaming .withOtelAttribute("gen_ai.request.top_k", options.getTopK()) .withOtelAttribute("gen_ai.request.top_p", options.getTopP()); } - if (config.isCaptureAiContent()) { + if (config.isCaptureSpringAiContent()) { captureStructuredContent(span, "gen_ai.input.messages", serializeMessages(prompt.getInstructions())); } return span; @@ -106,11 +105,10 @@ static Span startEmbeddingSpan(EmbeddingRequest request) { span.withName(spanName("embedding", requestModel)) .withType("app") .withSubtype("internal") - .withAction("embedding") .withOtelAttribute("gen_ai.operation.name", "embedding") .withOtelAttribute("gen_ai.request.model", requestModel) .withOtelAttribute("gen_ai.request.embedding.dimensions", options == null ? null : options.getDimensions()); - if (config.isCaptureAiContent()) { + if (config.isCaptureSpringAiContent()) { captureStructuredContent(span, "spring.ai.embedding.request.inputs", serializeStrings(request.getInstructions())); } return span; @@ -134,7 +132,6 @@ static Span startVectorStoreSpan(AbstractObservationVectorStore vectorStore, span.withName((hasDbSystem ? dbSystem : "vector_store") + " " + operation) .withType("db") .withSubtype("unknown") - .withAction(operation) .withOtelAttribute("spring.ai.kind", "vector_store") .withOtelAttribute("db.operation.name", operation) .withOtelAttribute("db.collection.name", context.getCollectionName()) @@ -150,7 +147,7 @@ static Span startVectorStoreSpan(AbstractObservationVectorStore vectorStore, captureContent(span, "db.vector.query.filter", searchRequest.getFilterExpression().toString()); } } else if ("add".equals(operation) && argument instanceof List) { - if (config.isCaptureAiContent()) { + if (config.isCaptureSpringAiContent()) { captureStructuredContent(span, "db.vector.documents", serializeDocuments((List) argument)); } } @@ -172,7 +169,6 @@ static Span startToolSpan(ToolCallback callback, String toolInput) { span.withName("execute_tool " + name) .withType("app") .withSubtype("internal") - .withAction("execute_tool") .withOtelAttribute("gen_ai.operation.name", "execute_tool") .withOtelAttribute("spring.ai.kind", "tool_call") .withOtelAttribute("spring.ai.tool.type", "function") @@ -209,7 +205,7 @@ static Flux wrapChatFlux(final Prompt prompt, final Flux source) { final AtomicReference failure = new AtomicReference<>(); final AtomicBoolean ended = new AtomicBoolean(); span.withSync(false); - Flux observedSource = config.isCaptureAiContent() + Flux observedSource = config.isCaptureSpringAiContent() ? new MessageAggregator().aggregate(timedSource, aggregatedResponse::set) : timedSource; return observedSource @@ -250,7 +246,7 @@ private static void addChatResponseAttributes(Span span, @Nullable ChatRespon } } span.withOtelAttribute("gen_ai.response.finish_reasons", stringify(finishReasons)); - if (config.isCaptureAiContent()) { + if (config.isCaptureSpringAiContent()) { captureStructuredContent(span, "gen_ai.output.messages", serializeGenerations(response.getResults())); } } @@ -258,7 +254,7 @@ private static void addChatResponseAttributes(Span span, @Nullable ChatRespon static void endVectorStoreSpan(@Nullable Span span, @Nullable Object response, @Nullable Throwable thrown) { if (span != null && response instanceof List) { - if (config.isCaptureAiContent()) { + if (config.isCaptureSpringAiContent()) { captureStructuredContent(span, "db.vector.query.response.documents", serializeDocuments((List) response)); } } @@ -319,7 +315,7 @@ private static String stringify(@Nullable List values) { } private static void captureContent(Span span, String key, @Nullable String content) { - if (config.isCaptureAiContent() && content != null) { + if (config.isCaptureSpringAiContent() && content != null) { span.withOtelAttribute(key, truncate(content, coreConfig.getLongFieldMaxLength())); } } diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java index f1a09ea4ec..0a0e0605cd 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/ToolCallbackInstrumentation.java @@ -28,7 +28,12 @@ import javax.annotation.Nullable; -import static net.bytebuddy.matcher.ElementMatchers.*; +import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; +import static net.bytebuddy.matcher.ElementMatchers.isInterface; +import static net.bytebuddy.matcher.ElementMatchers.nameContains; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.not; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; public class ToolCallbackInstrumentation extends AbstractSpringAiInstrumentation { diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java index ad2900b111..b7b1c36a13 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreInstrumentation.java @@ -28,18 +28,16 @@ import javax.annotation.Nullable; -import static co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass; -import static net.bytebuddy.matcher.ElementMatchers.*; +import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; +import static net.bytebuddy.matcher.ElementMatchers.isInterface; +import static net.bytebuddy.matcher.ElementMatchers.nameContains; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; +import static net.bytebuddy.matcher.ElementMatchers.not; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; public class VectorStoreInstrumentation extends AbstractSpringAiInstrumentation { - @Override - public ElementMatcher.Junction getClassLoaderMatcher() { - return super.getClassLoaderMatcher() - .and(classLoaderCanLoadClass( - "org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore")); - } - @Override public ElementMatcher getTypeMatcherPreFilter() { return nameContains("VectorStore"); diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java index 0b84df38ef..f70678256b 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/main/java/co/elastic/apm/agent/springai/VectorStoreQueryInstrumentation.java @@ -29,7 +29,6 @@ import javax.annotation.Nullable; -import static co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.isInterface; import static net.bytebuddy.matcher.ElementMatchers.nameContains; @@ -40,13 +39,6 @@ public class VectorStoreQueryInstrumentation extends AbstractSpringAiInstrumentation { - @Override - public ElementMatcher.Junction getClassLoaderMatcher() { - return super.getClassLoaderMatcher() - .and(classLoaderCanLoadClass( - "org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore")); - } - @Override public ElementMatcher getTypeMatcherPreFilter() { return nameContains("VectorStore"); diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiClassLoaderMatcherTest.java b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiClassLoaderMatcherAssertions.java similarity index 58% rename from apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiClassLoaderMatcherTest.java rename to apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiClassLoaderMatcherAssertions.java index fcb587d4f3..80eb5766da 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiClassLoaderMatcherTest.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiClassLoaderMatcherAssertions.java @@ -18,18 +18,12 @@ */ package co.elastic.apm.agent.springai; -import org.junit.jupiter.api.Test; - -import java.net.URL; -import java.net.URLClassLoader; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Arrays; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; -class SpringAiClassLoaderMatcherTest { +public class SpringAiClassLoaderMatcherAssertions { private static final String MODEL_CLASS = "org/springframework/ai/model/Model.class"; private static final String AI_OPERATION_TYPE_CLASS = @@ -39,29 +33,15 @@ class SpringAiClassLoaderMatcherTest { private static final String SPRING_AI_2_MARKER_CLASS = "org/springframework/ai/embedding/DefaultEmbeddingOptions$Builder.class"; - @Test - void rejectsUnsupportedSpringAi1xVersions() throws Exception { - assertUnsupported("1.0.8"); - assertUnsupported("1.1.7"); - } - - @Test - void acceptsMinimumSupportedSpringAi2xVersion() throws Exception { - try (URLClassLoader classLoader = springAiClassLoader("2.0.0")) { - assertCommonSpringAiClassesPresent(classLoader); + public static void assertClassLoaderMatcher(ClassLoader classLoader, boolean supported) { + assertCommonSpringAiClassesPresent(classLoader); + if (supported) { assertThat(classLoader.getResource(SPRING_AI_2_MARKER_CLASS)).isNotNull(); - assertThat(instrumentations()).allSatisfy(instrumentation -> - assertThat(instrumentation.getClassLoaderMatcher().matches(classLoader)).isTrue()); - } - } - - private static void assertUnsupported(String version) throws Exception { - try (URLClassLoader classLoader = springAiClassLoader(version)) { - assertCommonSpringAiClassesPresent(classLoader); + } else { assertThat(classLoader.getResource(SPRING_AI_2_MARKER_CLASS)).isNull(); - assertThat(instrumentations()).allSatisfy(instrumentation -> - assertThat(instrumentation.getClassLoaderMatcher().matches(classLoader)).isFalse()); } + assertThat(instrumentations()).allSatisfy(instrumentation -> + assertThat(instrumentation.getClassLoaderMatcher().matches(classLoader)).isEqualTo(supported)); } private static void assertCommonSpringAiClassesPresent(ClassLoader classLoader) { @@ -80,12 +60,4 @@ private static List instrumentations() { new ToolCallbackInstrumentation() ); } - - private static URLClassLoader springAiClassLoader(String version) throws Exception { - Path directory = Paths.get("target", "compatibility-libs", version); - URL model = directory.resolve("spring-ai-model-" + version + ".jar").toUri().toURL(); - URL commons = directory.resolve("spring-ai-commons-" + version + ".jar").toUri().toURL(); - URL vectorStore = directory.resolve("spring-ai-vector-store-" + version + ".jar").toUri().toURL(); - return new URLClassLoader(new URL[]{model, commons, vectorStore}, null); - } } diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java index afad5e263b..3f7669c9af 100644 --- a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiInstrumentationTest.java @@ -22,6 +22,7 @@ import co.elastic.apm.agent.embeddedotel.EmbeddedSdkManager; import co.elastic.apm.agent.impl.transaction.SpanImpl; import co.elastic.apm.agent.impl.transaction.TransactionImpl; +import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.tracer.configuration.CoreConfiguration; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -81,8 +82,7 @@ void capturesChatCallAndSemanticAttributes() { .hasName("chat gpt-test") .hasType("app") .hasSubType("internal") - .hasAction("chat") - .hasOutcome(co.elastic.apm.agent.tracer.Outcome.SUCCESS) + .hasOutcome(Outcome.SUCCESS) .hasOtelAttribute("gen_ai.operation.name", "chat") .hasOtelAttribute("gen_ai.request.model", "gpt-test") .hasOtelAttribute("gen_ai.response.model", "gpt-test-result") @@ -163,7 +163,6 @@ void capturesEmbeddingCall() { SpanImpl span = reporter.getFirstSpan(); assertThat(span) .hasName("embedding embedding-test") - .hasAction("embedding") .hasOtelAttribute("gen_ai.request.embedding.dimensions", 3) .hasOtelAttribute("gen_ai.usage.input_tokens", 4); assertThat(span.getOtelAttributes()) @@ -184,14 +183,13 @@ void capturesToolDefinitionButNotArgumentsOrResult() { .hasName("execute_tool weather") .hasType("app") .hasSubType("internal") - .hasAction("execute_tool") .hasOtelAttribute("spring.ai.tool.definition.name", "weather"); assertThat(span.getOtelAttributes()) .doesNotContainKeys("spring.ai.tool.call.arguments", "spring.ai.tool.call.result"); } @Test - void capturesVectorStoreQueryWithoutQueryText() { + void capturesVectorStoreQuery() { TestVectorStore vectorStore = new TestVectorStore(new OpenAiTestEmbeddingModel()); TransactionImpl transaction = startTestRootTransaction(); @@ -210,14 +208,10 @@ void capturesVectorStoreQueryWithoutQueryText() { .hasName("simple query") .hasType("db") .hasSubType("unknown") - .hasAction("query") .hasOtelAttribute("db.system", "simple") .hasOtelAttribute("db.operation.name", "query") .hasOtelAttribute("db.vector.query.top_k", 3) .hasOtelAttribute("db.vector.query.similarity_threshold", 0.5); - assertThat(span.getOtelAttributes()) - .doesNotContainKeys("db.query.text", "db.vector.query.content", "db.vector.query.filter", - "db.vector.query.response.documents"); } @Test @@ -353,7 +347,7 @@ void truncatesCapturedContentAtLongFieldLimit() { } private static void enableContentCapture() { - doReturn(true).when(config.getConfig(SpringAiConfiguration.class)).isCaptureAiContent(); + doReturn(true).when(config.getConfig(SpringAiConfiguration.class)).isCaptureSpringAiContent(); } static class OpenAiTestChatModel implements ChatModel { diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiSupportedVersionTest.java b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiSupportedVersionTest.java new file mode 100644 index 0000000000..2ccce3db17 --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiSupportedVersionTest.java @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.testutils.TestClassWithDependencyRunner; +import org.junit.jupiter.api.Test; + +import java.net.URLClassLoader; + +@TestClassWithDependencyRunner.DisableOutsideOfRunner +class SpringAiSupportedVersionTest { + + @Test + void acceptsSpringAiVersion() throws Exception { + URLClassLoader versionClassLoader = (URLClassLoader) getClass().getClassLoader(); + try (URLClassLoader isolatedClassLoader = new URLClassLoader(versionClassLoader.getURLs(), null)) { + SpringAiClassLoaderMatcherAssertions.assertClassLoaderMatcher(isolatedClassLoader, true); + } + } +} diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiUnsupportedVersionTest.java b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiUnsupportedVersionTest.java new file mode 100644 index 0000000000..9582d6e6be --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiUnsupportedVersionTest.java @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.testutils.TestClassWithDependencyRunner; +import org.junit.jupiter.api.Test; + +import java.net.URLClassLoader; + +@TestClassWithDependencyRunner.DisableOutsideOfRunner +class SpringAiUnsupportedVersionTest { + + @Test + void rejectsSpringAiVersion() throws Exception { + URLClassLoader versionClassLoader = (URLClassLoader) getClass().getClassLoader(); + try (URLClassLoader isolatedClassLoader = new URLClassLoader(versionClassLoader.getURLs(), null)) { + SpringAiClassLoaderMatcherAssertions.assertClassLoaderMatcher(isolatedClassLoader, false); + } + } +} diff --git a/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiVersionIT.java b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiVersionIT.java new file mode 100644 index 0000000000..e8a8b3f859 --- /dev/null +++ b/apm-agent-plugins/apm-spring-ai-plugin/src/test/java/co/elastic/apm/agent/springai/SpringAiVersionIT.java @@ -0,0 +1,50 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.springai; + +import co.elastic.apm.agent.testutils.TestClassWithDependencyRunner; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.List; + +class SpringAiVersionIT { + + @ParameterizedTest + @ValueSource(strings = {"1.0.8", "1.1.7"}) + void rejectsUnsupportedSpringAiVersions(String version) throws Exception { + runVersionTest(version, SpringAiUnsupportedVersionTest.class); + } + + @ParameterizedTest + @ValueSource(strings = {"2.0.0", "2.0.2"}) + void acceptsSupportedSpringAiVersions(String version) throws Exception { + runVersionTest(version, SpringAiSupportedVersionTest.class); + } + + private static void runVersionTest(String version, Class testClass) throws Exception { + List dependencies = List.of( + "org.springframework.ai:spring-ai-model:" + version, + "org.springframework.ai:spring-ai-commons:" + version, + "org.springframework.ai:spring-ai-vector-store:" + version); + TestClassWithDependencyRunner runner = new TestClassWithDependencyRunner( + dependencies, testClass.getName()); + runner.run(); + } +} diff --git a/docs/reference/config-ai.md b/docs/reference/config-ai.md index 194ba6eeac..cd5e16e645 100644 --- a/docs/reference/config-ai.md +++ b/docs/reference/config-ai.md @@ -10,7 +10,7 @@ applies_to: # AI configuration options [config-ai] -## `capture_ai_content` (experimental, security, performance) [config-capture-ai-content] +## `capture_spring_ai_content` (experimental, security, performance) [config-capture-spring-ai-content] ```{applies_to} apm_agent_java: ga 1.58.0 @@ -30,4 +30,4 @@ Support for configuring this option through Kibana central configuration will be | Java System Properties | Property file | Environment | | --- | --- | --- | -| `elastic.apm.capture_ai_content` | `capture_ai_content` | `ELASTIC_APM_CAPTURE_AI_CONTENT` | +| `elastic.apm.capture_spring_ai_content` | `capture_spring_ai_content` | `ELASTIC_APM_CAPTURE_SPRING_AI_CONTENT` | diff --git a/docs/reference/config-reference-properties-file.md b/docs/reference/config-reference-properties-file.md index 5b7d04a442..a71a72522c 100644 --- a/docs/reference/config-reference-properties-file.md +++ b/docs/reference/config-reference-properties-file.md @@ -27,7 +27,7 @@ applies_to: # Type: Boolean # Default value: false # -# capture_ai_content=false +# capture_spring_ai_content=false ############################################ # Circuit-Breaker # diff --git a/docs/reference/configuration.md b/docs/reference/configuration.md index fe5ca72a0f..fdb34bf65a 100644 --- a/docs/reference/configuration.md +++ b/docs/reference/configuration.md @@ -80,7 +80,7 @@ This is a list of all configuration options grouped by their category. Click on * [AI](/reference/config-ai.md) - * [`capture_ai_content` (experimental, security, performance [1.58.0])](/reference/config-ai.md#config-capture-ai-content) + * [`capture_spring_ai_content` (experimental, security, performance [1.58.0])](/reference/config-ai.md#config-capture-spring-ai-content) * [Circuit-Breaker](/reference/config-circuit-breaker.md) diff --git a/docs/reference/supported-technologies.md b/docs/reference/supported-technologies.md index 0a2e4b12d8..6e5397307e 100644 --- a/docs/reference/supported-technologies.md +++ b/docs/reference/supported-technologies.md @@ -133,7 +133,7 @@ The Elastic APM Java agent has generic support for the Servlet API 3+. However, | Framework | Supported versions | Description | Since | |-----------|--------------------|-------------|-------| -| Spring AI | 2.x | Creates spans for synchronous and streaming chat model calls, embedding model calls, vector store operations, and tool callbacks. Prompt, response, document, query, tool argument, and tool result content is not captured by default; use [`capture_ai_content`](/reference/config-ai.md#config-capture-ai-content) to opt in. This experimental instrumentation is disabled by default; enable it with [`enable_experimental_instrumentations`](/reference/config-core.md#config-enable-experimental-instrumentations). | 1.58.0 (experimental) | +| Spring AI | 2.0.0 - 2.0.2 | Creates spans for synchronous and streaming chat model calls, embedding model calls, vector store operations, and tool callbacks. Prompt, response, document, query, tool argument, and tool result content is not captured by default; use [`capture_spring_ai_content`](/reference/config-ai.md#config-capture-spring-ai-content) to opt in. This experimental instrumentation is disabled by default; enable it with [`enable_experimental_instrumentations`](/reference/config-core.md#config-enable-experimental-instrumentations). | 1.58.0 (experimental) | ## Networking frameworks [supported-networking-frameworks] From 9591fb7526c2bde0f753eb993473d6b30208ccbb Mon Sep 17 00:00:00 2001 From: hegerchr Date: Fri, 13 Nov 2026 12:37:36 +0100 Subject: [PATCH 15/15] Regenerate configuration reference --- docs/reference/config-reference-properties-file.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/docs/reference/config-reference-properties-file.md b/docs/reference/config-reference-properties-file.md index a71a72522c..5ed6a4c35c 100644 --- a/docs/reference/config-reference-properties-file.md +++ b/docs/reference/config-reference-properties-file.md @@ -16,12 +16,9 @@ applies_to: # AI # ############################################ -# Enables capture of Spring AI prompt and response messages, embedding inputs, vector store documents and queries, -# and tool arguments and results. +# Enables capture of Spring AI prompt and response messages, embedding inputs, vector store documents and queries, and tool arguments and results. # -# Captured content can contain sensitive information and can significantly increase storage usage. -# Content capture is disabled by default. Individual captured content values are truncated according to -# `long_field_max_length`. +# Captured content can contain sensitive information and can significantly increase storage usage. Content capture is disabled by default. Individual captured content values are truncated according to `long_field_max_length`. # # This setting can be changed at runtime # Type: Boolean