Class DestinationMetrics

java.lang.Object
io.github.flink.gcp.connector.base.metrics.DestinationMetrics

@Internal public final class DestinationMetrics extends Object
Optional per-destination send counters, as destination.NAME.recordsSend and destination.NAME.sendErrors on the writer's metric group, where NAME is the destination as the connector's docs page spells it.

Opt-in, and off by default, because Flink cannot unregister a metric. A sink writing to per-record destinations has an unbounded destination set — a topic per tenant, a table per day — so registering a subgroup per destination would grow the metric registry for the lifetime of the task and undo the eviction hygiene the writers themselves practise. Each connector that registers these counters exposes the switch as perDestinationMetrics on its own options object.

Entries are never removed, which is the deliberate consequence: a destination whose writer state was evicted and later rebuilt reuses the counters it had, so its totals stay continuous instead of restarting at zero. Nothing else could be true — an unregistered metric cannot be re-registered under the same name.

Call sites hold a DestinationMetrics.Counters handle rather than passing a destination name per record: the name is then composed once per destination instead of once per record, and a disabled instance costs the writer nothing beyond two null checks. DestinationMetrics.Counters is safe to cache alongside the writer's own per-destination state.

The counter type is the caller's choice, for the reason ErrorClassCounters records: of(MetricGroup, boolean) registers task-thread-only SimpleCounters, and of(MetricGroup, boolean, Supplier) takes the counter a connector counting from SDK callback threads needs. Registration itself is safe from any thread either way.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    One destination's counters; a no-op instance when per-destination metrics are off.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Group name carrying the destination, so the destination is a metric variable, not a name.
    static final String
    Counter name for records handed to the client for a destination.
    static final String
    Counter name for records routed to the failure handler for a destination.
  • Method Summary

    Modifier and Type
    Method
    Description
    forDestination(String destination)
    Returns the counters for a destination, registering them on first use.
    of(org.apache.flink.metrics.MetricGroup metricGroup, boolean enabled)
    Creates the per-destination counters.
    of(org.apache.flink.metrics.MetricGroup metricGroup, boolean enabled, Supplier<? extends org.apache.flink.metrics.Counter> counterSupplier)
    Creates the per-destination counters with the counter type the caller needs for the threads it counts from.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DESTINATION_GROUP

      public static final String DESTINATION_GROUP
      Group name carrying the destination, so the destination is a metric variable, not a name.
      See Also:
    • RECORDS_SEND

      public static final String RECORDS_SEND
      Counter name for records handed to the client for a destination.
      See Also:
    • SEND_ERRORS

      public static final String SEND_ERRORS
      Counter name for records routed to the failure handler for a destination.
      See Also:
  • Method Details

    • of

      public static DestinationMetrics of(org.apache.flink.metrics.MetricGroup metricGroup, boolean enabled)
      Creates the per-destination counters.
      Parameters:
      metricGroup - the sink writer's metric group
      enabled - whether the connector's perDestinationMetrics option is set; when false, nothing is ever registered and forDestination(java.lang.String) always returns a no-op
      Returns:
      the counters
    • of

      public static DestinationMetrics of(org.apache.flink.metrics.MetricGroup metricGroup, boolean enabled, Supplier<? extends org.apache.flink.metrics.Counter> counterSupplier)
      Creates the per-destination counters with the counter type the caller needs for the threads it counts from.
      Parameters:
      metricGroup - the metric group the per-destination subgroups register on
      enabled - whether the connector's perDestinationMetrics option is set; when false, nothing is ever registered and forDestination(java.lang.String) always returns a no-op
      counterSupplier - creates each counter before it is registered under its name; pass a thread-safe counter when increments arrive from more than one thread
      Returns:
      the counters
    • forDestination

      public DestinationMetrics.Counters forDestination(String destination)
      Returns the counters for a destination, registering them on first use. The result is stable per destination name and is meant to be cached by the caller.
      Parameters:
      destination - the destination name, as the connector's docs page spells it
      Returns:
      the destination's counters, or a no-op when the option is off