DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
Motivation
User-defined functions (UDFs) are widely used in Flink jobs to implement custom business logic. However, they pose significant challenges for debugging and performance optimization because they often act as black boxes. When issues arise (e.g., poor performance, high latency, frequent exceptions), it's hard for users and operators to trace the root causes inside UDFs. Currently, Flink does not provide dedicated built-in metrics to quantify critical aspects such as per-record processing time or exception count within UDFs. This lack of observability makes it harder to:
Debug production issues.
Tune performance and resource allocation.
Feed autoscaling systems (e.g., autosizer) with reliable UDF-level signals.
Providing standard, opt-in UDF-level metrics will help ensure the observability and health of the entire platform.
Public Interfaces
A new configuration option to enable or disable UDF metrics collection.
| Config Option | Default | Description |
|---|---|---|
| table.exec.udf-metric-enabled | false | Enables collection of per-record UDF metrics. |
| table.exec.udf-metric.sample-interval | 100 | Measure every Nth UDF invocation when metrics are enabled. Higher values reduce overhead; lower values increase accuracy. Set to 1 for every invocation (not recommended for production). |
Two new built-in metrics registered per operator for UDFs:
| Metric Name | Type | Description |
|---|---|---|
udfProcessingTime | Histogram | Sampled processing time for UDF invocations. Reports p50/p75/p95/p99/mean/min/max. Sampling interval is configurable. |
udfExceptionCount | Counter | Counts UDF exceptions. When > 0, it clearly signals that errors originate in user code (UDF), supporting autosizer logic and auto-remediation. It counts exceptions that surface from the UDF — both synchronous failures and asynchronous exceptional completions. |
Proposed Changes
Config Flag
Since per-invocation metrics can significantly impact performance (see FLINK-16444, where RocksDB per-operation stats caused >10% degradation), we adopt a two-layer protection:
- Feature gate: table.exec.udf-metric-enabled (default: false) — when disabled, no metrics are registered or recorded, ensuring zero overhead.
- Sampling: table.exec.udf-metric.sample-interval (default: 100) — when enabled, only every Nth invocation is measured. This follows the same counter-based sampling pattern established by Flink's state latency tracking (FLINK-21736). The fast path (non-sampled invocations) is a single integer increment with negligible overhead.
Metrics Hierarchy
To get UDF metrics at per TM UDF level, we define UdfMetricGroup with the following hierarchy and all metrics under this group should have the prefix “udf”.
TaskManagerMetricGroup
└── TaskManagerJobMetricGroup
└── TaskMetricGroup
└── OperatorMetricGroup
└── UdfMetricGroup
├── udfProcessingTime (Histogram)
└── udfExceptionCount (Counter)
Metrics will be registered in the MetricRegistry and reported via existing MetricReporters (e.g., Prometheus, JMX).
UDF Metrics Naming Conventions
Based on Flink's metric naming conventions, UDF metrics should follow this pattern:
<operator_name>.udf.<udf_name>.<metric_name>
|
Metric Registration
Whether UDF metrics are collected is decided when the query plan is generated (the flag is fixed for a job). When metrics are disabled, nothing is added to the operator and there is no runtime cost. When they are enabled, the two metrics are registered once per operator for each UDF it runs, so multiple calls to the same UDF within one operator report into a single processing-time histogram and exception counter.
Recording Values
With UDFs, we expect user to extend the UDF base classes and most of the time they will override the execution methods like open(), map(), or eval() in UDF base class. Therefore, the measurement of exception count or processing time cannot be added to those methods directly. To overcome this, we set the value for those metrics at CodeGen, so that these metrics are always available regardless of function overrides.
Sampling Strategy (following FLINK-21736 precedent):
We adopt the same counter-based sampling pattern used by Flink's state latency tracking: with a sample interval of N, one invocation out of every N is timed; a sample interval of 1 times every invocation. Exceptions are counted on every invocation, regardless of the sample interval.
private int invocationCount = 0;
private boolean shouldSample() {
if (sampleInterval == 1) { // sample every invocation
return true;
}
invocationCount = (invocationCount + 1 < sampleInterval) ? invocationCount + 1 : 0;
return invocationCount == 1;
}
- Fast path (non-sampled): Single integer increment per invocation — negligible overhead
- Sampled path: System.nanoTime() around the UDF call, result stored in a DescriptiveStatisticsHistogram with a bounded circular buffer (128 entries)
- Exception counting: Not sampled — exceptions are rare events, every occurrence is counted
UDF Metrics:
- On sampled invocations: measure start and end times around UDF invocations → udfProcessingTime.
On all invocations: catch and count exceptions → udfExceptionCount.
The generated operator code will automatically handle both synchronous and asynchronous UDF execution.
Implementation Entry Points:
CommonExecMatch.java (pass in Flink context classLoader to MatchCodeGenerator)
-> MatchCodeGenerator.scala
-> ExprCodeGenerator.scala
-> TableFunctionCallGen.scala
-> ScalarFunctionCallGen.scala
Asynchronous scalar and table UDFs are instrumented through their own code-generation paths rather than the pattern-matching path shown above.
Context Access:
- The metrics are registered on the executing operator's metric group, which correctly associates them with the operator running the UDF.
- The operator's metric group is available directly from the runtime context; no reflection is required to reach it.
- Because the values are recorded by the generated code around the UDF call rather than inside the user's function body, the metrics are populated regardless of how the user overrides the UDF's lifecycle methods.
For asynchronous UDFs (ASYNC_SCALAR and ASYNC_TABLE):
Asynchronous UDFs are invoked by handing the function a future that the user completes once the asynchronous work finishes.
- udfProcessingTime measures the end-to-end latency of an asynchronous invocation: the timer starts when the call is dispatched and stops when the user completes the future. The reported value is the full dispatch-to-completion time (the user-visible async latency), captured as a single sample.
- udfExceptionCount is incremented when an asynchronous invocation completes exceptionally.
- Because asynchronous completions run on a different thread from record dispatch, the sampling decision is made at dispatch time and the metric updates on completion are designed to be safe under concurrent in-flight invocations.
Compatibility, Deprecation, and Migration Plan
Backward Compatibility:
Fully backward compatible.
By default (
enableUdfMetrics = false), Flink behavior is unchanged.
Deprecation:
No existing APIs or configs are deprecated.
Migration:
No migration steps required for existing jobs.
Users who want these metrics simply enable the config.
This FLIP covers synchronous and asynchronous scalar and table UDFs. Process Table Functions are out of scope: a Process Table Function has a different execution model — it is a stateful function with its own timer callbacks — so defining a meaningful processing-time metric across its lifecycle is a separate design and a natural follow-up. Aggregate functions are likewise out of scope.
Test Plan
Unit Tests:
Verify metric registration logic with
enableUdfMetrics = trueandfalse.Validate correct metric hierarchy and naming.
Performance Testing:
Benchmark overhead of per-message metric recording.
Ensure any performance regression is documented and acceptable.
- Verify that with default sample-interval (100), overhead is <1% compared to metrics disabled
- Validate that sample-interval=1 (every invocation) produces accurate results for correctness testing
Rejected Alternatives
Add Metrics Directly in UDF Base Classes:
- Pros
- Straightforward logic.
- Fast and simple to implement
- Cons
- Maintenance overhead: for each of the new UDF base added we need to add metrics.
- Limited options to user and we will consistently need to add additional classes to address user needs.