|
| 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 | +package org.apache.skywalking.oap.server.analyzer.provider.trace.parser.listener.vservice; |
| 19 | + |
| 20 | +import lombok.RequiredArgsConstructor; |
| 21 | +import org.apache.skywalking.apm.network.language.agent.v3.SegmentObject; |
| 22 | +import org.apache.skywalking.apm.network.language.agent.v3.SpanLayer; |
| 23 | +import org.apache.skywalking.apm.network.language.agent.v3.SpanObject; |
| 24 | +import org.apache.skywalking.oap.analyzer.genai.service.IGenAIMeterAnalyzerService; |
| 25 | +import org.apache.skywalking.oap.server.core.analysis.Layer; |
| 26 | +import org.apache.skywalking.oap.server.core.config.NamingControl; |
| 27 | +import org.apache.skywalking.oap.server.core.source.GenAIMetrics; |
| 28 | +import org.apache.skywalking.oap.server.core.source.GenAIModelAccess; |
| 29 | +import org.apache.skywalking.oap.server.core.source.GenAIProviderAccess; |
| 30 | +import org.apache.skywalking.oap.server.core.source.ServiceInstance; |
| 31 | +import org.apache.skywalking.oap.server.core.source.ServiceMeta; |
| 32 | +import org.apache.skywalking.oap.server.core.source.Source; |
| 33 | + |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.List; |
| 36 | +import java.util.function.Consumer; |
| 37 | + |
| 38 | +@RequiredArgsConstructor |
| 39 | +public class VirtualGenAIProcessor implements VirtualServiceProcessor { |
| 40 | + |
| 41 | + private final NamingControl namingControl; |
| 42 | + |
| 43 | + private final IGenAIMeterAnalyzerService meterAnalyzerService; |
| 44 | + |
| 45 | + private final List<Source> recordList = new ArrayList<>(); |
| 46 | + |
| 47 | + @Override |
| 48 | + public void prepareVSIfNecessary(SpanObject span, SegmentObject segmentObject) { |
| 49 | + if (span.getSpanLayer() != SpanLayer.GenAI) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + GenAIMetrics metrics = meterAnalyzerService.extractMetricsFromSWSpan(span, segmentObject); |
| 54 | + if (metrics == null) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + recordList.add(toServiceMeta(metrics)); |
| 59 | + recordList.add(toInstance(metrics)); |
| 60 | + recordList.add(toProviderAccess(metrics)); |
| 61 | + recordList.add(toModelAccess(metrics)); |
| 62 | + } |
| 63 | + |
| 64 | + private ServiceMeta toServiceMeta(GenAIMetrics metrics) { |
| 65 | + ServiceMeta service = new ServiceMeta(); |
| 66 | + service.setName(namingControl.formatServiceName(metrics.getProviderName())); |
| 67 | + service.setLayer(Layer.VIRTUAL_GENAI); |
| 68 | + service.setTimeBucket(metrics.getTimeBucket()); |
| 69 | + return service; |
| 70 | + } |
| 71 | + |
| 72 | + private Source toInstance(GenAIMetrics metrics) { |
| 73 | + ServiceInstance instance = new ServiceInstance(); |
| 74 | + instance.setTimeBucket(metrics.getTimeBucket()); |
| 75 | + instance.setName(namingControl.formatInstanceName(metrics.getModelName())); |
| 76 | + instance.setServiceLayer(Layer.VIRTUAL_GENAI); |
| 77 | + instance.setServiceName(metrics.getProviderName()); |
| 78 | + return instance; |
| 79 | + } |
| 80 | + |
| 81 | + private GenAIProviderAccess toProviderAccess(GenAIMetrics metrics) { |
| 82 | + GenAIProviderAccess source = new GenAIProviderAccess(); |
| 83 | + source.setName(namingControl.formatServiceName(metrics.getProviderName())); |
| 84 | + source.setInputTokens(metrics.getInputTokens()); |
| 85 | + source.setOutputTokens(metrics.getOutputTokens()); |
| 86 | + source.setTotalEstimatedCost(metrics.getTotalEstimatedCost()); |
| 87 | + source.setLatency(metrics.getLatency()); |
| 88 | + source.setStatus(metrics.isStatus()); |
| 89 | + source.setTimeBucket(metrics.getTimeBucket()); |
| 90 | + return source; |
| 91 | + } |
| 92 | + |
| 93 | + private GenAIModelAccess toModelAccess(GenAIMetrics metrics) { |
| 94 | + GenAIModelAccess source = new GenAIModelAccess(); |
| 95 | + source.setServiceName(namingControl.formatServiceName(metrics.getProviderName())); |
| 96 | + source.setModelName(namingControl.formatInstanceName(metrics.getModelName())); |
| 97 | + source.setInputTokens(metrics.getInputTokens()); |
| 98 | + source.setOutputTokens(metrics.getOutputTokens()); |
| 99 | + source.setTotalEstimatedCost(metrics.getTotalEstimatedCost()); |
| 100 | + source.setTimeToFirstToken(metrics.getTimeToFirstToken()); |
| 101 | + source.setLatency(metrics.getLatency()); |
| 102 | + source.setStatus(metrics.isStatus()); |
| 103 | + source.setTimeBucket(metrics.getTimeBucket()); |
| 104 | + return source; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void emitTo(Consumer<Source> consumer) { |
| 109 | + for (Source source : recordList) { |
| 110 | + if (source != null) { |
| 111 | + consumer.accept(source); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments