Class Metrics

java.lang.Object
com.hazelcast.jet.core.metrics.Metrics

public final class Metrics extends Object
Utility class for obtaining handlers to user-defined metrics.

User-defined metrics are simple numeric values used to count or measure things. A code submitted to Jet can use them to publish any custom run-time values.

Since:
Jet 4.0
  • Method Details

    • metric

      public static Metric metric(String name)
      Returns a non-thread-safe handler for manipulating the metric with the specified name.

      This method only works if called from a processor thread and the returned handler is tied to the processor that is currently executing. The handler is created on the first call, subsequent calls return the cached instance and ignore the requested unit or thread safety. Until the first call is made, the metric is not visible in JMX or using Job.getMetrics().

    • metric

      public static Metric metric(String name, Unit unit)
      Same as metric(String), but allows us to also specify the measurement Unit of the metric.
    • threadSafeMetric

      public static Metric threadSafeMetric(String name)
      Returns a thread-safe handler for manipulating the metric with the specified name and measurement unit.

      You need the thread-safe method if you submit work to other threads and want to manipulate the metric in those threads. For example:

       
        p.readFrom(...)
         .mapUsingServiceAsync(
             nonSharedService(pctx -> 10L),
             (ctx, item) -> {
                 // need to use thread-safe metric since it will be mutated from another thread
                 Metric mapped = Metrics.threadSafeMetric("mapped", Unit.COUNT);
                 return CompletableFuture.supplyAsync(
                     () -> {
                         mapped.increment();
                         return item * ctx;
                     }
                 );
             }
         )
       

      This method only works if called from a processor thread and the returned handler is tied to the processor that is currently executing. The handler is created on the first call, subsequent calls return the cached instance and ignore the requested unit or thread safety. Until the first call is made, the metric is not visible in JMX or using Job.getMetrics().

    • threadSafeMetric

      public static Metric threadSafeMetric(String name, Unit unit)
      Same as threadSafeMetric(String), but allows us to also specify the measurement Unit of the metric.