You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2022/10/27 13:23:43 UTC

[GitHub] [skywalking] kezhenxu94 commented on a diff in pull request #9855: Add virtual MQ analysis for native traces

kezhenxu94 commented on code in PR #9855:
URL: https://github.com/apache/skywalking/pull/9855#discussion_r1006855714


##########
oap-server/analyzer/agent-analyzer/src/main/java/org/apache/skywalking/oap/server/analyzer/provider/trace/parser/listener/vservice/VirtualMQProcessor.java:
##########
@@ -0,0 +1,139 @@
+/*
+ *   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.oap.server.analyzer.provider.trace.parser.listener.vservice;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Consumer;
+import lombok.RequiredArgsConstructor;
+import org.apache.skywalking.apm.network.common.v3.KeyStringValuePair;
+import org.apache.skywalking.apm.network.language.agent.v3.SegmentObject;
+import org.apache.skywalking.apm.network.language.agent.v3.SpanLayer;
+import org.apache.skywalking.apm.network.language.agent.v3.SpanObject;
+import org.apache.skywalking.apm.network.language.agent.v3.SpanType;
+import org.apache.skywalking.oap.server.analyzer.provider.trace.parser.SpanTags;
+import org.apache.skywalking.oap.server.core.analysis.Layer;
+import org.apache.skywalking.oap.server.core.analysis.TimeBucket;
+import org.apache.skywalking.oap.server.core.config.NamingControl;
+import org.apache.skywalking.oap.server.core.source.EndpointMeta;
+import org.apache.skywalking.oap.server.core.source.MQAccess;
+import org.apache.skywalking.oap.server.core.source.MQEndpointAccess;
+import org.apache.skywalking.oap.server.core.source.MQOperation;
+import org.apache.skywalking.oap.server.core.source.ServiceMeta;
+import org.apache.skywalking.oap.server.core.source.Source;
+import org.apache.skywalking.oap.server.library.util.StringUtil;
+
+@RequiredArgsConstructor
+public class VirtualMQProcessor implements VirtualServiceProcessor {
+
+    private final NamingControl namingControl;
+    private final List<Source> sourceList = new ArrayList<>();
+
+    @Override
+    public void prepareVSIfNecessary(final SpanObject span, final SegmentObject segmentObject) {
+        if (span.getSpanLayer() != SpanLayer.MQ) {
+            return;
+        }
+        MQTags mqTags = collectTags(span.getTagsList());
+        if (StringUtil.isBlank(mqTags.broker)) {
+            return;
+        }
+        long timeBucket = TimeBucket.getMinuteTimeBucket(span.getStartTime());
+        String serviceName = namingControl.formatServiceName(mqTags.broker);
+        sourceList.add(toServiceMeta(serviceName, timeBucket));
+        String endpoint = buildEndpointName(mqTags.topic, mqTags.queue);
+        String endpointName = namingControl.formatEndpointName(serviceName, endpoint);
+        sourceList.add(toEndpointMeta(serviceName, endpointName, timeBucket));
+
+        final MQOperation mqOperation = span.getSpanType() == SpanType.Entry ? MQOperation.Consume : MQOperation.Produce;
+        MQAccess access = new MQAccess();
+        access.setTypeId(span.getComponentId());
+        access.setTransmissionLatency(mqTags.transmissionLatency);
+        access.setName(serviceName);
+        access.setStatus(!span.getIsError());
+        access.setTimeBucket(timeBucket);
+        access.setOperation(mqOperation);
+        sourceList.add(access);
+
+        MQEndpointAccess endpointAccess = new MQEndpointAccess();
+        endpointAccess.setTypeId(span.getComponentId());
+        endpointAccess.setTransmissionLatency(mqTags.transmissionLatency);
+        endpointAccess.setStatus(!span.getIsError());
+        endpointAccess.setTimeBucket(timeBucket);
+        endpointAccess.setOperation(mqOperation);
+        endpointAccess.setServiceName(serviceName);
+        endpointAccess.setEndpoint(endpointName);
+        sourceList.add(endpointAccess);
+    }
+
+    private String buildEndpointName(String topic, String queue) {
+        return Arrays.stream(new String[] {
+                         topic,
+                         queue
+                     }).filter(StringUtil::isNotBlank)
+                     .reduce((a, b) -> a + "/" + b).orElse("");

Review Comment:
   ```suggestion
           return Stream.of(topic, queue)
                        .filter(StringUtil::isNotBlank)
                        .reduce((a, b) -> a + "/" + b).orElse("");
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org