You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2021/03/31 04:09:38 UTC

[skywalking] branch master updated: Resolve envoy service name from a different position (#6654)

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

wusheng 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 7d06c21  Resolve envoy service name from a different position (#6654)
7d06c21 is described below

commit 7d06c210aec075edc22bfe2c70235a0eefb299fc
Author: Gao Hongtao <ha...@gmail.com>
AuthorDate: Wed Mar 31 12:09:18 2021 +0800

    Resolve envoy service name from a different position (#6654)
---
 CHANGES.md                                         |  1 +
 .../src/main/resources/application.yml             |  2 +-
 .../main/resources/metadata-service-mapping.yaml   |  2 +-
 .../oap/server/library/util/ResourceUtils.java     |  3 ++-
 .../envoy/als/k8s/ServiceNameFormatter.java        |  2 +-
 .../server/receiver/envoy/als/mx/FieldsHelper.java | 22 +++++++++++++++++++++-
 .../envoy/als/k8s/ServiceNameFormatterTest.java    | 17 +++++++++--------
 .../receiver/envoy/als/mx/FieldsHelperTest.java    |  8 ++++----
 .../test/resources/metadata-service-mapping.yaml   |  2 +-
 9 files changed, 41 insertions(+), 18 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 97231e2..2f55097 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -63,6 +63,7 @@ Release Notes.
 * Optimize the self monitoring grafana dashboard.
 * Enhance the export service.
 * Add function `retagByK8sMeta` and opt type `K8sRetagType.Pod2Service` in MAL for k8s to relate pods and services.
+* Using "service.istio.io/canonical-name" to replace "app" label to resolve Envoy ALS service name
 
 #### UI
 * Update selector scroller to show in all pages.
diff --git a/oap-server/server-bootstrap/src/main/resources/application.yml b/oap-server/server-bootstrap/src/main/resources/application.yml
index 91ea490..a5d6f27 100755
--- a/oap-server/server-bootstrap/src/main/resources/application.yml
+++ b/oap-server/server-bootstrap/src/main/resources/application.yml
@@ -327,7 +327,7 @@ envoy-metric:
     # the available variables are `pod`, `service`, f.e., you can use `${service.metadata.name}-${pod.metadata.labels.version}`
     # to append the version number to the service name.
     # Be careful, when using environment variables to pass this configuration, use single quotes(`''`) to avoid it being evaluated by the shell.
-    k8sServiceNameRule: ${K8S_SERVICE_NAME_RULE:"${service.metadata.name}"}
+    k8sServiceNameRule: ${K8S_SERVICE_NAME_RULE:"${pod.metadata.labels.(service.istio.io/canonical-name)}"}
 
 prometheus-fetcher:
   selector: ${SW_PROMETHEUS_FETCHER:-}
diff --git a/oap-server/server-bootstrap/src/main/resources/metadata-service-mapping.yaml b/oap-server/server-bootstrap/src/main/resources/metadata-service-mapping.yaml
index 3526f66..941ece6 100644
--- a/oap-server/server-bootstrap/src/main/resources/metadata-service-mapping.yaml
+++ b/oap-server/server-bootstrap/src/main/resources/metadata-service-mapping.yaml
@@ -13,5 +13,5 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-serviceName: ${LABELS.app}
+serviceName: ${LABELS."service.istio.io/canonical-name"}
 serviceInstanceName: ${NAME}
diff --git a/oap-server/server-library/library-util/src/main/java/org/apache/skywalking/oap/server/library/util/ResourceUtils.java b/oap-server/server-library/library-util/src/main/java/org/apache/skywalking/oap/server/library/util/ResourceUtils.java
index 48e3d49..1381090 100644
--- a/oap-server/server-library/library-util/src/main/java/org/apache/skywalking/oap/server/library/util/ResourceUtils.java
+++ b/oap-server/server-library/library-util/src/main/java/org/apache/skywalking/oap/server/library/util/ResourceUtils.java
@@ -48,7 +48,8 @@ public class ResourceUtils {
         if (url == null) {
             throw new FileNotFoundException("path not found: " + path);
         }
-        return Objects.requireNonNull(new File(url.getPath()).listFiles(), "No files in " + path);
+        return Arrays.stream(Objects.requireNonNull(new File(url.getPath()).listFiles(), "No files in " + path))
+                     .filter(File::isFile).toArray(File[]::new);
     }
 
     public static File[] getPathFiles(String parentPath, String[] fileNames) throws FileNotFoundException {
diff --git a/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/k8s/ServiceNameFormatter.java b/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/k8s/ServiceNameFormatter.java
index 12534de..64d34ae 100644
--- a/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/k8s/ServiceNameFormatter.java
+++ b/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/k8s/ServiceNameFormatter.java
@@ -34,7 +34,7 @@ public class ServiceNameFormatter {
     private final StringBuffer serviceNamePattern;
 
     public ServiceNameFormatter(String rule) {
-        rule = StringUtils.defaultIfBlank(rule, "${service.metadata.name}");
+        rule = StringUtils.defaultIfBlank(rule, "${pod.metadata.labels.(service.istio.io/canonical-name)}");
 
         this.properties = new ArrayList<>();
         this.serviceNamePattern = new StringBuffer();
diff --git a/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/mx/FieldsHelper.java b/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/mx/FieldsHelper.java
index 43737d7..04ad511 100644
--- a/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/mx/FieldsHelper.java
+++ b/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/mx/FieldsHelper.java
@@ -39,6 +39,9 @@ import org.apache.skywalking.oap.server.receiver.envoy.als.ServiceMetaInfo;
 import org.yaml.snakeyaml.Yaml;
 
 @Slf4j
+/**
+ * FieldsHelper
+ */
 public enum FieldsHelper {
     SINGLETON;
 
@@ -82,7 +85,24 @@ public enum FieldsHelper {
             final StringBuffer serviceNamePattern = new StringBuffer();
             while (m.find()) {
                 final String property = m.group("property");
-                flatBuffersFieldNames.add(Splitter.on('.').omitEmptyStrings().splitToList(property));
+                List<String> tokens = Splitter.on('.').omitEmptyStrings().splitToList(property);
+
+                StringBuilder tokenBuffer = new StringBuilder();
+                List<String> compactedTokens = new ArrayList<>(tokens.size());
+                for (String token : tokens) {
+                    if (tokenBuffer.length() == 0 && token.startsWith("\"")) {
+                       tokenBuffer.append(token);
+                    } else if (tokenBuffer.length() > 0) {
+                        tokenBuffer.append(".").append(token);
+                        if (token.endsWith("\"")) {
+                            compactedTokens.add(tokenBuffer.toString().replaceAll("\"", ""));
+                            tokenBuffer.setLength(0);
+                        }
+                    } else {
+                        compactedTokens.add(token);
+                    }
+                }
+                flatBuffersFieldNames.add(compactedTokens);
                 m.appendReplacement(serviceNamePattern, "%s");
             }
 
diff --git a/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/test/java/org/apache/skywalking/oap/server/receiver/envoy/als/k8s/ServiceNameFormatterTest.java b/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/test/java/org/apache/skywalking/oap/server/receiver/envoy/als/k8s/ServiceNameFormatterTest.java
index 581f5c1..7dd1981 100644
--- a/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/test/java/org/apache/skywalking/oap/server/receiver/envoy/als/k8s/ServiceNameFormatterTest.java
+++ b/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/test/java/org/apache/skywalking/oap/server/receiver/envoy/als/k8s/ServiceNameFormatterTest.java
@@ -28,6 +28,7 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
 
+import static com.google.common.collect.ImmutableSortedMap.of;
 import static junit.framework.TestCase.assertEquals;
 
 @RequiredArgsConstructor
@@ -40,22 +41,22 @@ public class ServiceNameFormatterTest {
         return new Case[] {
             new Case(
                 null,
-                ImmutableMap.of("service", service("Clash")),
+                ImmutableMap.of("pod", pod(of("service.istio.io/canonical-name", "Clash"))),
                 "Clash"
             ),
             new Case(
                 null,
-                ImmutableMap.of("service", service("ClashX"), "pod", pod("version", "v1")),
+                ImmutableMap.of("pod", pod(of("service.istio.io/canonical-name", "ClashX", "service.istio.io/canonical-revision", "v1"))),
                 "ClashX"
             ),
             new Case(
-                "${service.metadata.name}-${pod.metadata.labels.version}",
-                ImmutableMap.of("service", service("Clash"), "pod", pod("version", "v1beta")),
+                "${pod.metadata.labels.(service.istio.io/canonical-name)}-${pod.metadata.labels.(service.istio.io/canonical-revision)}",
+                ImmutableMap.of("pod", pod(of("service.istio.io/canonical-name", "Clash", "service.istio.io/canonical-revision", "v1beta"))),
                 "Clash-v1beta"
             ),
             new Case(
-                "${pod.metadata.labels.app}",
-                ImmutableMap.of("service", service("Clash"), "pod", pod("app", "ClashX-alpha")),
+                "${pod.metadata.labels.(service.istio.io/canonical-name)}",
+                ImmutableMap.of("service", service("Clash"), "pod", pod(of("service.istio.io/canonical-name", "ClashX-alpha"))),
                 "ClashX-alpha"
             )
         };
@@ -80,14 +81,14 @@ public class ServiceNameFormatterTest {
         };
     }
 
-    static V1Pod pod(final String label, final String value) {
+    static V1Pod pod(ImmutableMap<String, String> lb) {
         return new V1Pod() {
             @Override
             public V1ObjectMeta getMetadata() {
                 return new V1ObjectMeta() {
                     @Override
                     public Map<String, String> getLabels() {
-                        return ImmutableMap.of(label, value);
+                        return lb;
                     }
                 };
             }
diff --git a/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/test/java/org/apache/skywalking/oap/server/receiver/envoy/als/mx/FieldsHelperTest.java b/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/test/java/org/apache/skywalking/oap/server/receiver/envoy/als/mx/FieldsHelperTest.java
index dc4c210..8a38c18 100644
--- a/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/test/java/org/apache/skywalking/oap/server/receiver/envoy/als/mx/FieldsHelperTest.java
+++ b/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/test/java/org/apache/skywalking/oap/server/receiver/envoy/als/mx/FieldsHelperTest.java
@@ -51,22 +51,22 @@ public class FieldsHelperTest {
     public static Collection<Object[]> data() {
         return Arrays.asList(new Object[][] {
             {
-                "serviceName: ${LABELS.app}\nserviceInstanceName: ${NAME}",
+                "serviceName: ${LABELS.\"service.istio.io/canonical-name\"}\nserviceInstanceName: ${NAME}",
                 "productpage",
                 "productpage-v1-65576bb7bf-4mzsp"
             },
             {
-                "serviceName: ${LABELS.app}-${LABELS.version}\nserviceInstanceName: ${NAME}.${NAMESPACE}",
+                "serviceName: ${LABELS.\"service.istio.io/canonical-name\"}-${LABELS.version}\nserviceInstanceName: ${NAME}.${NAMESPACE}",
                 "productpage-v1",
                 "productpage-v1-65576bb7bf-4mzsp.default"
             },
             {
-                "serviceName: ${LABELS.app}-${CLUSTER_ID}\nserviceInstanceName: ${NAME}.${NAMESPACE}.${SERVICE_ACCOUNT}",
+                "serviceName: ${LABELS.\"service.istio.io/canonical-name\"}-${CLUSTER_ID}\nserviceInstanceName: ${NAME}.${NAMESPACE}.${SERVICE_ACCOUNT}",
                 "productpage-Kubernetes",
                 "productpage-v1-65576bb7bf-4mzsp.default.bookinfo-productpage"
             },
             {
-                "serviceName: fixed-${LABELS.app}\nserviceInstanceName: yeah_${NAME}",
+                "serviceName: fixed-${LABELS.\"service.istio.io/canonical-name\"}\nserviceInstanceName: yeah_${NAME}",
                 "fixed-productpage",
                 "yeah_productpage-v1-65576bb7bf-4mzsp"
             }
diff --git a/test/e2e/e2e-test/src/test/resources/metadata-service-mapping.yaml b/test/e2e/e2e-test/src/test/resources/metadata-service-mapping.yaml
index 0177de8..9e58722 100644
--- a/test/e2e/e2e-test/src/test/resources/metadata-service-mapping.yaml
+++ b/test/e2e/e2e-test/src/test/resources/metadata-service-mapping.yaml
@@ -13,5 +13,5 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-serviceName: e2e::${LABELS.app}
+serviceName: e2e::${LABELS."service.istio.io/canonical-name"}
 serviceInstanceName: ${NAME}