Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This file contains all changes which are not released yet.
<!--FIXES-END-->
# Features and enhancements
<!--ENHANCEMENTS-START-->
- Add experimental auto-instrumentation for Spring AI 2.x chat models, embedding models, vector stores, and tool callbacks, with opt-in AI content capture

<!--ENHANCEMENTS-END-->
# Deprecations
Expand Down
5 changes: 5 additions & 0 deletions apm-agent-builds/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@
<artifactId>apm-sparkjava-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apm-spring-ai-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apm-spring-resttemplate-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down
80 changes: 80 additions & 0 deletions apm-agent-plugins/apm-spring-ai-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>co.elastic.apm</groupId>
<artifactId>apm-agent-plugins</artifactId>
<version>1.57.2-SNAPSHOT</version>
</parent>

<artifactId>apm-spring-ai-plugin</artifactId>
<name>${project.groupId}:${project.artifactId}</name>

<properties>
<apm-agent-parent.base.dir>${project.basedir}/../..</apm-agent-parent.base.dir>
<animal.sniffer.skip>true</animal.sniffer.skip>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<version.spring-ai>2.0.2</version.spring-ai>
<version.reactor>3.8.8</version.reactor>
<version.log4j2-test>2.24.3</version.log4j2-test>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apm-agent-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apm-opentelemetry-embedded-metrics-sdk</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-model</artifactId>
<version>${version.spring-ai}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-vector-store</artifactId>
<version>${version.spring-ai}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>${version.reactor}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apm-opentelemetry-metricsdk-plugin</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.ivy</groupId>
<artifactId>ivy</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Framework 7's spring-jcl requires a current Log4j API when
the agent core test fixture puts Log4j on the test classpath. -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${version.log4j2-test}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${version.log4j2-test}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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<ClassLoader> getClassLoaderMatcher() {
return classLoaderCanLoadClass("org.springframework.ai.model.Model")
.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
public Collection<String> getInstrumentationGroupNames() {
return Arrays.asList("spring-ai", "experimental");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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<? super NamedElement> getTypeMatcherPreFilter() {
return nameContains("ChatModel");
}

@Override
public ElementMatcher<? super TypeDescription> getTypeMatcher() {
return not(isInterface()).and(hasSuperType(named("org.springframework.ai.chat.model.ChatModel")));
}

@Override
public ElementMatcher<? super MethodDescription> 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 {

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
public static Object onEnter(@Advice.Argument(0) Prompt prompt) {
Span<?> span = SpringAiSpanHelper.startModelSpan("chat", prompt, 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.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 {

@Override
public ElementMatcher<? super NamedElement> getTypeMatcherPreFilter() {
return nameContains("Embedding");
}

@Override
public ElementMatcher<? super TypeDescription> getTypeMatcher() {
return not(isInterface()).and(hasSuperType(named("org.springframework.ai.embedding.EmbeddingModel")));
}

@Override
public ElementMatcher<? super MethodDescription> 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 {

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
public static Object onEnter(@Advice.Argument(0) EmbeddingRequest request) {
Span<?> span = SpringAiSpanHelper.startEmbeddingSpan(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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Boolean> 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" +
"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 isCaptureSpringAiContent() {
return captureSpringAiContent.get();
}
}
Loading
Loading