From 8ff67d09e90712341c7889e0e6f4dc9506f4ad16 Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Fri, 8 Dec 2023 22:02:17 +0800 Subject: [PATCH] Allow excluding ServiceEntries in some namespaces when looking up ServiceEntries (#11640) --- docs/en/changes/changes.md | 2 ++ docs/en/setup/backend/configuration-vocabulary.md | 1 + .../server/receiver/envoy/EnvoyMetricReceiverConfig.java | 8 ++++++++ .../envoy/als/istio/IstioServiceEntryRegistry.java | 3 +++ .../server-starter/src/main/resources/application.yml | 6 ++++++ 5 files changed, 20 insertions(+) diff --git a/docs/en/changes/changes.md b/docs/en/changes/changes.md index 2e245bd334..1498f81d56 100644 --- a/docs/en/changes/changes.md +++ b/docs/en/changes/changes.md @@ -9,6 +9,8 @@ * Remove unreasonable default configurations for gRPC thread executor. * Remove `gRPCThreadPoolQueueSize (SW_RECEIVER_GRPC_POOL_QUEUE_SIZE)` configuration. +* Allow excluding ServiceEntries in some namespaces when looking up + ServiceEntries as a final resolution method of service metadata. #### UI * Fix the mismatch between the unit and calculation of the "Network Bandwidth Usage" widget in Linux-Service Dashboard. diff --git a/docs/en/setup/backend/configuration-vocabulary.md b/docs/en/setup/backend/configuration-vocabulary.md index ceec116d0e..c9a7c301a9 100644 --- a/docs/en/setup/backend/configuration-vocabulary.md +++ b/docs/en/setup/backend/configuration-vocabulary.md @@ -191,6 +191,7 @@ The Configuration Vocabulary lists all available configurations provided by `app | - | - | alsTCPAnalysis | Starts Envoy TCP Access Log Service analysis. Value = `k8s-mesh` means starting the analysis. | SW_ENVOY_METRIC_ALS_TCP_ANALYSIS | - | | - | - | k8sServiceNameRule | `k8sServiceNameRule` allows you to customize the service name in ALS via Kubernetes metadata. The available variables are `pod` and `service`. E.g. you can use `${service.metadata.name}-${pod.metadata.labels.version}` to append the version number to the service name. Note that when using environment variables to pass this configuration, use single quotes(`''`) to avoid being evaluated by the shell. | K8S_SERVICE_NAME_RULE | ${pod.metadata.labels.(service.istio.io/canonical-name)} | | - | - | istioServiceNameRule | `istioServiceNameRule` allows you to customize the service name in ALS via Kubernetes metadata. The available variables are `serviceEntry`. E.g. you can use `${serviceEntry.metadata.name}-${serviceEntry.metadata.labels.version}` to append the version number to the service name. Note that when using environment variables to pass this configuration, use single quotes(`''`) to avoid being evaluated by the shell. | ISTIO_SERVICE_NAME_RULE | ${serviceEntry.metadata.name} | +| - | - | istioServiceEntryIgnoredNamespaces | When looking up service informations from the Istio ServiceEntries, some of the ServiceEntries might be created in several namespaces automatically by some components, and OAP will randomly pick one of them to build the service name, users can use this config to exclude ServiceEntries that they don't want to be used. Comma separated. | SW_ISTIO_SERVICE_ENTRY_IGNORED_NAMESPACES | - | | receiver-otel | default | A receiver for analyzing metrics data from OpenTelemetry. | - | - | | | - | - | enabledHandlers | Enabled handlers for otel. | SW_OTEL_RECEIVER_ENABLED_HANDLERS | - | | - | - | enabledOtelMetricsRules | Enabled metric rules for OTLP handler. | SW_OTEL_RECEIVER_ENABLED_OTEL_METRICS_RULES | - | diff --git a/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/EnvoyMetricReceiverConfig.java b/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/EnvoyMetricReceiverConfig.java index fd34c808b8..0ef2d77429 100644 --- a/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/EnvoyMetricReceiverConfig.java +++ b/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/EnvoyMetricReceiverConfig.java @@ -18,12 +18,14 @@ package org.apache.skywalking.oap.server.receiver.envoy; +import com.google.common.base.Splitter; import com.google.common.base.Strings; import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import lombok.Getter; import org.apache.skywalking.oap.meter.analyzer.prometheus.rule.Rule; @@ -41,6 +43,7 @@ public class EnvoyMetricReceiverConfig extends ModuleConfig { private String k8sServiceNameRule; @Getter private String istioServiceNameRule; + private String istioServiceEntryIgnoredNamespaces; private final ServiceMetaInfoFactory serviceMetaInfoFactory = new ServiceMetaInfoFactoryImpl(); @Getter @@ -71,4 +74,9 @@ public class EnvoyMetricReceiverConfig extends ModuleConfig { public ServiceMetaInfoFactory serviceMetaInfoFactory() { return serviceMetaInfoFactory; } + + public Set getIstioServiceEntryIgnoredNamespaces() { + final var s = Strings.nullToEmpty(istioServiceEntryIgnoredNamespaces); + return Splitter.on(",").omitEmptyStrings().trimResults().splitToStream(s).collect(Collectors.toSet()); + } } diff --git a/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/istio/IstioServiceEntryRegistry.java b/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/istio/IstioServiceEntryRegistry.java index 47da033887..a525c4fea5 100644 --- a/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/istio/IstioServiceEntryRegistry.java +++ b/oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/istio/IstioServiceEntryRegistry.java @@ -58,6 +58,8 @@ public class IstioServiceEntryRegistry { serviceNameFormatter = new ServiceNameFormatter(config.getIstioServiceNameRule()); + final var ignoredNamespaces = config.getIstioServiceEntryIgnoredNamespaces(); + final var cacheBuilder = CacheBuilder.newBuilder().expireAfterWrite(Duration.ofMinutes(3)); ipServiceMetaInfoMap = cacheBuilder.build(new CacheLoader<>() { @@ -69,6 +71,7 @@ public class IstioServiceEntryRegistry { .parallelStream() .filter(se -> se.getMetadata() != null) .filter(se -> se.getSpec() != null) + .filter(se -> !ignoredNamespaces.contains(se.getMetadata().getNamespace())) .filter(se -> { final var spec = se.getSpec(); switch (spec.getResolution()) { diff --git a/oap-server/server-starter/src/main/resources/application.yml b/oap-server/server-starter/src/main/resources/application.yml index 26d345372d..17681d8539 100644 --- a/oap-server/server-starter/src/main/resources/application.yml +++ b/oap-server/server-starter/src/main/resources/application.yml @@ -316,6 +316,12 @@ envoy-metric: # 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:"${pod.metadata.labels.(service.istio.io/canonical-name)}"} istioServiceNameRule: ${ISTIO_SERVICE_NAME_RULE:"${serviceEntry.metadata.name}"} + # When looking up service informations from the Istio ServiceEntries, some + # of the ServiceEntries might be created in several namespaces automatically + # by some components, and OAP will randomly pick one of them to build the + # service name, users can use this config to exclude ServiceEntries that + # they don't want to be used. Comma separated. + istioServiceEntryIgnoredNamespaces: ${SW_ISTIO_SERVICE_ENTRY_IGNORED_NAMESPACES:""} kafka-fetcher: selector: ${SW_KAFKA_FETCHER:-}