Class TestSinkWriterMetricGroup

java.lang.Object
org.apache.flink.runtime.metrics.groups.ProxyMetricGroup<org.apache.flink.metrics.MetricGroup>
io.github.flink.gcp.connector.testutils.TestSinkWriterMetricGroup
All Implemented Interfaces:
org.apache.flink.metrics.groups.OperatorMetricGroup, org.apache.flink.metrics.groups.SinkWriterMetricGroup, org.apache.flink.metrics.MetricGroup

@Internal public final class TestSinkWriterMetricGroup extends org.apache.flink.runtime.metrics.groups.ProxyMetricGroup<org.apache.flink.metrics.MetricGroup> implements org.apache.flink.metrics.groups.SinkWriterMetricGroup
A SinkWriterMetricGroup whose metrics can be read back by the names they registered under, so a sink writer's metrics are asserted the way a reporter would see them.

Everything a writer registers — including the FLIP-33 standard counters, which are registered here under their documented names rather than merely held — goes through one MetricListener, so counterValue(String...) and gaugeValue(String...) reach all of them and a renamed or unregistered metric fails its test. That is what the alternatives cannot do: UnregisteredMetricsGroup.createSinkWriterMetricGroup() hands out a fresh SimpleCounter on every call, so the counter the writer captured is unreachable afterwards, and InternalSinkWriterMetricGroup has no mock(...) factory in either supported Flink line (1.20 and 2.x offer a package-private constructor and wrap(OperatorMetricGroup), which a listener group cannot satisfy).

ProxyMetricGroup, through the package-private ListenerReadableMetricGroup base holding the listener and the inherited counterValue(String...), gaugeValue(String...) and hasMetric(String...) accessors, supplies the delegation to the listener's group, which is why the registration methods are not overridden here.

  • Field Details

    • NUM_RECORDS_SEND

      public static final String NUM_RECORDS_SEND
      FLIP-33 name of the records counter, as a reporter sees it.
      See Also:
    • NUM_BYTES_SEND

      public static final String NUM_BYTES_SEND
      FLIP-33 name of the bytes counter.
      See Also:
    • NUM_RECORDS_SEND_ERRORS

      public static final String NUM_RECORDS_SEND_ERRORS
      FLIP-33 name of the send-error counter.
      See Also:
  • Method Details

    • create

      public static TestSinkWriterMetricGroup create()
      Creates a group over a fresh listener.
    • getCurrentSendTimeGauge

      @Nullable public org.apache.flink.metrics.Gauge<Long> getCurrentSendTimeGauge()
    • getNumRecordsSendCounter

      public org.apache.flink.metrics.Counter getNumRecordsSendCounter()
      Specified by:
      getNumRecordsSendCounter in interface org.apache.flink.metrics.groups.SinkWriterMetricGroup
    • getNumBytesSendCounter

      public org.apache.flink.metrics.Counter getNumBytesSendCounter()
      Specified by:
      getNumBytesSendCounter in interface org.apache.flink.metrics.groups.SinkWriterMetricGroup
    • getNumRecordsSendErrorsCounter

      public org.apache.flink.metrics.Counter getNumRecordsSendErrorsCounter()
      Specified by:
      getNumRecordsSendErrorsCounter in interface org.apache.flink.metrics.groups.SinkWriterMetricGroup
    • getNumRecordsOutErrorsCounter

      public org.apache.flink.metrics.Counter getNumRecordsOutErrorsCounter()
      Specified by:
      getNumRecordsOutErrorsCounter in interface org.apache.flink.metrics.groups.SinkWriterMetricGroup
    • setCurrentSendTimeGauge

      public void setCurrentSendTimeGauge(org.apache.flink.metrics.Gauge<Long> currentSendTimeGauge)
      Specified by:
      setCurrentSendTimeGauge in interface org.apache.flink.metrics.groups.SinkWriterMetricGroup
    • getIOMetricGroup

      public org.apache.flink.metrics.groups.OperatorIOMetricGroup getIOMetricGroup()
      Specified by:
      getIOMetricGroup in interface org.apache.flink.metrics.groups.OperatorMetricGroup
    • counterValue

      public final long counterValue(String... identifier)
      Returns the counter registered under identifier, relative to the group.
      Parameters:
      identifier - the name path, one element per group level (for example "errorClass", "UNAVAILABLE", "errors")
      Returns:
      the counter's value
      Throws:
      AssertionError - if nothing was registered under that name
    • hasMetric

      public final boolean hasMetric(String... identifier)
      Whether any metric is registered under identifier — the assertion a metric that may legitimately be absent needs, since "not registered" is not "registered at zero".
    • gaugeValue

      public final <T> T gaugeValue(String... identifier)
      Returns the value of the gauge registered under identifier.
      Type Parameters:
      T - the gauge's value type
      Parameters:
      identifier - the name path, one element per group level
      Returns:
      the gauge's current value
      Throws:
      AssertionError - if nothing was registered under that name