-
Notifications
You must be signed in to change notification settings - Fork 689
Support tracing RAG retrieval in Spring AI 1.x plugin #808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 22 commits
dd0fc35
1347195
81c3b2f
75d09e6
f33622d
8768a9d
3cc5e13
7268543
fa23baf
591427b
4682bd7
77b4e81
d1dd31d
a9843a7
d772141
f6675da
3b1acdc
e7923be
c852d8e
a453e4e
aeaf35a
e928170
2219fe7
9874bd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF 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 org.apache.skywalking.apm.plugin.spring.ai.v1; | ||
|
|
||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; | ||
|
|
||
| public class AbstractObservationVectorStoreConstructorInterceptor implements InstanceConstructorInterceptor { | ||
|
|
||
| @Override | ||
| public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { | ||
| objInst.setSkyWalkingDynamicField(new VectorStoreEnhanceContext(resolveContextFromArgument(allArguments[0]))); | ||
| } | ||
|
|
||
| private EmbeddingModelEnhanceContext resolveContextFromArgument(Object argument) { | ||
| if (argument instanceof EnhancedInstance) { | ||
| return getOrCreateContext((EnhancedInstance) argument); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private EmbeddingModelEnhanceContext getOrCreateContext(EnhancedInstance embeddingModel) { | ||
| Object context = embeddingModel.getSkyWalkingDynamicField(); | ||
| if (context instanceof EmbeddingModelEnhanceContext) { | ||
| return (EmbeddingModelEnhanceContext) context; | ||
| } | ||
| EmbeddingModelEnhanceContext embeddingModelEnhanceContext = new EmbeddingModelEnhanceContext(); | ||
| embeddingModel.setSkyWalkingDynamicField(embeddingModelEnhanceContext); | ||
| return embeddingModelEnhanceContext; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF 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 org.apache.skywalking.apm.plugin.spring.ai.v1; | ||
|
|
||
| import org.apache.skywalking.apm.agent.core.context.ContextManager; | ||
| import org.apache.skywalking.apm.agent.core.context.tag.Tags; | ||
| import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; | ||
| import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; | ||
| import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
| import org.apache.skywalking.apm.agent.core.util.GsonUtil; | ||
| import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; | ||
| import org.apache.skywalking.apm.plugin.spring.ai.v1.contant.Constants; | ||
| import org.springframework.ai.document.Document; | ||
| import org.springframework.ai.vectorstore.SearchRequest; | ||
| import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore; | ||
| import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class AbstractObservationVectorStoreInterceptor implements InstanceMethodsAroundInterceptor { | ||
|
|
||
| @Override | ||
| public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, | ||
| MethodInterceptResult result) throws Throwable { | ||
| SearchRequest request = (SearchRequest) allArguments[0]; | ||
| String dataSourceId = objInst.getClass().getSimpleName(); | ||
|
|
||
| try { | ||
| VectorStoreObservationContext context = | ||
| createObservationContext(objInst, request); | ||
|
|
||
| String resolved = | ||
| resolveDataSourceId(context, objInst); | ||
|
|
||
| if (StringUtils.hasText(resolved)) { | ||
| dataSourceId = resolved; | ||
| } | ||
| } catch (Throwable ignored) { | ||
|
|
||
| } | ||
|
|
||
| AbstractSpan span = ContextManager.createExitSpan(Constants.RETRIEVAL + "/" + dataSourceId, dataSourceId); | ||
|
|
||
| SpanLayer.asGenAI(span); | ||
| span.setComponent(ComponentsDefine.SPRING_AI); | ||
| Tags.GEN_AI_OPERATION_NAME.set(span, Constants.RETRIEVAL); | ||
| Tags.GEN_AI_DATA_SOURCE_ID.set(span, dataSourceId); | ||
| String model = resolveEmbeddingModelName(objInst); | ||
| if (StringUtils.hasText(model)) { | ||
| Tags.GEN_AI_REQUEST_MODEL.set(span, model); | ||
| } | ||
|
|
||
| if (request != null) { | ||
| Tags.GEN_AI_TOP_K.set(span, String.valueOf(request.getTopK())); | ||
| if (StringUtils.hasText(request.getQuery())) { | ||
| Tags.GEN_AI_RETRIEVAL_QUERY_TEXT.set(span, request.getQuery()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, | ||
| Object ret) throws Throwable { | ||
| if (!ContextManager.isActive()) { | ||
| return ret; | ||
| } | ||
| try { | ||
| if (ret instanceof List<?>) { | ||
| Tags.GEN_AI_RETRIEVAL_DOCUMENTS.set(ContextManager.activeSpan(), toDocumentsJson((List<?>) ret)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Lower risk than the query text since only |
||
| } | ||
| } finally { | ||
| ContextManager.stopSpan(); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| @Override | ||
| public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, | ||
| Class<?>[] argumentsTypes, Throwable t) { | ||
| if (ContextManager.isActive()) { | ||
| ContextManager.activeSpan().log(t); | ||
|
wu-sheng marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| private VectorStoreObservationContext createObservationContext(EnhancedInstance objInst, SearchRequest request) { | ||
| VectorStoreObservationContext.Builder builder = ((AbstractObservationVectorStore) objInst) | ||
| .createObservationContextBuilder(VectorStoreObservationContext.Operation.QUERY.value()); | ||
| if (request != null) { | ||
| builder.queryRequest(request); | ||
| } | ||
| return builder.build(); | ||
| } | ||
|
|
||
| private String resolveEmbeddingModelName(EnhancedInstance objInst) { | ||
| Object context = objInst.getSkyWalkingDynamicField(); | ||
| if (context instanceof VectorStoreEnhanceContext) { | ||
| return ((VectorStoreEnhanceContext) context).getEmbeddingModelName(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private String resolveDataSourceId(VectorStoreObservationContext context, EnhancedInstance objInst) { | ||
| StringBuilder dataSourceId = new StringBuilder(); | ||
| appendDataSourcePart(dataSourceId, context.getDatabaseSystem()); | ||
| appendDataSourcePart(dataSourceId, context.getNamespace()); | ||
| appendDataSourcePart(dataSourceId, context.getCollectionName()); | ||
| if (dataSourceId.length() > 0) { | ||
| return dataSourceId.toString(); | ||
| } | ||
| return objInst.getClass().getSimpleName(); | ||
| } | ||
|
|
||
| private void appendDataSourcePart(StringBuilder dataSourceId, String value) { | ||
| if (!StringUtils.hasText(value)) { | ||
| return; | ||
| } | ||
| if (dataSourceId.length() > 0) { | ||
| dataSourceId.append('/'); | ||
| } | ||
| dataSourceId.append(value); | ||
| } | ||
|
|
||
| private String toDocumentsJson(List<?> documents) { | ||
| List<Map<String, Object>> retrievalDocuments = new ArrayList<>(documents.size()); | ||
| for (Object item : documents) { | ||
| if (!(item instanceof Document)) { | ||
| continue; | ||
| } | ||
| Document document = (Document) item; | ||
| Map<String, Object> documentMap = new LinkedHashMap<>(); | ||
| documentMap.put("id", document.getId()); | ||
| if (document.getScore() != null) { | ||
| documentMap.put("score", document.getScore()); | ||
| } | ||
| retrievalDocuments.add(documentMap); | ||
| } | ||
| return GsonUtil.toJson(retrievalDocuments); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF 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 org.apache.skywalking.apm.plugin.spring.ai.v1; | ||
|
|
||
| import org.springframework.util.StringUtils; | ||
|
|
||
| public class EmbeddingModelEnhanceContext { | ||
|
|
||
| private volatile String embeddingModelName; | ||
|
|
||
| public String getEmbeddingModelName() { | ||
| return embeddingModelName; | ||
| } | ||
|
|
||
| public void setEmbeddingModelNameIfAbsent(String embeddingModelName) { | ||
| if (!StringUtils.hasText(this.embeddingModelName) && StringUtils.hasText(embeddingModelName)) { | ||
| this.embeddingModelName = embeddingModelName; | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.