aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/metric/instrument/Gauge.scala
blob: 1efff2bcd5e943fc0cc2e5ce59a172175f79559c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package kamon.metric.instrument

import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicReference

import akka.actor.{ Cancellable, ActorSystem }
import com.typesafe.config.Config
import kamon.metric.{ CollectionContext, Scale, MetricRecorder }

import scala.concurrent.duration.FiniteDuration

trait Gauge extends MetricRecorder {
  type SnapshotType = Histogram.Snapshot

  def record(value: Long)
  def record(value: Long, count: Long)
}

object Gauge {

  trait CurrentValueCollector {
    def currentValue: Long
  }

  def apply(precision: Histogram.Precision, highestTrackableValue: Long, scale: Scale, refreshInterval: FiniteDuration,
    system: ActorSystem)(currentValueCollector: CurrentValueCollector): Gauge = {

    val underlyingHistogram = Histogram(highestTrackableValue, precision, scale)
    val gauge = new HistogramBackedGauge(underlyingHistogram, currentValueCollector)

    val refreshValuesSchedule = system.scheduler.schedule(refreshInterval, refreshInterval) {
      gauge.refreshValue()
    }(system.dispatcher) // TODO: Move this to Kamon dispatchers

    gauge.refreshValuesSchedule.set(refreshValuesSchedule)
    gauge
  }

  def fromDefaultConfig(system: ActorSystem)(currentValueCollectorFunction: ()  Long): Gauge =
    fromDefaultConfig(system, functionZeroAsCurrentValueCollector(currentValueCollectorFunction))

  def fromDefaultConfig(system: ActorSystem, currentValueCollector: CurrentValueCollector): Gauge = {
    val config = system.settings.config.getConfig("kamon.metrics.precision.default-gauge-precision")
    fromConfig(config, system)(currentValueCollector)
  }

  def fromConfig(config: Config, system: ActorSystem)(currentValueCollector: CurrentValueCollector): Gauge = {
    import scala.concurrent.duration._

    val highest = config.getLong("highest-trackable-value")
    val significantDigits = config.getInt("significant-value-digits")
    val refreshInterval = config.getDuration("refresh-interval", TimeUnit.MILLISECONDS)

    Gauge(Histogram.Precision(significantDigits), highest, Scale.Unit, refreshInterval.millis, system)(currentValueCollector)
  }

  implicit def functionZeroAsCurrentValueCollector(f: ()  Long): CurrentValueCollector = new CurrentValueCollector {
    def currentValue: Long = f.apply()
  }
}

class HistogramBackedGauge(underlyingHistogram: Histogram, currentValueCollector: Gauge.CurrentValueCollector) extends Gauge {
  val refreshValuesSchedule = new AtomicReference[Cancellable]()

  def record(value: Long): Unit = underlyingHistogram.record(value)

  def record(value: Long, count: Long): Unit = underlyingHistogram.record(value, count)

  def collect(context: CollectionContext): Histogram.Snapshot = underlyingHistogram.collect(context)

  def cleanup: Unit = {
    if (refreshValuesSchedule.get() != null)
      refreshValuesSchedule.get().cancel()
  }

  def refreshValue(): Unit = underlyingHistogram.record(currentValueCollector.currentValue)
}