|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.apm.plugin.spring.ai.v1; |
| 20 | + |
| 21 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 22 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags; |
| 23 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 24 | +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; |
| 25 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 26 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 27 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 28 | +import org.apache.skywalking.apm.agent.core.util.GsonUtil; |
| 29 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 30 | +import org.apache.skywalking.apm.plugin.spring.ai.v1.common.ErrorTypeResolver; |
| 31 | +import org.apache.skywalking.apm.plugin.spring.ai.v1.config.SpringAiPluginConfig; |
| 32 | +import org.apache.skywalking.apm.plugin.spring.ai.v1.contant.Constants; |
| 33 | +import org.springframework.ai.document.Document; |
| 34 | +import org.springframework.ai.vectorstore.SearchRequest; |
| 35 | +import org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore; |
| 36 | +import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext; |
| 37 | +import org.springframework.util.StringUtils; |
| 38 | + |
| 39 | +import java.lang.reflect.Method; |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.LinkedHashMap; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Map; |
| 44 | + |
| 45 | +public class AbstractObservationVectorStoreInterceptor implements InstanceMethodsAroundInterceptor { |
| 46 | + |
| 47 | + @Override |
| 48 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 49 | + MethodInterceptResult result) throws Throwable { |
| 50 | + SearchRequest request = (SearchRequest) allArguments[0]; |
| 51 | + String dataSourceId = objInst.getClass().getSimpleName(); |
| 52 | + |
| 53 | + try { |
| 54 | + VectorStoreObservationContext context = |
| 55 | + createObservationContext(objInst, request); |
| 56 | + |
| 57 | + String resolved = |
| 58 | + resolveDataSourceId(context, objInst); |
| 59 | + |
| 60 | + if (StringUtils.hasText(resolved)) { |
| 61 | + dataSourceId = resolved; |
| 62 | + } |
| 63 | + } catch (Throwable ignored) { |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + AbstractSpan span = ContextManager.createExitSpan(Constants.RETRIEVAL + "/" + dataSourceId, dataSourceId); |
| 68 | + |
| 69 | + SpanLayer.asGenAI(span); |
| 70 | + span.setComponent(ComponentsDefine.SPRING_AI); |
| 71 | + Tags.GEN_AI_OPERATION_NAME.set(span, Constants.RETRIEVAL); |
| 72 | + Tags.GEN_AI_DATA_SOURCE_ID.set(span, dataSourceId); |
| 73 | + String model = resolveEmbeddingModelName(objInst); |
| 74 | + if (StringUtils.hasText(model)) { |
| 75 | + Tags.GEN_AI_REQUEST_MODEL.set(span, model); |
| 76 | + } |
| 77 | + |
| 78 | + if (request != null) { |
| 79 | + Tags.GEN_AI_TOP_K.set(span, String.valueOf(request.getTopK())); |
| 80 | + String query = request.getQuery(); |
| 81 | + if (StringUtils.hasText(query) && SpringAiPluginConfig.Plugin.SpringAi.COLLECT_RETRIEVAL_QUERY) { |
| 82 | + int limit = SpringAiPluginConfig.Plugin.SpringAi.RETRIEVAL_QUERY_LENGTH_LIMIT; |
| 83 | + if (limit > 0 && query.length() > limit) { |
| 84 | + query = query.substring(0, limit); |
| 85 | + } |
| 86 | + Tags.GEN_AI_RETRIEVAL_QUERY_TEXT.set(span, query); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 93 | + Object ret) throws Throwable { |
| 94 | + if (!ContextManager.isActive()) { |
| 95 | + return ret; |
| 96 | + } |
| 97 | + try { |
| 98 | + if (ret instanceof List<?> && SpringAiPluginConfig.Plugin.SpringAi.COLLECT_RETRIEVAL_DOCUMENTS) { |
| 99 | + Tags.GEN_AI_RETRIEVAL_DOCUMENTS.set(ContextManager.activeSpan(), toDocumentsJson((List<?>) ret)); |
| 100 | + } |
| 101 | + } finally { |
| 102 | + ContextManager.stopSpan(); |
| 103 | + } |
| 104 | + return ret; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, |
| 109 | + Class<?>[] argumentsTypes, Throwable t) { |
| 110 | + if (ContextManager.isActive()) { |
| 111 | + AbstractSpan span = ContextManager.activeSpan(); |
| 112 | + span.log(t); |
| 113 | + ErrorTypeResolver.setErrorType(span, t); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private VectorStoreObservationContext createObservationContext(EnhancedInstance objInst, SearchRequest request) { |
| 118 | + VectorStoreObservationContext.Builder builder = ((AbstractObservationVectorStore) objInst) |
| 119 | + .createObservationContextBuilder(VectorStoreObservationContext.Operation.QUERY.value()); |
| 120 | + if (request != null) { |
| 121 | + builder.queryRequest(request); |
| 122 | + } |
| 123 | + return builder.build(); |
| 124 | + } |
| 125 | + |
| 126 | + private String resolveEmbeddingModelName(EnhancedInstance objInst) { |
| 127 | + Object context = objInst.getSkyWalkingDynamicField(); |
| 128 | + if (context instanceof VectorStoreEnhanceContext) { |
| 129 | + return ((VectorStoreEnhanceContext) context).getEmbeddingModelName(); |
| 130 | + } |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + private String resolveDataSourceId(VectorStoreObservationContext context, EnhancedInstance objInst) { |
| 135 | + StringBuilder dataSourceId = new StringBuilder(); |
| 136 | + appendDataSourcePart(dataSourceId, context.getDatabaseSystem()); |
| 137 | + appendDataSourcePart(dataSourceId, context.getNamespace()); |
| 138 | + appendDataSourcePart(dataSourceId, context.getCollectionName()); |
| 139 | + if (dataSourceId.length() > 0) { |
| 140 | + return dataSourceId.toString(); |
| 141 | + } |
| 142 | + return objInst.getClass().getSimpleName(); |
| 143 | + } |
| 144 | + |
| 145 | + private void appendDataSourcePart(StringBuilder dataSourceId, String value) { |
| 146 | + if (!StringUtils.hasText(value)) { |
| 147 | + return; |
| 148 | + } |
| 149 | + if (dataSourceId.length() > 0) { |
| 150 | + dataSourceId.append('/'); |
| 151 | + } |
| 152 | + dataSourceId.append(value); |
| 153 | + } |
| 154 | + |
| 155 | + private String toDocumentsJson(List<?> documents) { |
| 156 | + List<Map<String, Object>> retrievalDocuments = new ArrayList<>(documents.size()); |
| 157 | + for (Object item : documents) { |
| 158 | + if (!(item instanceof Document)) { |
| 159 | + continue; |
| 160 | + } |
| 161 | + Document document = (Document) item; |
| 162 | + Map<String, Object> documentMap = new LinkedHashMap<>(); |
| 163 | + documentMap.put("id", document.getId()); |
| 164 | + if (document.getScore() != null) { |
| 165 | + documentMap.put("score", document.getScore()); |
| 166 | + } |
| 167 | + retrievalDocuments.add(documentMap); |
| 168 | + } |
| 169 | + return GsonUtil.toJson(retrievalDocuments); |
| 170 | + } |
| 171 | +} |
0 commit comments