A colleague was complaining to me that Micrometer gauges didn't work the way he expected. This led to some interesting work.
What is a gauge?In science a gauge is a device for making measurements. In computer systems a gauge is very similar: a 'metric' which tracks something in your system over time. For example, you could track the number of items in a job queue. Libraries like Micrometer and Dropwizard metrics make it easy to define gauges. Since the measurement in itself is not useful, those libraries also make it easy to send the measurements to a metric system such as Graphite or Prometheus. These systems are used for visualization and generating alerts.
Gauges are typically defined with a callback function that does the measurement. For example, using metrics-scala, the scala API for Dropwizard metrics, it looks like:
class JobQueue extends DefaultInstrumented {
private val waitingJobsQueue = ???
// Defines a gauge
metrics.gauge("queue.size") {
// This code block is the callback which does a 'measurement'.
waitingJobsQueue.size
}
}
Please note that the metric library determines when the callback function is invoked. For example, once every minute.
What is a push gauge?My colleague had something else in mind. He didn't have access to the value all the time, but only when something was being processed. More like this:
class ExternalCacheUpdater extends DefaultInstrumented {
def updateExternalCache(): Unit = {
val items = fetchItemsFromDatabase()
pushItemsToExternalCache(items)
gauge.push(items.size) // Pushes a new measurement to the gauge.
}
}
In the example the application becomes responsible for pushing new measurements. The push gauge simply keeps track of the last value and reports that whenever the metrics library needs it. So under the covers the push gauge behaves like a normal gauge.
Push gauges like in this example are now made possible in this pull-request for metrics-scala. The only thing that was missing is the definition of the push gauge:
class ExternalCacheUpdater extends DefaultInstrumented {
// Defines a push gauge
private val gauge = metrics.pushGauge[Int]("cached.items", 0)
def updateExternalCache(): Unit = // as above
}
In some situations it may be misleading to report a very old measurement as the 'current' value. If the external cache in our example evicts items after 10 minutes, then the push gauge should not report measurements from more then 10 minutes ago. This is solved with a push gauge with timeout:
class ExternalCache extends DefaultInstrumented {
// Defines a push gauge with timeout
private val gauge = metrics.pushGaugeWithTimeout[Int]("cached.items", 0, 10.minutes)
def updateExternalCache(): Unit = // as above
}
I have not seen this concept before in any metric library in the JVM ecosystem. Therefore I would like to collect as much feedback as possible before shipping this as a new feature of metrics-scala. If you have any ideas, comments or whatever, please leave a comment on the push-gauges pull-request or drop me an email!
Update 2020-03-05: The code example have been updated to reflect changes in the pull request.
No comments:
Post a Comment