This is an automated email from the ASF dual-hosted git repository.

liuhan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git


The following commit(s) were added to refs/heads/master by this push:
     new f06886c1ef Enhance the `serviceRelation` in MAL by adding settings for 
the `delimiter` and `component` fields (#11251)
f06886c1ef is described below

commit f06886c1ef49946474df09aac76badde7ac102d4
Author: mrproliu <[email protected]>
AuthorDate: Tue Aug 22 18:19:42 2023 +0800

    Enhance the `serviceRelation` in MAL by adding settings for the `delimiter` 
and `component` fields (#11251)
---
 docs/en/changes/changes.md                         |  1 +
 .../skywalking/oap/meter/analyzer/Analyzer.java    |  4 +-
 .../ServiceRelationEntityDescription.java          | 13 +++++--
 .../oap/meter/analyzer/dsl/SampleFamily.java       | 22 ++++++++++-
 .../oap/meter/analyzer/dsl/ScopeTest.java          | 43 +++++++++++++++++++++-
 .../server/core/analysis/meter/MeterEntity.java    |  3 +-
 6 files changed, 76 insertions(+), 10 deletions(-)

diff --git a/docs/en/changes/changes.md b/docs/en/changes/changes.md
index ed578dfa7a..d079bc4a5e 100644
--- a/docs/en/changes/changes.md
+++ b/docs/en/changes/changes.md
@@ -73,6 +73,7 @@
 * Support `Compare Operation` in MQE.
 * Fix the Kubernetes resource cache not refreshed.
 * Fix wrong classpath that might cause OOM in startup.
+* Enhance the `serviceRelation` in MAL by adding settings for the `delimiter` 
and `component` fields. 
 
 #### UI
 
diff --git 
a/oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/Analyzer.java
 
b/oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/Analyzer.java
index be96151c0b..a87bd47122 100644
--- 
a/oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/Analyzer.java
+++ 
b/oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/Analyzer.java
@@ -324,7 +324,7 @@ public class Analyzer {
         
metrics.setTimeBucket(TimeBucket.getMinuteTimeBucket(System.currentTimeMillis()));
         metrics.setSourceServiceId(entity.sourceServiceId());
         metrics.setDestServiceId(entity.destServiceId());
-        metrics.getComponentIds().add(0);
+        metrics.getComponentIds().add(entity.getComponentId());
         metrics.setEntityId(entity.id());
         MetricsStreamProcessor.getInstance().in(metrics);
     }
@@ -334,7 +334,7 @@ public class Analyzer {
         
metrics.setTimeBucket(TimeBucket.getMinuteTimeBucket(System.currentTimeMillis()));
         metrics.setSourceServiceId(entity.sourceServiceId());
         metrics.setDestServiceId(entity.destServiceId());
-        metrics.getComponentIds().add(0);
+        metrics.getComponentIds().add(entity.getComponentId());
         metrics.setEntityId(entity.id());
         MetricsStreamProcessor.getInstance().in(metrics);
     }
diff --git 
a/oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/dsl/EntityDescription/ServiceRelationEntityDescription.java
 
b/oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/dsl/EntityDescription/ServiceRelationEntityDescription.java
index ce64101904..1adf7fd9aa 100644
--- 
a/oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/dsl/EntityDescription/ServiceRelationEntityDescription.java
+++ 
b/oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/dsl/EntityDescription/ServiceRelationEntityDescription.java
@@ -19,14 +19,14 @@
 package org.apache.skywalking.oap.meter.analyzer.dsl.EntityDescription;
 
 import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
+import com.google.common.collect.ImmutableList;
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 import lombok.ToString;
 import org.apache.skywalking.oap.server.core.analysis.Layer;
 import org.apache.skywalking.oap.server.core.analysis.meter.ScopeType;
 import org.apache.skywalking.oap.server.core.source.DetectPoint;
+import org.apache.skywalking.oap.server.library.util.StringUtil;
 
 @Getter
 @RequiredArgsConstructor
@@ -38,9 +38,16 @@ public class ServiceRelationEntityDescription implements 
EntityDescription {
     private final DetectPoint detectPoint;
     private final Layer layer;
     private final String delimiter;
+    private final String componentIdKey;
 
     @Override
     public List<String> getLabelKeys() {
-        return Stream.concat(this.sourceServiceKeys.stream(), 
this.destServiceKeys.stream()).collect(Collectors.toList());
+        final ImmutableList.Builder<String> builder = 
ImmutableList.<String>builder()
+            .addAll(this.sourceServiceKeys)
+            .addAll(this.destServiceKeys);
+        if (StringUtil.isNotEmpty(componentIdKey)) {
+            builder.add(componentIdKey);
+        }
+        return builder.build();
     }
 }
diff --git 
a/oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/dsl/SampleFamily.java
 
b/oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/dsl/SampleFamily.java
index 25f7e8a75c..638429ae5e 100644
--- 
a/oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/dsl/SampleFamily.java
+++ 
b/oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/dsl/SampleFamily.java
@@ -510,7 +510,22 @@ public class SampleFamily {
         if (this == EMPTY) {
             return EMPTY;
         }
-        return createMeterSamples(new 
ServiceRelationEntityDescription(sourceServiceKeys, destServiceKeys, 
detectPoint, layer, Const.POINT));
+        return createMeterSamples(new 
ServiceRelationEntityDescription(sourceServiceKeys, destServiceKeys, 
detectPoint, layer, Const.POINT, null));
+    }
+
+    public SampleFamily serviceRelation(DetectPoint detectPoint, List<String> 
sourceServiceKeys, List<String> destServiceKeys, String delimiter, Layer layer, 
String componentIdKey) {
+        Preconditions.checkArgument(sourceServiceKeys.size() > 0);
+        Preconditions.checkArgument(destServiceKeys.size() > 0);
+        ExpressionParsingContext.get().ifPresent(ctx -> {
+            ctx.scopeType = ScopeType.SERVICE_RELATION;
+            ctx.scopeLabels.addAll(sourceServiceKeys);
+            ctx.scopeLabels.addAll(destServiceKeys);
+            ctx.scopeLabels.add(componentIdKey);
+        });
+        if (this == EMPTY) {
+            return EMPTY;
+        }
+        return createMeterSamples(new 
ServiceRelationEntityDescription(sourceServiceKeys, destServiceKeys, 
detectPoint, layer, delimiter, componentIdKey));
     }
 
     public SampleFamily forEach(List<String> array, Closure<Void> each) {
@@ -701,10 +716,13 @@ public class SampleFamily {
                     );
                 case SERVICE_RELATION:
                     ServiceRelationEntityDescription 
serviceRelationEntityDescription = (ServiceRelationEntityDescription) 
entityDescription;
+                    final String serviceRelationComponentValue = 
InternalOps.dim(samples,
+                        
Collections.singletonList(serviceRelationEntityDescription.getComponentIdKey()),
 serviceRelationEntityDescription.getDelimiter());
+                    int serviceRelationComponentId = 
StringUtil.isNotEmpty(serviceRelationComponentValue) ? 
Integer.parseInt(serviceRelationComponentValue) : 0;
                     return MeterEntity.newServiceRelation(
                         InternalOps.dim(samples, 
serviceRelationEntityDescription.getSourceServiceKeys(), 
serviceRelationEntityDescription.getDelimiter()),
                         InternalOps.dim(samples, 
serviceRelationEntityDescription.getDestServiceKeys(), 
serviceRelationEntityDescription.getDelimiter()),
-                        serviceRelationEntityDescription.getDetectPoint(), 
serviceRelationEntityDescription.getLayer()
+                        serviceRelationEntityDescription.getDetectPoint(), 
serviceRelationEntityDescription.getLayer(), serviceRelationComponentId
                     );
                 case PROCESS_RELATION:
                     final ProcessRelationEntityDescription 
processRelationEntityDescription = (ProcessRelationEntityDescription) 
entityDescription;
diff --git 
a/oap-server/analyzer/meter-analyzer/src/test/java/org/apache/skywalking/oap/meter/analyzer/dsl/ScopeTest.java
 
b/oap-server/analyzer/meter-analyzer/src/test/java/org/apache/skywalking/oap/meter/analyzer/dsl/ScopeTest.java
index a3117ddeb6..040b4e025e 100644
--- 
a/oap-server/analyzer/meter-analyzer/src/test/java/org/apache/skywalking/oap/meter/analyzer/dsl/ScopeTest.java
+++ 
b/oap-server/analyzer/meter-analyzer/src/test/java/org/apache/skywalking/oap/meter/analyzer/dsl/ScopeTest.java
@@ -494,7 +494,7 @@ public class ScopeTest {
                 new HashMap<MeterEntity, Sample[]>() {
                     {
                         put(
-                            MeterEntity.newServiceRelation("productpage", 
"details", DetectPoint.CLIENT, Layer.GENERAL),
+                            MeterEntity.newServiceRelation("productpage", 
"details", DetectPoint.CLIENT, Layer.GENERAL, 0),
                             new Sample[] {
                                 Sample.builder()
                                       .labels(of())
@@ -503,7 +503,7 @@ public class ScopeTest {
                             }
                         );
                         put(
-                            MeterEntity.newServiceRelation("productpage", 
"reviews", DetectPoint.CLIENT, Layer.GENERAL),
+                            MeterEntity.newServiceRelation("productpage", 
"reviews", DetectPoint.CLIENT, Layer.GENERAL, 0),
                             new Sample[] {
                                 Sample.builder()
                                       .labels(of())
@@ -514,6 +514,45 @@ public class ScopeTest {
                     }
                 }
             },
+            {
+                "sum_service_relation_with_component",
+                of("envoy_cluster_metrics_up_cx_active", 
SampleFamilyBuilder.newBuilder(
+                    Sample.builder()
+                        .labels(of("app", "productpage", "cluster_name", 
"details", "env", "test", "component", "10"))
+                        .value(11)
+                        .name("envoy_cluster_metrics_up_cx_active")
+                        .build(),
+                    Sample.builder()
+                        .labels(of("app", "productpage", "cluster_name", 
"reviews", "env", "test", "component", "10"))
+                        .value(16)
+                        .name("envoy_cluster_metrics_up_cx_active")
+                        .build()
+                ).build()),
+                "envoy_cluster_metrics_up_cx_active.sum(['app' 
,'cluster_name', 'env', 'component']).serviceRelation(DetectPoint.CLIENT, 
['app', 'env'], ['cluster_name'], '-', Layer.GENERAL, 'component')",
+                false,
+                new HashMap<MeterEntity, Sample[]>() {
+                    {
+                        put(
+                            MeterEntity.newServiceRelation("productpage-test", 
"details", DetectPoint.CLIENT, Layer.GENERAL, 10),
+                            new Sample[] {
+                                Sample.builder()
+                                    .labels(of())
+                                    .value(11)
+                                    
.name("envoy_cluster_metrics_up_cx_active").build()
+                            }
+                        );
+                        put(
+                            MeterEntity.newServiceRelation("productpage-test", 
"reviews", DetectPoint.CLIENT, Layer.GENERAL, 10),
+                            new Sample[] {
+                                Sample.builder()
+                                    .labels(of())
+                                    .value(16)
+                                    
.name("envoy_cluster_metrics_up_cx_active").build()
+                            }
+                        );
+                    }
+                }
+            },
             {
                 "sum_process_relation",
                 of("rover_network_profiling_process_write_bytes", 
SampleFamilyBuilder.newBuilder(
diff --git 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/MeterEntity.java
 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/MeterEntity.java
index 03961eff3e..ef4aed500c 100644
--- 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/MeterEntity.java
+++ 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/MeterEntity.java
@@ -154,13 +154,14 @@ public class MeterEntity {
 
     public static MeterEntity newServiceRelation(String sourceServiceName,
                                                  String destServiceName,
-                                                 DetectPoint detectPoint, 
Layer layer) {
+                                                 DetectPoint detectPoint, 
Layer layer, int componentId) {
         final MeterEntity meterEntity = new MeterEntity();
         meterEntity.scopeType = ScopeType.SERVICE_RELATION;
         meterEntity.sourceServiceName = 
NAMING_CONTROL.formatServiceName(sourceServiceName);
         meterEntity.destServiceName = 
NAMING_CONTROL.formatServiceName(destServiceName);
         meterEntity.detectPoint = detectPoint;
         meterEntity.layer = layer;
+        meterEntity.componentId = componentId;
         return meterEntity;
     }
 

Reply via email to