weiqingy commented on code in PR #28692:
URL: https://github.com/apache/flink/pull/28692#discussion_r3700641675


##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/operators/metrics/UdfMetricsTest.java:
##########
@@ -0,0 +1,118 @@
+/*
+ * 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.table.runtime.operators.metrics;
+
+import org.apache.flink.metrics.Counter;
+import org.apache.flink.metrics.Histogram;
+import org.apache.flink.metrics.ThreadSafeSimpleCounter;
+import org.apache.flink.metrics.testutils.MetricListener;
+import org.apache.flink.runtime.metrics.DescriptiveStatisticsHistogram;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link UdfMetrics}. */
+class UdfMetricsTest {
+
+    private MetricListener metricListener;
+
+    @BeforeEach
+    void setUp() {
+        metricListener = new MetricListener();
+    }
+
+    @Test
+    void sampleEveryNth() {
+        UdfMetrics metrics = register("myUdf", 100);
+
+        List<Integer> sampledCalls = new ArrayList<>();
+        for (int call = 1; call <= 300; call++) {
+            if (metrics.shouldSample()) {
+                sampledCalls.add(call);
+            }
+        }
+
+        // With interval 100 exactly one call in every 100 is sampled, on the 
1st, 101st, 201st.
+        assertThat(sampledCalls).containsExactly(1, 101, 201);
+    }
+
+    @Test
+    void sampleEveryCallWhenIntervalOne() {
+        UdfMetrics metrics = register("myUdf", 1);
+
+        for (int call = 0; call < 10; call++) {
+            assertThat(metrics.shouldSample()).isTrue();
+        }
+    }
+
+    @Test
+    void rejectsNonPositiveInterval() {
+        assertThatThrownBy(() -> register("myUdf", 
0)).isInstanceOf(IllegalArgumentException.class);
+        assertThatThrownBy(() -> register("myUdf", -1))
+                .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    void registrationNamesAndTypes() {
+        register("myUdf", 100);
+
+        assertThat(metricListener.getHistogram("udf", "myUdf", 
"udfProcessingTime"))
+                .get()
+                .isInstanceOf(DescriptiveStatisticsHistogram.class);
+        assertThat(metricListener.getCounter("udf", "myUdf", 
"udfExceptionCount"))
+                .get()
+                .isInstanceOf(ThreadSafeSimpleCounter.class);
+    }
+
+    @Test
+    void updateFeedsHistogram() {
+        UdfMetrics metrics = register("myUdf", 1);
+        Histogram histogram =
+                metricListener.getHistogram("udf", "myUdf", 
"udfProcessingTime").get();
+
+        metrics.update(10L);
+        metrics.update(20L);
+        metrics.update(30L);
+
+        assertThat(histogram.getCount()).isEqualTo(3L);
+        assertThat(histogram.getStatistics().getMin()).isEqualTo(10L);
+        assertThat(histogram.getStatistics().getMax()).isEqualTo(30L);
+    }
+
+    @Test
+    void markExceptionCounts() {
+        UdfMetrics metrics = register("myUdf", 1);
+        Counter counter = metricListener.getCounter("udf", "myUdf", 
"udfExceptionCount").get();
+
+        metrics.markException();
+        metrics.markException();
+
+        assertThat(counter.getCount()).isEqualTo(2L);
+    }
+

Review Comment:
   Thanks for taking a look.
   This PR was a draft reference implementation for the FLIP-485 vote. The FLIP 
passed on 2026-08-01, so the code is going up as four smaller PRs and this one 
is superseded. The file you commented on is now in #28878, which is open for 
review.
   Your point holds there. I added metricsAreIndependentPerUdf to 
UdfMetricsTest in #28878, which registers two UDFs, records on one, and asserts 
the other's histogram and counter stay at zero. I kept it to the separation 
check, since the min/max and counting assertions in your snippet are already 
covered by the existing updateFeedsHistogram and markExceptionCounts tests.
   



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