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.

Document the state by adding a label to the FLIP page with one of "discussion", "accepted", "released", "rejected".

Discussion threadhttps://lists.apache.org/thread/l0ysm6jyhv3gjm1wnyhwvmwk5dot4ckt
Vote thread
JIRA
Release<Flink Version>

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

rate(v range-vector) calculates the per-second average rate of increase of the time series in the range vector.

Breaks in monotonicity (such as counter resets due to target restarts) are automatically adjusted for. Also, the calculation extrapolates to the ends of the time range, allowing for missed scrapes or imperfect alignment of scrape cycles with the range's time period.


From OpenTelemetry monotonicity docs:

This monotonicity property is important because it gives the downstream systems additional hints so they can handle the data in a better way. Imagine we report the total number of bytes received in a cumulative sum data stream:

    • At Tn, we reported 3,896,473,820.
    • At Tn+1, we reported 4,294,967,293.
    • At Tn+2, we reported 1,800,372.

The backend system could tell that there was integer overflow or system restart during (Tn+1, Tn+2], so it has chance to “fix” the data. Refer to additive property for more information about integer overflow.

Let’s take another example with a process using an Asynchronous Counter to report the total page faults of the process:

The page faults are managed by the operating system, and the process could retrieve the number of page faults via some system APIs.

    • At T0:
      • the process started
      • the process didn’t ask the operating system to report the page faults
    • At T1:
      • the operating system reported with 1000 page faults for the process
    • At T2:
      • the process didn’t ask the operating system to report the page faults
    • At T3:
      • the operating system reported with 1050 page faults for the process
    • At T4:
      • the operating system reported with 1200 page faults for the process

You can see that the number being reported is the absolute value rather than increments, and the value is monotonically increasing.

If we need to calculate “how many page faults have been introduced during (T3, T4]”, we need to apply subtraction 1200 - 1050 = 150.


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

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

ReporterCurrent behaviorProposed change
Prometheus

Flink Counter (non-monotonic) maps to Prometheus Gauge.

(loses monotonicity semantic)

  • Flink MonotonicCounter maps to Prometheus Counter
  • Any other Flink Counter maps to Prometheus Gauge
OpenTelemetry

Flink Counter (non-monotonic) maps to OTel (monotonic) Counter,

(drops non-incremental data points with a warning)

  • Flink MonotonicCounter maps to Otel Counter 
  • Any other Flink Counter maps to Otel UpDownCounter 
StatsDHas no counter monotonicity distinctionno change in behavior
DatadogHas no counter monotonicity distinctionno change in behavior
DropwizardHas no counter monotonicity distinctionno change in behavior
GraphiteHas no counter monotonicity distinctionno change in behavior
InfluxDBHas no counter monotonicity distinctionno change in behavior
JMXHas no counter monotonicity distinctionno change in behavior
SLF4JHas no counter monotonicity distinctionno 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 MonotonicCounter implementations 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 Counter  interface to declare monotonicity via an isMonotonic()  flag: Keeps the single interface but pushes a behavioral contract into a boolean, and keeps dec(...)  available to monotonic counters.