Interface TimerApi

All Known Subinterfaces:
DistributionDataPoint, GaugeDataPoint
All Known Implementing Classes:
Gauge, Histogram, Histogram.DataPoint, Summary, Summary.DataPoint

public interface TimerApi
Convenience API for timing durations.

Durations are recorded in seconds. The Prometheus instrumentation guidelines say: "Metrics must use base units (e.g. seconds, bytes) and leave converting them to something more readable to graphing tools".

  • Method Details

    • startTimer

      Start a Timer. Example:
      
       Histogram histogram = Histogram.builder()
               .name("http_request_duration_seconds")
               .help("HTTP request service time in seconds")
               .unit(SECONDS)
               .labelNames("method", "path")
               .register();
      
       try (Timer timer = histogram.labelValues("GET", "/").startTimer()) {
           // duration of this code block will be observed.
       }
       
      Durations are recorded in seconds. The Prometheus instrumentation guidelines say: "Metrics must use base units (e.g. seconds, bytes) and leave converting them to something more readable to graphing tools".
    • time

      default void time(Runnable func)
      Observe the duration of the func call. Example:
      
       Histogram histogram = Histogram.builder()
               .name("request_duration_seconds")
               .help("HTTP request service time in seconds")
               .unit(SECONDS)
               .labelNames("method", "path")
               .register();
      
       histogram2.labelValues("GET", "/").time(() -> {
           // duration of this code block will be observed.
       });
       

      Durations are recorded in seconds. The Prometheus instrumentation guidelines say: "Metrics must use base units (e.g. seconds, bytes) and leave converting them to something more readable to graphing tools".

    • time

      default <T> T time(Supplier<T> func)
      Like time(Runnable), but returns the return value of func.
    • timeChecked

      default <T> T timeChecked(Callable<T> func) throws Exception
      Like time(Supplier), but func may throw a checked Exception.
      Throws:
      Exception