AnatolyPopov commented on code in PR #17025:
URL: https://github.com/apache/iceberg/pull/17025#discussion_r3559049353


##########
kafka-connect/kafka-connect/src/main/java/org/apache/iceberg/connect/channel/CoordinatorMetrics.java:
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.iceberg.connect.channel;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.function.Supplier;
+import org.apache.kafka.common.MetricName;
+import org.apache.kafka.common.metrics.Gauge;
+import org.apache.kafka.common.metrics.JmxReporter;
+import org.apache.kafka.common.metrics.KafkaMetricsContext;
+import org.apache.kafka.common.metrics.MetricConfig;
+import org.apache.kafka.common.metrics.Metrics;
+import org.apache.kafka.common.metrics.Sensor;
+import org.apache.kafka.common.metrics.stats.Avg;
+import org.apache.kafka.common.metrics.stats.CumulativeSum;
+import org.apache.kafka.common.metrics.stats.Max;
+import org.apache.kafka.common.utils.Time;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class CoordinatorMetrics implements AutoCloseable {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(CoordinatorMetrics.class);
+  private static final String GROUP = "coordinator-metrics";
+  private static final String NAMESPACE = "iceberg-kafka-connect-metrics";
+
+  private final Metrics metrics;
+  private final Sensor commitTime;
+  private final Sensor consumeTime;
+  private final Sensor startCommit;
+  private final Sensor commitComplete;
+
+  CoordinatorMetrics(
+      String connector, Supplier<Long> commitBufferSize, Supplier<Long> 
readyBufferSize) {
+    Map<String, String> tags = new LinkedHashMap<>();
+    tags.put("connector", connector);
+
+    Metrics newMetrics =
+        new Metrics(
+            new MetricConfig(),
+            Collections.singletonList(new JmxReporter()),
+            Time.SYSTEM,
+            new KafkaMetricsContext(NAMESPACE));
+    try {
+      this.commitTime =
+          createTimerSensor(
+              newMetrics, "commit-time", "Time spent in Coordinator.commit() 
in ms", tags);
+      this.consumeTime =
+          createTimerSensor(
+              newMetrics,
+              "consume-available-time",
+              "Time spent in Channel.consumeAvailable() (coordinator side) in 
ms",
+              tags);
+      this.startCommit =
+          createCounterSensor(
+              newMetrics, "start-commit", "Number of START_COMMIT events 
emitted", tags);
+      this.commitComplete =
+          createCounterSensor(
+              newMetrics, "commit-complete", "Number of COMMIT_COMPLETE events 
emitted", tags);
+
+      newMetrics.addMetric(
+          new MetricName(
+              "commit-buffer-size", GROUP, "Current size of 
CommitState.commitBuffer", tags),
+          (Gauge<Long>) (config, now) -> commitBufferSize.get());

Review Comment:
   @HenryCaiHaiying unless you are going to make these instance variables 
volatile nothing will be changed I think. And it will just bring additional 
complexity because they will have to be properly updated.
   I think commitBuffer.size() is safe enough because it returns a plain 
precomputed int value that is the same non-volatile int read and in the worst 
case it might be slightly stale.
   So I would propose then either keep it as it is since it's a best effort 
metric and slightly stale values do not change much OR make volatile instance 
variables if we really want a memory barrier. But in the latter case they need 
to be always carefully updated.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to