morhidi commented on code in PR #311: URL: https://github.com/apache/flink-kubernetes-operator/pull/311#discussion_r918277417
########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/metrics/lifecycle/LifecycleMetrics.java: ########## @@ -0,0 +1,205 @@ +/* + * 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.flink.kubernetes.operator.metrics.lifecycle; + +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.kubernetes.operator.crd.AbstractFlinkResource; +import org.apache.flink.kubernetes.operator.metrics.KubernetesOperatorMetricGroup; +import org.apache.flink.metrics.Histogram; +import org.apache.flink.metrics.MetricGroup; +import org.apache.flink.runtime.metrics.DescriptiveStatisticsHistogram; + +import lombok.RequiredArgsConstructor; +import lombok.ToString; + +import java.time.Clock; +import java.time.Instant; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; + +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.CREATED; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.DEPLOYED; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.ROLLED_BACK; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.ROLLING_BACK; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.STABLE; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.SUSPENDED; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.UPGRADING; + +/** + * Utility for tracking resource lifecycle metrics globally and per namespace. + * + * @param <CR> Flink resource type. + */ +public class LifecycleMetrics<CR extends AbstractFlinkResource<?, ?>> { + + public static final List<Transition> TRACKED_TRANSITIONS = getTrackedTransitions(); + + private final Map<Tuple2<String, String>, ResourceLifecycleMetricTracker> lifecycleTrackers = + new ConcurrentHashMap<>(); + private final Set<String> namespaces = Collections.newSetFromMap(new ConcurrentHashMap<>()); + + private final int histogramWindowSize = 1000; + private final Configuration configuration; + private final Clock clock; + private final KubernetesOperatorMetricGroup operatorMetricGroup; + + private Map<String, Tuple2<Histogram, Map<String, Histogram>>> transitionMetrics; + private Function<MetricGroup, MetricGroup> metricGroupFunction; + + public LifecycleMetrics( + Configuration configuration, + Clock clock, + KubernetesOperatorMetricGroup operatorMetricGroup) { + this.configuration = configuration; + this.clock = clock; + this.operatorMetricGroup = operatorMetricGroup; + } + + public void onUpdate(CR cr) { + getLifecycleMetricTracker(cr).onUpdate(cr.getStatus().getLifecycleState(), clock.instant()); + } + + public void onRemove(CR cr) { + lifecycleTrackers.remove( + Tuple2.of(cr.getMetadata().getNamespace(), cr.getMetadata().getName())); + } + + private ResourceLifecycleMetricTracker getLifecycleMetricTracker(CR cr) { + init(cr); + createNamespaceStateCountIfMissing(cr.getMetadata().getNamespace()); + return lifecycleTrackers.computeIfAbsent( + Tuple2.of(cr.getMetadata().getNamespace(), cr.getMetadata().getName()), + k -> { + var initialState = cr.getStatus().getLifecycleState(); + var time = + initialState == CREATED + ? Instant.parse(cr.getMetadata().getCreationTimestamp()) + : clock.instant(); + return new ResourceLifecycleMetricTracker( + initialState, time, getTransitionHistograms(cr)); + }); + } + + private void createNamespaceStateCountIfMissing(String namespace) { + if (!namespaces.add(namespace)) { + return; + } + + MetricGroup lifecycleGroup = + metricGroupFunction.apply( + operatorMetricGroup.createResourceNamespaceGroup(configuration, namespace)); + for (ResourceLifecycleState state : ResourceLifecycleState.values()) { + lifecycleGroup + .addGroup("State") + .addGroup(state.name()) + .gauge( + "Count", + () -> + lifecycleTrackers.values().stream() + .map(ResourceLifecycleMetricTracker::getCurrentState) + .filter(s -> s == state) + .count()); + } + } + + private synchronized void init(CR cr) { + if (transitionMetrics != null) { + return; + } + this.metricGroupFunction = Review Comment: We should follow this naming convention in other metrics too, i'll add this to my open PR. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
