Class TimerRepresentation

  • All Implemented Interfaces:
    io.micrometer.core.instrument.distribution.HistogramSupport, io.micrometer.core.instrument.Meter, io.micrometer.core.instrument.Timer

    public class TimerRepresentation
    extends MeterRepresentation
    implements io.micrometer.core.instrument.Timer
    This class aims to provide a prototypical implementation of the Timer Interface from Micrometer-API, allowing a client process to access the Timer values collected from a Service in a uniform way.
    Even though the methods are functional and will indeed offer an accurate implementation of the metric we are simulating with this prototype, it is highly recommended that an instance of this class is used solely to access data and not to modify it, as the changes will not be registered in any sort of registry under normal circumstances, and this will alter the read values of the actual metrics.
    Timer intended to track of a large number of short running events.
    The JsonObject representing a Timer will have the following format:
    {
        "name": "sample.name",
        "description": "sample description, can be null",
        "baseUnit": "ourTimeUnit",
        "measurements": [
            {
                "statistic": "COUNT",
                "value": 123
            },
            {
                "statistic": "TOTAL_TIME",
                "value": 123.123
            },
            {
                "statistic": "MAX",
                "value": 123.123
            }
        ],
        "availableTags": []
    }
     
    Special attention is required in the keys for the JsonValues as well as the type of Statistic and number of measurements. For the time valid time unit values, see TimeUnit
    The services offered by this class are:
    • Parse a JsonObject into a Timer
    • Take a Histogram Snapshot
    • Record an amount of time with this timer
    • Check the maximum and total time recorded by the timer
    • Check the amount of times the timer has been called
    • Check the basic time unit for the timer
    Author:
    Miguel Gomez
    See Also:
    Timer, TimeUnit
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface io.micrometer.core.instrument.Meter

        io.micrometer.core.instrument.Meter.Builder, io.micrometer.core.instrument.Meter.Id, io.micrometer.core.instrument.Meter.Type
      • Nested classes/interfaces inherited from interface io.micrometer.core.instrument.Timer

        io.micrometer.core.instrument.Timer.Builder, io.micrometer.core.instrument.Timer.ResourceSample, io.micrometer.core.instrument.Timer.Sample
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.TimeUnit baseTimeUnit  
      private long count  
      private long maxTime  
      private java.util.List<io.micrometer.core.instrument.Measurement> measurements  
      private long totalTime  
      private java.util.List<java.lang.Long> updates  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private TimerRepresentation​(java.lang.String name)
      Initializes a new TimerRepresentation.
      private TimerRepresentation​(javax.json.JsonObject object, java.lang.String... tags)
      Initializes a new TimerRepresentation.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.concurrent.TimeUnit baseTimeUnit()  
      long count()  
      static io.micrometer.core.instrument.Timer createNewTimer​(java.lang.String name)
      Creates a new Timer with no measurements in it.
      javax.json.JsonObject getUpdater()
      Provides an updater for the Timer.
      double max​(java.util.concurrent.TimeUnit unit)  
      java.lang.Iterable<io.micrometer.core.instrument.Measurement> measure()  
      static io.micrometer.core.instrument.Timer parseTimer​(javax.json.JsonObject object, java.lang.String... tags)
      Parses a new timer from a JsonObject.
      See the class documentation to see the format of a JsonObject representing a Timer is expecting to have.
      void record​(long amount, java.util.concurrent.TimeUnit unit)  
      void record​(java.lang.Runnable runnable)  
      <T> T record​(java.util.function.Supplier<T> supplier)  
      <T> T recordCallable​(java.util.concurrent.Callable<T> callable)  
      io.micrometer.core.instrument.distribution.HistogramSnapshot takeSnapshot()  
      double totalTime​(java.util.concurrent.TimeUnit unit)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface io.micrometer.core.instrument.distribution.HistogramSupport

        takeSnapshot
      • Methods inherited from interface io.micrometer.core.instrument.Meter

        close, getId, match, use
      • Methods inherited from interface io.micrometer.core.instrument.Timer

        histogramCountAtValue, mean, percentile, record, wrap, wrap, wrap
    • Field Detail

      • count

        private long count
      • baseTimeUnit

        private java.util.concurrent.TimeUnit baseTimeUnit
      • measurements

        private java.util.List<io.micrometer.core.instrument.Measurement> measurements
      • totalTime

        private long totalTime
      • maxTime

        private long maxTime
      • updates

        private java.util.List<java.lang.Long> updates
    • Constructor Detail

      • TimerRepresentation

        private TimerRepresentation​(javax.json.JsonObject object,
                                    java.lang.String... tags)
        Initializes a new TimerRepresentation.
        Parameters:
        object - JsonObject representing the Timer
        tags - tags that the Timer has
        Throws:
        java.lang.IllegalArgumentException - if the object is null, or if the JsonObject doesn't represent a valid timer.
      • TimerRepresentation

        private TimerRepresentation​(java.lang.String name)
        Initializes a new TimerRepresentation.
        Parameters:
        name - URN of the timer
        Throws:
        java.lang.IllegalArgumentException - if the name is null or empty
    • Method Detail

      • parseTimer

        public static io.micrometer.core.instrument.Timer parseTimer​(javax.json.JsonObject object,
                                                                     java.lang.String... tags)
        Parses a new timer from a JsonObject.
        See the class documentation to see the format of a JsonObject representing a Timer is expecting to have.
        Parameters:
        object - JsonObject representing the Timer we wish to parse
        tags - tags that the counter has following the format key:value
        Returns:
        a Timer representation of the JsonObject
        Throws:
        java.lang.IllegalArgumentException - if the object is null, or if the JsonObject doesn't represent a valid timer.
      • createNewTimer

        public static io.micrometer.core.instrument.Timer createNewTimer​(java.lang.String name)
        Creates a new Timer with no measurements in it.
        Parameters:
        name - URN of the timer
        Returns:
        a new Timer ready to be used
        Throws:
        java.lang.IllegalArgumentException - if the name is null or empty
      • takeSnapshot

        public io.micrometer.core.instrument.distribution.HistogramSnapshot takeSnapshot()
        Specified by:
        takeSnapshot in interface io.micrometer.core.instrument.distribution.HistogramSupport
      • record

        public void record​(long amount,
                           java.util.concurrent.TimeUnit unit)
        Specified by:
        record in interface io.micrometer.core.instrument.Timer
      • record

        public <T> T record​(java.util.function.Supplier<T> supplier)
        Specified by:
        record in interface io.micrometer.core.instrument.Timer
      • recordCallable

        public <T> T recordCallable​(java.util.concurrent.Callable<T> callable)
                             throws java.lang.Exception
        Specified by:
        recordCallable in interface io.micrometer.core.instrument.Timer
        Throws:
        java.lang.Exception
      • record

        public void record​(java.lang.Runnable runnable)
        Specified by:
        record in interface io.micrometer.core.instrument.Timer
      • count

        public long count()
        Specified by:
        count in interface io.micrometer.core.instrument.Timer
      • totalTime

        public double totalTime​(java.util.concurrent.TimeUnit unit)
        Specified by:
        totalTime in interface io.micrometer.core.instrument.Timer
      • max

        public double max​(java.util.concurrent.TimeUnit unit)
        Specified by:
        max in interface io.micrometer.core.instrument.Timer
      • baseTimeUnit

        public java.util.concurrent.TimeUnit baseTimeUnit()
        Specified by:
        baseTimeUnit in interface io.micrometer.core.instrument.Timer
      • measure

        public java.lang.Iterable<io.micrometer.core.instrument.Measurement> measure()
        Specified by:
        measure in interface io.micrometer.core.instrument.Meter
        Specified by:
        measure in interface io.micrometer.core.instrument.Timer
        Specified by:
        measure in class MeterRepresentation
      • getUpdater

        public javax.json.JsonObject getUpdater()
        Provides an updater for the Timer.
        The information includes the name and the different recordings carried out by this timer since its instantiation. This information is packed into a JSON Object with the following format:
         {
             "name":"customtimer",
             "recordings":[
                 100000000,
                 300000000,
                 200000000
             ]
         }
         
        Specified by:
        getUpdater in class MeterRepresentation
        Returns:
        a JsonObject representing the changes this meter has experienced
        See Also:
        MeterRepresentation.getUpdater()