mumrah commented on code in PR #15622:
URL: https://github.com/apache/kafka/pull/15622#discussion_r1895787850


##########
metadata/src/main/java/org/apache/kafka/controller/EventPerformanceMonitor.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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.kafka.controller;
+
+import org.apache.kafka.common.utils.LogContext;
+
+import org.slf4j.Logger;
+
+import java.text.DecimalFormat;
+import java.util.AbstractMap;
+import java.util.Map;
+
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+
+/**
+ * Track the performance of controller events. Periodically log the slowest 
events.
+ * Log any event slower than a certain threshold.
+ */
+class EventPerformanceMonitor {
+    /**
+     * The format to use when displaying milliseconds.
+     */
+    private static final DecimalFormat MILLISECOND_DECIMAL_FORMAT = new 
DecimalFormat("#0.00");
+
+    static class Builder {
+        LogContext logContext = null;
+        long periodNs = SECONDS.toNanos(60);
+        long alwaysLogThresholdNs = SECONDS.toNanos(2);
+
+        Builder setLogContext(LogContext logContext) {
+            this.logContext = logContext;
+            return this;
+        }
+
+        Builder setPeriodNs(long periodNs) {
+            this.periodNs = periodNs;
+            return this;
+        }
+
+        Builder setAlwaysLogThresholdNs(long alwaysLogThresholdNs) {
+            this.alwaysLogThresholdNs = alwaysLogThresholdNs;
+            return this;
+        }
+
+        EventPerformanceMonitor build() {
+            if (logContext == null) logContext = new LogContext();
+            return new EventPerformanceMonitor(logContext,
+                    periodNs,
+                    alwaysLogThresholdNs);
+        }
+    }
+
+    /**
+     * The log4j object to use.
+     */
+    private final Logger log;
+
+    /**
+     * The period in nanoseconds.
+     */
+    private long periodNs;
+
+    /**
+     * The always-log threshold in nanoseconds.
+     */
+    private long alwaysLogThresholdNs;
+
+    /**
+     * The name of the slowest event we've seen so far, or null if none has 
been seen.
+     */
+    private String slowestEventName;
+
+    /**
+     * The duration of the slowest event we've seen so far, or 0 if none has 
been seen.
+     */
+    private long slowestEventDurationNs;
+
+    /**
+     * The total duration of all the events we've seen.
+     */
+    private long totalEventDurationNs;

Review Comment:
   Recording this is interesting. Since we have a single controller thread and 
can see its total event duration over a fixed period, we could calculate the 
idle percentage of the controller. Might be interesting to look at as a high 
level "busy" metric



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

Reply via email to