DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
This page is meant as a template for writing a FLIP. To create a FLIP choose Tools->Copy on this page and modify with your content and replace the heading with the next FLIP number and a description of your issue. Replace anything in italics with your own description.
Motivation
Monotonic counters are resilient by contract - if a process resets or a datapoint is lost, monitoring backends can rely on the declared monotonicity to auto-correct the results:
Prometheus rate()/increase() (docs):
From OpenTelemetry monotonicity docs:
org.apache.flink.metrics.Counter isn't monotonic - it exposes dec() / dec(long) / inc(<negative>) so by contract a counter may decrease at any time. Exporters must therefore treat every counter as potentially non-monotonic, even though all system-emitted counters in Flink only ever increase.
Since not expressed in the metric type, reporters handle counters monotonicity inconsistently:
- OpenTelemetry reporter drops non-incremental data points with a warning (FLINK-39893)
- Prometheus reporter works around it by mapping flink counters to Prometheus Gauges, losing the counter semantics (and thus the reset-detection benefit) entirely.
The goal of this FLIP is to let Flink metrics distinguish counters that are monotonic from those that may legitimately go up and down, so that:
- Effectively-monotonic System counters can become monotonic by contract and benefit from automatic reset detection.
- Rare non-monotonic counter use cases are still fully supported
- Within flink codebase, the only counter to decrement in production code is numEffectiveSpeculativeExecutions
- Reporting is consistent across monitoring systems.
- Fully binary compatible (removing deprecated methods will optionally take effect on the next major release - 3.0.0)
Public Interfaces
- Counter dec/dec(long) will become optional operations (like List#remove) and also deprecated to allow for their optional removal on the next major release.
@Public
public interface Counter extends Metric {
/**
* Decrement the current count by 1.
*
* <p>This is an <i>optional operation</i>: implementations whose count is meant to only ever
* increase (see {@link MonotonicCounter}) may refuse to support it and throw {@link
* UnsupportedOperationException} instead.
*
* @throws UnsupportedOperationException if this counter does not support decrementing
* @deprecated If you need a counter that can decrement,
* please migrate to {@link UpDownCounter}, where decrementing is guaranteed to be fully supported.
*/
@Deprecated
void dec();
/**
* Decrement the current count by the given value.
*
* <p>This is an <i>optional operation</i>: implementations whose count is meant to only ever
* increase (see {@link MonotonicCounter}) may refuse to support it and throw {@link
* UnsupportedOperationException} instead.
*
* @param n value to decrement the current count by
* @throws UnsupportedOperationException if this counter does not support decrementing
* @deprecated If you need a counter that can decrement,
* please migrate to {@link UpDownCounter}, where decrementing is guaranteed to be fully supported.
*/
@Deprecated
void dec(long n);
}
2. A new UpDownCounter will be offered as a migration path to support rare cases / user defined non-monotonic counters:
@PublicEvolving
public interface UpDownCounter extends Counter {
/**
* {@inheritDoc}
*
* <p>Unlike the general {@link Counter} contract, decrementing is fully supported.
*/
@Override
void dec();
/**
* {@inheritDoc}
*
* <p>Unlike the general {@link Counter} contract, decrementing is fully supported.
*/
@Override
void dec(long n);
}
3. A MonotonicCounter interface will be introduced, with guaranteed monotonicity
@PublicEvolving
public interface MonotonicCounter extends Counter {
/**
* Always throws {@link UnsupportedOperationException}, since a {@code MonotonicCounter}'s count
* only ever increases.
*
* @throws UnsupportedOperationException always
*/
@Override
default void dec() {
throw new UnsupportedOperationException("MonotonicCounter does not support decrementing.");
}
/**
* Always throws {@link UnsupportedOperationException}, since a {@code MonotonicCounter}'s count
* only ever increases.
*
* @throws UnsupportedOperationException always
*/
@Override
default void dec(long n) {
throw new UnsupportedOperationException("MonotonicCounter does not support decrementing.");
}
}
4. MetricGroup factory methods will be added overloads to register a MonotonicCounter
@Public
public interface MetricGroup {
/**
* Creates and registers a new {@link MonotonicCounter} with Flink.
*
* <p>Use this for counts that only ever increase, so that reporters can export them as
* monotonic/cumulative counters and benefit from automatic reset detection.
*
* @param name name of the counter
* @return the created counter
*/
@PublicEvolving
default MonotonicCounter monotonicCounter(String name) {
return counter(name, new SimpleMonotonicCounter());
}
/**
* Creates and registers a new {@link MonotonicCounter} with Flink.
*
* @param name name of the counter
* @return the created counter
* @see #monotonicCounter(String)
*/
@PublicEvolving
default MonotonicCounter monotonicCounter(int name) {
return monotonicCounter(String.valueOf(name));
}
}
Proposed Changes
A Counter interface with optional dec becomes the parent interface for both UpDownCounter (preserving the current behavior) and MonotonicCounter (guarantees monotonicity).
This simplifies types for all reporters that have no monotonicity distinction, and yet allows Prometheus and OpenTelemetry reporters (and any future reporter supporting monotonicity declaration) to tell them apart.
| Reporter | Current behavior | Proposed change |
|---|---|---|
| Prometheus | Flink (loses monotonicity semantic) |
|
| OpenTelemetry | Flink (drops non-incremental data points with a warning) |
|
| StatsD | Has no counter monotonicity distinction | no change in behavior |
| Datadog | Has no counter monotonicity distinction | no change in behavior |
| Dropwizard | Has no counter monotonicity distinction | no change in behavior |
| Graphite | Has no counter monotonicity distinction | no change in behavior |
| InfluxDB | Has no counter monotonicity distinction | no change in behavior |
| JMX | Has no counter monotonicity distinction | no change in behavior |
| SLF4J | Has no counter monotonicity distinction | no change in behavior |
API model
(Interfaces are Public/PublicEvolving, implementations are interval)
Compatibility, Deprecation, and Migration Plan
Counter is a Public, removing the deprecated Counter#dec can only happen on 3.0.0
The proposed change allow us to achieve monotonicity by contract, without braking binary compatibility
After Counter#dec removal, MonotonicCounter can be optionally deprecated in favor of the implicit monotonicity of Counter
Test Plan
UpDownCounter and MonotonicCounterimplementations can be unit tested- Integration test: scrape a running Flink job's metrics and confirm that monotonic counters are exposed with counter semantics.
Rejected Alternatives
- Expand the
Counterinterface to declare monotonicity via anisMonotonic()flag: Keeps the single interface but pushes a behavioral contract into a boolean, and keepsdec(...)available to monotonic counters.
