Class Metrics

java.lang.Object
org.apache.beam.sdk.metrics.Metrics

public class Metrics extends Object
The Metrics is a utility class for producing various kinds of metrics for reporting properties of an executing pipeline.

Metrics are created by calling one of the static methods in this class. Each metric is associated with a namespace and a name. The namespace allows grouping related metrics together based on the definition while also disambiguating common names based on where they are defined.

Reported metrics are implicitly scoped to the transform within the pipeline that reported them. This allows reporting the same metric name in multiple places and identifying the value each transform reported, as well as aggregating the metric across

It is runner-dependent whether Metrics are accessible during pipeline execution or only after jobs have completed.

Example:

 class SomeDoFn extendsDoFn<String, String> {
   private Counter counter = Metrics.counter(SomeDoFn.class, "my-counter");

  @ProcessElement
   public void processElement(ProcessContext c) {
     counter.inc();
     Metrics.counter(SomeDoFn.class, "my-counter2").inc();
   }
 }

See MetricResults (available from the PipelineResults interface) for an example off how to query metrics.

  • Field Details

  • Method Details

    • setDefaultPipelineOptions

      @Internal public static void setDefaultPipelineOptions(PipelineOptions options)
      Initialize metrics flags if not already done so.

      Should be called by worker at worker harness initialization. Should not be called by user code (and it does not have an effect as the initialization completed before).

    • counter

      public static Counter counter(String namespace, String name)
      Create a metric that can be incremented and decremented, and is aggregated by taking the sum.
    • counter

      public static Counter counter(Class<?> namespace, String name)
      Create a metric that can be incremented and decremented, and is aggregated by taking the sum.
    • distribution

      public static Distribution distribution(String namespace, String name)
      Create a metric that records various statistics about the distribution of reported values.
    • distribution

      public static Distribution distribution(Class<?> namespace, String name)
      Create a metric that records various statistics about the distribution of reported values.
    • gauge

      public static Gauge gauge(String namespace, String name)
      Create a metric that can have its new value set, and is aggregated by taking the last reported value.
    • gauge

      public static Gauge gauge(Class<?> namespace, String name)
      Create a metric that can have its new value set, and is aggregated by taking the last reported value.
    • gauge

      public static Gauge gauge(MetricName metricName)
      Create a metric that can have its new value set, and is aggregated by taking the last reported value.
    • stringSet

      public static StringSet stringSet(String namespace, String name)
      Create a metric that accumulates and reports set of unique string values.
    • stringSet

      public static StringSet stringSet(Class<?> namespace, String name)
      Create a metric that accumulates and reports set of unique string values.
    • boundedTrie

      public static BoundedTrie boundedTrie(Class<?> namespace, String name)
      Create a metric that accumulates and reports set of unique string values bounded to a max limit.
    • boundedTrie

      public static BoundedTrie boundedTrie(String namespace, String name)
      Create a metric that accumulates and reports set of unique string values bounded to a max limit.