You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "weiqingy (via GitHub)" <gi...@apache.org> on 2026/08/02 23:02:36 UTC
[PR] [FLINK-40292][table-runtime] Add UdfMetrics helper for UDF metrics [flink]
weiqingy opened a new pull request, #28878:
URL: https://github.com/apache/flink/pull/28878
## What is the purpose of the change
This is the first of four pull requests implementing [FLIP-485: Add UDF Metrics](https://cwiki.apache.org/confluence/spaces/FLINK/pages/373885706/FLIP-485+Add+UDF+Metrics), which was accepted on 2026-08-01 with 3 binding +1 and no -1 ([vote thread](https://lists.apache.org/thread/symqpswsohl2s5wmtkcw0jjp1w5dot0n)). It supersedes the single reference-implementation draft in #28692, which is now closed.
FLIP-485 adds opt-in, per-operator observability for SQL/Table user-defined functions so operators can see inside UDF "black boxes" when debugging latency or errors, and so autoscaling can get a reliable "the problem is in user code" signal.
This sub-task ([FLINK-40292](https://issues.apache.org/jira/browse/FLINK-40292)) adds only the shared runtime helper, `UdfMetrics`. It owns metric registration, the sampling decision, timing, and exception counting, and it is the piece that both the synchronous and the asynchronous instrumentation paths use. It has no caller yet. The first caller arrives in [FLINK-40293](https://issues.apache.org/jira/browse/FLINK-40293), which adds the configuration options and instruments synchronous scalar and table UDF calls. Splitting it out keeps the sampling and registration logic reviewable on its own.
The remaining PRs in the series:
2. [FLINK-40293](https://issues.apache.org/jira/browse/FLINK-40293): `table.exec.udf-metric-enabled` and `table.exec.udf-metric.sample-interval`, plus synchronous scalar and table instrumentation.
3. [FLINK-40294](https://issues.apache.org/jira/browse/FLINK-40294): asynchronous scalar and table instrumentation.
4. [FLINK-40295](https://issues.apache.org/jira/browse/FLINK-40295): documentation.
## Brief change log
- Add `UdfMetrics` in `flink-table-runtime` (`org.apache.flink.table.runtime.operators.metrics`, beside `SimpleGauge`). It registers `udfProcessingTime` and `udfExceptionCount` under `addGroup("udf", udfName)` on the operator metric group, so the full identifier is `<operator_name>.udf.<udf_name>.<metric>`.
- `udfProcessingTime` is a `DescriptiveStatisticsHistogram` recording per-invocation nanoseconds. It is sampled with the same counter-based scheme as state latency tracking (FLINK-21736): only every Nth invocation is timed, including the `interval == 1` "measure every call" case.
- `udfExceptionCount` is a `ThreadSafeSimpleCounter` and is incremented on every exception, not sampled.
- The histogram is safe to update from an async callback thread; the sampling counter is only advanced on the task thread at dispatch.
## Verifying this change
This change added tests and can be verified as follows:
- `UdfMetricsTest` covers metric registration and naming, the sampling decision across the interval boundary, the `sample-interval = 1` case, the counter reset boundary, timing recorded into the histogram, and exception counting.
## Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): no
- The public API, i.e., is any changed class annotated with `@Public(Evolving)`: no
- The serializers: no
- The runtime per-record code paths (performance sensitive): no. Nothing calls this class yet. The call sites, and their gating, arrive in PRs 2 and 3.
- Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
- The S3 file system connector: no
## Documentation
- Does this pull request introduce a new feature? yes, as the first step of FLIP-485
- If yes, how is the feature documented? JavaDocs here. The user-facing documentation in `docs/content{,.zh}/docs/ops/metrics.md` lands in [FLINK-40295](https://issues.apache.org/jira/browse/FLINK-40295).
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes (please specify the tool below)
Generated-by: Claude Code (Anthropic Claude Opus 4.8 and Claude Opus 5)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40292][table-runtime] Add UdfMetrics helper for UDF metrics [flink]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on code in PR #28878:
URL: https://github.com/apache/flink/pull/28878#discussion_r3755139512
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/metrics/UdfMetrics.java:
##########
@@ -0,0 +1,99 @@
+/*
+ * 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.MetricGroup;
+import org.apache.flink.metrics.ThreadSafeSimpleCounter;
+import org.apache.flink.runtime.metrics.DescriptiveStatisticsHistogram;
+import org.apache.flink.util.Preconditions;
+
+/**
+ * Per-operator metrics for a single user-defined function, registered under {@code
+ * <operator>.udf.<udfName>}: {@code udfProcessingTime} (a latency histogram) and {@code
+ * udfExceptionCount} (a counter of exceptions escaping the function).
+ *
+ * <p>Timing is sampled: only one invocation out of every {@code sampleInterval} is measured, so a
+ * hot function does not pay {@code System.nanoTime()} on every record. The sample decision advances
+ * a plain {@code int} and is only ever taken on the task thread, so it needs no synchronization.
+ * The two registered metrics are safe for the async completion thread to touch: the histogram
+ * synchronizes internally and the exception counter is {@link ThreadSafeSimpleCounter}.
+ */
+public final class UdfMetrics {
Review Comment:
Thanks for the clarify.
Just keep original naming.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40292][table-runtime] Add UdfMetrics helper for UDF metrics [flink]
Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #28878:
URL: https://github.com/apache/flink/pull/28878#discussion_r3755074265
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/metrics/UdfMetrics.java:
##########
@@ -0,0 +1,99 @@
+/*
+ * 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.MetricGroup;
+import org.apache.flink.metrics.ThreadSafeSimpleCounter;
+import org.apache.flink.runtime.metrics.DescriptiveStatisticsHistogram;
+import org.apache.flink.util.Preconditions;
+
+/**
+ * Per-operator metrics for a single user-defined function, registered under {@code
+ * <operator>.udf.<udfName>}: {@code udfProcessingTime} (a latency histogram) and {@code
+ * udfExceptionCount} (a counter of exceptions escaping the function).
+ *
+ * <p>Timing is sampled: only one invocation out of every {@code sampleInterval} is measured, so a
+ * hot function does not pay {@code System.nanoTime()} on every record. The sample decision advances
+ * a plain {@code int} and is only ever taken on the task thread, so it needs no synchronization.
+ * The two registered metrics are safe for the async completion thread to touch: the histogram
+ * synchronizes internally and the exception counter is {@link ThreadSafeSimpleCounter}.
+ */
+public final class UdfMetrics {
Review Comment:
Thanks for the review!
Flink seems to treat acronyms as words in class names: `AbstractUdfStreamOperator` and `UdfStreamOperatorFactory` in `flink-runtime`, and `TableAbstractCoUdfStreamOperator` right next door in `flink-table-runtime`. The wider pattern looks the same, `Sql` and `Json` and `Rest` outnumber the all-caps spellings in class names by a long way.
The all-caps ones I can find are the older `RuntimeUDFContext` family in `flink-core` and a couple of test fixtures.
Happy to rename if you'd still prefer it though. It's this class plus its two tests and a few codegen references.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40292][table-runtime] Add UdfMetrics helper for UDF metrics [flink]
Posted by "HuangZhenQiu (via GitHub)" <gi...@apache.org>.
HuangZhenQiu commented on code in PR #28878:
URL: https://github.com/apache/flink/pull/28878#discussion_r3762625048
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/metrics/UdfMetrics.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.MetricGroup;
+import org.apache.flink.metrics.ThreadSafeSimpleCounter;
+import org.apache.flink.runtime.metrics.DescriptiveStatisticsHistogram;
+import org.apache.flink.util.Preconditions;
+
+/**
+ * Per-operator metrics for a single user-defined function, registered under {@code
+ * <operator>.udf.<udfName>}: {@code udfProcessingTime} (a latency histogram) and {@code
+ * udfExceptionCount} (a counter of exceptions escaping the function).
+ *
+ * <p>Timing is sampled: only one invocation out of every {@code sampleInterval} is measured, so a
+ * hot function does not pay {@code System.nanoTime()} on every record. The sample decision advances
+ * a plain {@code int} and is only ever taken on the task thread, so it needs no synchronization.
+ * The two registered metrics are safe for the async completion thread to touch: the histogram
+ * synchronizes internally and the exception counter is {@link ThreadSafeSimpleCounter}.
+ */
+public final class UdfMetrics {
+
+ private static final int HISTORY_SIZE = 128;
+
+ private final int sampleInterval;
+ private final Histogram processingTime;
+ private final Counter exceptionCount;
+
+ // Sample counter; only touched on the task thread, hence a plain int with no synchronization.
+ private int invocationCount = 0;
+
+ private UdfMetrics(int sampleInterval, Histogram processingTime, Counter exceptionCount) {
+ this.sampleInterval = sampleInterval;
+ this.processingTime = processingTime;
+ this.exceptionCount = exceptionCount;
+ }
+
+ /**
+ * Registers the {@code udfProcessingTime} histogram and {@code udfExceptionCount} counter under
+ * {@code <operatorMetricGroup>.udf.<udfName>}.
+ *
+ * @param sampleInterval measure one invocation out of every {@code sampleInterval}; must be
+ * {@code >= 1} ({@code 1} measures every invocation)
+ */
+ public static UdfMetrics register(
+ MetricGroup operatorMetricGroup, String udfName, int sampleInterval) {
+ Preconditions.checkArgument(
+ sampleInterval >= 1,
+ "UDF metric sample interval must be >= 1, but was %s.",
+ sampleInterval);
+ MetricGroup group = operatorMetricGroup.addGroup("udf", udfName);
+ return new UdfMetrics(
+ sampleInterval,
+ group.histogram(
+ "udfProcessingTime", new DescriptiveStatisticsHistogram(HISTORY_SIZE)),
+ group.counter("udfExceptionCount", new ThreadSafeSimpleCounter()));
+ }
+
+ /**
+ * Returns {@code true} for the one invocation in every {@code sampleInterval} whose processing
+ * time should be measured. Must be called once per invocation, on the task thread only.
+ */
+ public boolean shouldSample() {
+ if (sampleInterval == 1) {
Review Comment:
Make sense. Thanks for the context.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40292][table-runtime] Add UdfMetrics helper for UDF metrics [flink]
Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on PR #28878:
URL: https://github.com/apache/flink/pull/28878#issuecomment-5160766593
FLIP-485 passed the vote on 2026-08-01 with 3 binding +1 and no -1: https://lists.apache.org/thread/symqpswsohl2s5wmtkcw0jjp1w5dot0n
This is the first of four PRs landing the accepted design. It replaces the single reference-implementation draft in #28692, which I'm closing.
This PR adds only the `UdfMetrics` helper and its unit test. It has no caller yet on purpose. The first caller arrives in PR 2, which adds the two config options and instruments synchronous scalar and table UDF calls. Splitting it out keeps the sampling and registration logic reviewable on its own, since it is the piece both the sync and async paths share.
The series:
1. `UdfMetrics` helper (this PR)
2. `table.exec.udf-metric-enabled` / `table.exec.udf-metric.sample-interval` plus synchronous scalar and table instrumentation
3. Asynchronous scalar and table instrumentation
4. Documentation
Each PR builds and passes its own tests independently. I'll open the next one as the previous merges.
@xuyangzhong @RocMarshal @HuangZhenQiu you all voted on the FLIP, so if you have time to take a look here I'd appreciate it.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40292][table-runtime] Add UdfMetrics helper for UDF metrics [flink]
Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #28878:
URL: https://github.com/apache/flink/pull/28878#issuecomment-5160802405
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "6556131258b53f291d482575b36ea48dfc4fe289",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "6556131258b53f291d482575b36ea48dfc4fe289",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* 6556131258b53f291d482575b36ea48dfc4fe289 UNKNOWN
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run azure` re-run the last Azure build
</details>
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40292][table-runtime] Add UdfMetrics helper for UDF metrics [flink]
Posted by "HuangZhenQiu (via GitHub)" <gi...@apache.org>.
HuangZhenQiu commented on code in PR #28878:
URL: https://github.com/apache/flink/pull/28878#discussion_r3751843730
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/metrics/UdfMetrics.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.MetricGroup;
+import org.apache.flink.metrics.ThreadSafeSimpleCounter;
+import org.apache.flink.runtime.metrics.DescriptiveStatisticsHistogram;
+import org.apache.flink.util.Preconditions;
+
+/**
+ * Per-operator metrics for a single user-defined function, registered under {@code
+ * <operator>.udf.<udfName>}: {@code udfProcessingTime} (a latency histogram) and {@code
+ * udfExceptionCount} (a counter of exceptions escaping the function).
+ *
+ * <p>Timing is sampled: only one invocation out of every {@code sampleInterval} is measured, so a
+ * hot function does not pay {@code System.nanoTime()} on every record. The sample decision advances
+ * a plain {@code int} and is only ever taken on the task thread, so it needs no synchronization.
+ * The two registered metrics are safe for the async completion thread to touch: the histogram
+ * synchronizes internally and the exception counter is {@link ThreadSafeSimpleCounter}.
+ */
+public final class UdfMetrics {
+
+ private static final int HISTORY_SIZE = 128;
+
+ private final int sampleInterval;
Review Comment:
What's time unit of the sampleInterval?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40292][table-runtime] Add UdfMetrics helper for UDF metrics [flink]
Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #28878:
URL: https://github.com/apache/flink/pull/28878#discussion_r3753878793
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/metrics/UdfMetrics.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.MetricGroup;
+import org.apache.flink.metrics.ThreadSafeSimpleCounter;
+import org.apache.flink.runtime.metrics.DescriptiveStatisticsHistogram;
+import org.apache.flink.util.Preconditions;
+
+/**
+ * Per-operator metrics for a single user-defined function, registered under {@code
+ * <operator>.udf.<udfName>}: {@code udfProcessingTime} (a latency histogram) and {@code
+ * udfExceptionCount} (a counter of exceptions escaping the function).
+ *
+ * <p>Timing is sampled: only one invocation out of every {@code sampleInterval} is measured, so a
+ * hot function does not pay {@code System.nanoTime()} on every record. The sample decision advances
+ * a plain {@code int} and is only ever taken on the task thread, so it needs no synchronization.
+ * The two registered metrics are safe for the async completion thread to touch: the histogram
+ * synchronizes internally and the exception counter is {@link ThreadSafeSimpleCounter}.
+ */
+public final class UdfMetrics {
+
+ private static final int HISTORY_SIZE = 128;
+
+ private final int sampleInterval;
Review Comment:
Thanks for the review! It's not time, it's a count of calls. At the default of 100, one call in every 100 gets timed; set it to 1 and every call is timed.
Fair point that the declaration didn't say that. I've added a line there and made the `@param` spell it out.
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/metrics/UdfMetrics.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.MetricGroup;
+import org.apache.flink.metrics.ThreadSafeSimpleCounter;
+import org.apache.flink.runtime.metrics.DescriptiveStatisticsHistogram;
+import org.apache.flink.util.Preconditions;
+
+/**
+ * Per-operator metrics for a single user-defined function, registered under {@code
+ * <operator>.udf.<udfName>}: {@code udfProcessingTime} (a latency histogram) and {@code
+ * udfExceptionCount} (a counter of exceptions escaping the function).
+ *
+ * <p>Timing is sampled: only one invocation out of every {@code sampleInterval} is measured, so a
+ * hot function does not pay {@code System.nanoTime()} on every record. The sample decision advances
+ * a plain {@code int} and is only ever taken on the task thread, so it needs no synchronization.
+ * The two registered metrics are safe for the async completion thread to touch: the histogram
+ * synchronizes internally and the exception counter is {@link ThreadSafeSimpleCounter}.
+ */
+public final class UdfMetrics {
+
+ private static final int HISTORY_SIZE = 128;
+
+ private final int sampleInterval;
+ private final Histogram processingTime;
+ private final Counter exceptionCount;
+
+ // Sample counter; only touched on the task thread, hence a plain int with no synchronization.
+ private int invocationCount = 0;
+
+ private UdfMetrics(int sampleInterval, Histogram processingTime, Counter exceptionCount) {
+ this.sampleInterval = sampleInterval;
+ this.processingTime = processingTime;
+ this.exceptionCount = exceptionCount;
+ }
+
+ /**
+ * Registers the {@code udfProcessingTime} histogram and {@code udfExceptionCount} counter under
+ * {@code <operatorMetricGroup>.udf.<udfName>}.
+ *
+ * @param sampleInterval measure one invocation out of every {@code sampleInterval}; must be
+ * {@code >= 1} ({@code 1} measures every invocation)
+ */
+ public static UdfMetrics register(
+ MetricGroup operatorMetricGroup, String udfName, int sampleInterval) {
+ Preconditions.checkArgument(
+ sampleInterval >= 1,
+ "UDF metric sample interval must be >= 1, but was %s.",
+ sampleInterval);
+ MetricGroup group = operatorMetricGroup.addGroup("udf", udfName);
+ return new UdfMetrics(
+ sampleInterval,
+ group.histogram(
+ "udfProcessingTime", new DescriptiveStatisticsHistogram(HISTORY_SIZE)),
+ group.counter("udfExceptionCount", new ThreadSafeSimpleCounter()));
+ }
+
+ /**
+ * Returns {@code true} for the one invocation in every {@code sampleInterval} whose processing
+ * time should be measured. Must be called once per invocation, on the task thread only.
+ */
+ public boolean shouldSample() {
+ if (sampleInterval == 1) {
Review Comment:
Fair point, the name doesn't tell you that on its own.
Would it be worth keeping for consistency though? Flink's state latency tracking does the same kind of sampling with the same word: `state.latency-track.sample-interval` is an int, defaults to 100, and means "track the latency every 100 access requests". Its `MetricsTrackingStateConfig` declares the same `private final int sampleInterval` with the same `>= 1` check. FLIP-485 followed that pattern, and the matching option here, `table.exec.udf-metric.sample-interval`, lands in the next PR.
What I'd worry about with a rename is the field no longer matching the option users actually set. So I've made the field say it counts invocations, not time. Does that address it, or would you still prefer a different name?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40292][table-runtime] Add UdfMetrics helper for UDF metrics [flink]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on code in PR #28878:
URL: https://github.com/apache/flink/pull/28878#discussion_r3754685953
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/metrics/UdfMetrics.java:
##########
@@ -0,0 +1,99 @@
+/*
+ * 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.MetricGroup;
+import org.apache.flink.metrics.ThreadSafeSimpleCounter;
+import org.apache.flink.runtime.metrics.DescriptiveStatisticsHistogram;
+import org.apache.flink.util.Preconditions;
+
+/**
+ * Per-operator metrics for a single user-defined function, registered under {@code
+ * <operator>.udf.<udfName>}: {@code udfProcessingTime} (a latency histogram) and {@code
+ * udfExceptionCount} (a counter of exceptions escaping the function).
+ *
+ * <p>Timing is sampled: only one invocation out of every {@code sampleInterval} is measured, so a
+ * hot function does not pay {@code System.nanoTime()} on every record. The sample decision advances
+ * a plain {@code int} and is only ever taken on the task thread, so it needs no synchronization.
+ * The two registered metrics are safe for the async completion thread to touch: the histogram
+ * synchronizes internally and the exception counter is {@link ThreadSafeSimpleCounter}.
+ */
+public final class UdfMetrics {
Review Comment:
```suggestion
public final class UDFMetrics {
```
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40292][table-runtime] Add UdfMetrics helper for UDF metrics [flink]
Posted by "HuangZhenQiu (via GitHub)" <gi...@apache.org>.
HuangZhenQiu commented on code in PR #28878:
URL: https://github.com/apache/flink/pull/28878#discussion_r3751871885
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/metrics/UdfMetrics.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.MetricGroup;
+import org.apache.flink.metrics.ThreadSafeSimpleCounter;
+import org.apache.flink.runtime.metrics.DescriptiveStatisticsHistogram;
+import org.apache.flink.util.Preconditions;
+
+/**
+ * Per-operator metrics for a single user-defined function, registered under {@code
+ * <operator>.udf.<udfName>}: {@code udfProcessingTime} (a latency histogram) and {@code
+ * udfExceptionCount} (a counter of exceptions escaping the function).
+ *
+ * <p>Timing is sampled: only one invocation out of every {@code sampleInterval} is measured, so a
+ * hot function does not pay {@code System.nanoTime()} on every record. The sample decision advances
+ * a plain {@code int} and is only ever taken on the task thread, so it needs no synchronization.
+ * The two registered metrics are safe for the async completion thread to touch: the histogram
+ * synchronizes internally and the exception counter is {@link ThreadSafeSimpleCounter}.
+ */
+public final class UdfMetrics {
+
+ private static final int HISTORY_SIZE = 128;
+
+ private final int sampleInterval;
+ private final Histogram processingTime;
+ private final Counter exceptionCount;
+
+ // Sample counter; only touched on the task thread, hence a plain int with no synchronization.
+ private int invocationCount = 0;
+
+ private UdfMetrics(int sampleInterval, Histogram processingTime, Counter exceptionCount) {
+ this.sampleInterval = sampleInterval;
+ this.processingTime = processingTime;
+ this.exceptionCount = exceptionCount;
+ }
+
+ /**
+ * Registers the {@code udfProcessingTime} histogram and {@code udfExceptionCount} counter under
+ * {@code <operatorMetricGroup>.udf.<udfName>}.
+ *
+ * @param sampleInterval measure one invocation out of every {@code sampleInterval}; must be
+ * {@code >= 1} ({@code 1} measures every invocation)
+ */
+ public static UdfMetrics register(
+ MetricGroup operatorMetricGroup, String udfName, int sampleInterval) {
+ Preconditions.checkArgument(
+ sampleInterval >= 1,
+ "UDF metric sample interval must be >= 1, but was %s.",
+ sampleInterval);
+ MetricGroup group = operatorMetricGroup.addGroup("udf", udfName);
+ return new UdfMetrics(
+ sampleInterval,
+ group.histogram(
+ "udfProcessingTime", new DescriptiveStatisticsHistogram(HISTORY_SIZE)),
+ group.counter("udfExceptionCount", new ThreadSafeSimpleCounter()));
+ }
+
+ /**
+ * Returns {@code true} for the one invocation in every {@code sampleInterval} whose processing
+ * time should be measured. Must be called once per invocation, on the task thread only.
+ */
+ public boolean shouldSample() {
+ if (sampleInterval == 1) {
Review Comment:
If the sampling is determined 1 / operation counts, sampleInterval is probably not the best variable name.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40292][table-runtime] Add UdfMetrics helper for UDF metrics [flink]
Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on PR #28878:
URL: https://github.com/apache/flink/pull/28878#issuecomment-5346749269
Thanks for the reviews, @HuangZhenQiu @RocMarshal ! CI is green on the latest commit.
@HuangZhenQiu , would you be able to merge when you get a chance? Happy to address anything else first if you'd like.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40292][table-runtime] Add UdfMetrics helper for UDF metrics [flink]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal merged PR #28878:
URL: https://github.com/apache/flink/pull/28878
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org