diff --git a/CHANGES.md b/CHANGES.md index 97231e226..2f55097f1 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 91ea490bd..a5d6f27ec 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 3526f6639..941ece6d4 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 48e3d49e9..1381090a2 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 12534de0e..64d34aebe 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 43737d7fc..04ad51154 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 tokens = Splitter.on('.').omitEmptyStrings().splitToList(property); + + StringBuilder tokenBuffer = new StringBuilder(); + List 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 581f5c14a..7dd198151 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 lb) { return new V1Pod() { @Override public V1ObjectMeta getMetadata() { return new V1ObjectMeta() { @Override public Map 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 dc4c210ae..8a38c18f5 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 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 0177de819..9e5872211 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}