wankai123 commented on a change in pull request #6608:
URL: https://github.com/apache/skywalking/pull/6608#discussion_r601022892



##########
File path: docs/en/concepts-and-designs/mal.md
##########
@@ -57,6 +57,25 @@ For example, this filters all instance_trace_count samples 
for values >= 33:
 ```
 instance_trace_count.valueGreaterEqual(33)
 ```
+### K8s
+MAL support add specific Labels to the samples that collect from K8s metrics 
collectors.
+This feature need OAP Server has the authority to access the K8s's `API 
Server`.
+
+#### k8sTagServiceByPodName
+`k8sTagServiceByPodName(podName,serviceName)`. Add a sevice Label to the 
sample that already has a pod name.

Review comment:
       done

##########
File path: 
oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/k8s/K8sInfoRegistry.java
##########
@@ -0,0 +1,233 @@
+/*
+ * 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.meter.analyzer.k8s;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import io.kubernetes.client.informer.ResourceEventHandler;
+import io.kubernetes.client.informer.SharedInformerFactory;
+import io.kubernetes.client.openapi.Configuration;
+import io.kubernetes.client.openapi.apis.CoreV1Api;
+import io.kubernetes.client.openapi.models.V1Endpoints;
+import io.kubernetes.client.openapi.models.V1EndpointsList;
+import io.kubernetes.client.openapi.models.V1ObjectMeta;
+import io.kubernetes.client.openapi.models.V1PodList;
+import io.kubernetes.client.util.Config;
+import io.kubernetes.client.openapi.ApiClient;
+import io.kubernetes.client.openapi.models.V1Pod;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oap.meter.analyzer.prometheus.rule.Rule;
+
+import static com.google.common.base.Strings.isNullOrEmpty;
+import static java.util.Objects.isNull;
+import static java.util.Optional.ofNullable;
+
+@Slf4j
+public class K8sInfoRegistry {
+
+    private final static K8sInfoRegistry INSTANCE = new K8sInfoRegistry();
+    private final AtomicBoolean isStarted = new AtomicBoolean(false);
+    private final List<String> matchedFunc = 
Arrays.asList(".k8sTagServiceByPodName");
+    private final Map<String/* ip */, V1Pod> ipPodMap = new 
ConcurrentHashMap<>();
+    private final Map<String/* ip */, String/* namespace:serviceName */> 
ipServiceMap = new ConcurrentHashMap<>();
+    private final Map<String/* podName */, String /* namespace:serviceName*/> 
podServiceMap = new ConcurrentHashMap<>();
+    private ExecutorService executor;
+
+    public static K8sInfoRegistry getInstance() {
+        return INSTANCE;
+    }
+
+    private void init() {
+        executor = Executors.newCachedThreadPool(
+            new ThreadFactoryBuilder()
+                .setNameFormat("K8sInfoRegistry-%d")
+                .setDaemon(true)
+                .build()
+        );
+    }
+
+    @SneakyThrows
+    public void start(Rule rule) {
+        if (isStarted.compareAndSet(false, true)) {
+            if (!matchFunc2Start(rule)) {
+                return;
+            }
+            init();
+            final ApiClient apiClient = Config.defaultClient();
+            apiClient.setHttpClient(apiClient.getHttpClient()
+                                             .newBuilder()
+                                             .readTimeout(0, TimeUnit.SECONDS)
+                                             .build());
+            Configuration.setDefaultApiClient(apiClient);
+
+            final CoreV1Api coreV1Api = new CoreV1Api();
+            final SharedInformerFactory factory = new 
SharedInformerFactory(executor);
+
+            listenEndpointsEvents(coreV1Api, factory);
+            listenPodEvents(coreV1Api, factory);
+            factory.startAllRegisteredInformers();
+        }
+    }
+
+    private boolean matchFunc2Start(Rule rule) {
+        for (String mf : matchedFunc) {
+            return rule.getMetricsRules().stream().anyMatch(r -> 
r.getExp().split(mf).length > 1);
+
+        }
+        return false;
+    }
+
+    private void listenEndpointsEvents(final CoreV1Api coreV1Api, final 
SharedInformerFactory factory) {
+        factory.sharedIndexInformerFor(
+            params -> coreV1Api.listEndpointsForAllNamespacesCall(
+                null,
+                null,
+                null,
+                null,
+                null,
+                null,
+                params.resourceVersion,
+                300,
+                params.watch,
+                null
+            ),
+            V1Endpoints.class,
+            V1EndpointsList.class
+        ).addEventHandler(new ResourceEventHandler<V1Endpoints>() {
+            @Override
+            public void onAdd(final V1Endpoints endpoints) {
+                addEndpoints(endpoints);
+            }
+
+            @Override
+            public void onUpdate(final V1Endpoints oldEndpoints, final 
V1Endpoints newEndpoints) {
+                addEndpoints(newEndpoints);
+            }
+
+            @Override
+            public void onDelete(final V1Endpoints endpoints, final boolean 
deletedFinalStateUnknown) {
+                removeEndpoints(endpoints);
+            }
+        });
+    }
+
+    private void listenPodEvents(final CoreV1Api coreV1Api, final 
SharedInformerFactory factory) {
+        factory.sharedIndexInformerFor(
+            params -> coreV1Api.listPodForAllNamespacesCall(
+                null,
+                null,
+                null,
+                null,
+                null,
+                null,
+                params.resourceVersion,
+                300,
+                params.watch,
+                null
+            ),
+            V1Pod.class,
+            V1PodList.class
+        ).addEventHandler(new ResourceEventHandler<V1Pod>() {
+            @Override
+            public void onAdd(final V1Pod pod) {
+                addPod(pod);
+            }
+
+            @Override
+            public void onUpdate(final V1Pod oldPod, final V1Pod newPod) {
+                addPod(newPod);
+            }
+
+            @Override
+            public void onDelete(final V1Pod pod, final boolean 
deletedFinalStateUnknown) {
+                removePod(pod);
+            }
+        });
+    }
+
+    private void addPod(final V1Pod pod) {
+        ofNullable(pod.getStatus()).ifPresent(
+            status -> ipPodMap.put(status.getPodIP(), pod)
+        );
+
+        recompose();
+    }
+
+    private void removePod(final V1Pod pod) {
+        ofNullable(pod.getStatus()).ifPresent(
+            status -> ipPodMap.remove(status.getPodIP())
+        );
+    }
+
+    private void addEndpoints(final V1Endpoints endpoints) {
+        V1ObjectMeta endpointsMetadata = endpoints.getMetadata();
+        if (isNull(endpointsMetadata)) {
+            log.error("Endpoints metadata is null: {}", endpoints);
+            return;
+        }
+
+        final String namespace = endpointsMetadata.getNamespace();
+        final String name = endpointsMetadata.getName();
+
+        ofNullable(endpoints.getSubsets()).ifPresent(subsets -> 
subsets.forEach(
+            subset -> ofNullable(subset.getAddresses()).ifPresent(addresses -> 
addresses.forEach(
+                address -> ipServiceMap.put(address.getIp(), namespace + ":" + 
name)
+            ))
+        ));
+
+        recompose();
+    }
+
+    private void removeEndpoints(final V1Endpoints endpoints) {
+        ofNullable(endpoints.getSubsets()).ifPresent(subsets -> 
subsets.forEach(
+            subset -> ofNullable(subset.getAddresses()).ifPresent(addresses -> 
addresses.forEach(
+                address -> ipServiceMap.remove(address.getIp())
+            ))
+        ));
+    }
+
+    private void recompose() {
+        ipPodMap.forEach((ip, pod) -> {
+            final String namespaceService = ipServiceMap.get(ip);
+            if (isNullOrEmpty(namespaceService)) {
+                return;
+            }
+
+            final V1ObjectMeta podMetadata = pod.getMetadata();
+            if (isNull(podMetadata)) {
+                log.warn("Pod metadata is null, {}", pod);
+                return;
+            }
+
+            podServiceMap.put(pod.getMetadata().getName(), namespaceService);

Review comment:
       ignore

##########
File path: docs/en/concepts-and-designs/mal.md
##########
@@ -57,6 +57,25 @@ For example, this filters all instance_trace_count samples 
for values >= 33:
 ```
 instance_trace_count.valueGreaterEqual(33)
 ```
+### K8s
+MAL support add specific Labels to the samples that collect from K8s metrics 
collectors.
+This feature need OAP Server has the authority to access the K8s's `API 
Server`.
+
+#### k8sTagServiceByPodName
+`k8sTagServiceByPodName(podName,serviceName)`. Add a sevice Label to the 
sample that already has a pod name.
+
+For example:
+```
+container_cpu_usage_seconds_total{container=my-nginx, cpu=total, 
pod=my-nginx-5dc4865748-mbczh} 2
+```
+Expression:
+```
+container_cpu_usage_seconds_total.k8sTagServiceByPodName('pod' , 'service')
+```
+Output:
+```
+container_cpu_usage_seconds_total{container=my-nginx, cpu=total, 
pod=my-nginx-5dc4865748-mbczh, service='default:nginx-service'} 2

Review comment:
       done

##########
File path: 
oap-server/analyzer/meter-analyzer/src/test/java/org/apache/skywalking/oap/meter/analyzer/dsl/K8sTagTest.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.meter.analyzer.dsl;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Arrays;
+import java.util.Collection;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oap.meter.analyzer.k8s.K8sInfoRegistry;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.mockito.Mockito;
+import org.powermock.reflect.Whitebox;
+
+import static com.google.common.collect.ImmutableMap.of;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.when;
+
+@Slf4j
+@RunWith(Parameterized.class)
+public class K8sTagTest {
+
+    @Parameterized.Parameter
+    public String name;
+
+    @Parameterized.Parameter(1)
+    public ImmutableMap<String, SampleFamily> input;
+
+    @Parameterized.Parameter(2)
+    public String expression;
+
+    @Parameterized.Parameter(3)
+    public Result want;
+
+    @Parameterized.Parameter(4)
+    public boolean isThrow;
+
+    @Parameterized.Parameters(name = "{index}: {0}")
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][] {
+            {

Review comment:
       done




-- 
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to