aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/metric/UserMetrics.scala
blob: f3803d37d3d8f19a31cd28c1574ec8c8c9cc2ed8 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package kamon.metric

import akka.actor
import akka.actor.{ ActorSystem, ExtendedActorSystem, ExtensionIdProvider, ExtensionId }
import com.typesafe.config.Config
import kamon.Kamon
import kamon.metric.instrument.{ Gauge, MinMaxCounter, Counter, Histogram }

import scala.collection.concurrent.TrieMap
import scala.concurrent.duration.FiniteDuration

class UserMetricsExtension(system: ExtendedActorSystem) extends Kamon.Extension {
  lazy val userMetricsRecorder = Kamon(Metrics)(system).register(UserMetrics, UserMetrics.Factory).get

  def registerHistogram(name: String, precision: Histogram.Precision, highestTrackableValue: Long): Histogram =
    userMetricsRecorder.buildHistogram(name, precision, highestTrackableValue)

  def registerHistogram(name: String): Histogram =
    userMetricsRecorder.buildHistogram(name)

  def registerCounter(name: String): Counter =
    userMetricsRecorder.buildCounter(name)

  def registerMinMaxCounter(name: String, precision: Histogram.Precision, highestTrackableValue: Long,
    refreshInterval: FiniteDuration): MinMaxCounter = {
    userMetricsRecorder.buildMinMaxCounter(name, precision, highestTrackableValue, refreshInterval)
  }

  def registerMinMaxCounter(name: String): MinMaxCounter =
    userMetricsRecorder.buildMinMaxCounter(name)

  def registerGauge(name: String)(currentValueCollector: Gauge.CurrentValueCollector): Gauge =
    userMetricsRecorder.buildGauge(name)(currentValueCollector)

  def registerGauge(name: String, precision: Histogram.Precision, highestTrackableValue: Long,
    refreshInterval: FiniteDuration)(currentValueCollector: Gauge.CurrentValueCollector): Gauge =
    userMetricsRecorder.buildGauge(name, precision, highestTrackableValue, refreshInterval, currentValueCollector)

  def removeHistogram(name: String): Unit =
    userMetricsRecorder.removeHistogram(name)

  def removeCounter(name: String): Unit =
    userMetricsRecorder.removeCounter(name)

  def removeMinMaxCounter(name: String): Unit =
    userMetricsRecorder.removeMinMaxCounter(name)

  def removeGauge(name: String): Unit =
    userMetricsRecorder.removeGauge(name)
}

object UserMetrics extends ExtensionId[UserMetricsExtension] with ExtensionIdProvider with MetricGroupIdentity {
  def lookup(): ExtensionId[_ <: actor.Extension] = Metrics
  def createExtension(system: ExtendedActorSystem): UserMetricsExtension = new UserMetricsExtension(system)

  val name: String = "user-metrics-recorder"
  val category = new MetricGroupCategory {
    val name: String = "user-metrics"
  }

  val Factory = new MetricGroupFactory {
    type GroupRecorder = UserMetricsRecorder
    def create(config: Config, system: ActorSystem): UserMetricsRecorder = new UserMetricsRecorder(system)
  }

  class UserMetricsRecorder(system: ActorSystem) extends MetricGroupRecorder {
    val precisionConfig = system.settings.config.getConfig("kamon.metrics.precision")
    val defaultHistogramPrecisionConfig = precisionConfig.getConfig("default-histogram-precision")
    val defaultMinMaxCounterPrecisionConfig = precisionConfig.getConfig("default-min-max-counter-precision")
    val defaultGaugePrecisionConfig = precisionConfig.getConfig("default-gauge-precision")

    val histograms = TrieMap[String, Histogram]()
    val counters = TrieMap[String, Counter]()
    val minMaxCounters = TrieMap[String, MinMaxCounter]()
    val gauges = TrieMap[String, Gauge]()

    def buildHistogram(name: String, precision: Histogram.Precision, highestTrackableValue: Long): Histogram =
      histograms.getOrElseUpdate(name, Histogram(highestTrackableValue, precision, Scale.Unit))

    def buildHistogram(name: String): Histogram =
      histograms.getOrElseUpdate(name, Histogram.fromConfig(defaultHistogramPrecisionConfig))

    def buildCounter(name: String): Counter =
      counters.getOrElseUpdate(name, Counter())

    def buildMinMaxCounter(name: String, precision: Histogram.Precision, highestTrackableValue: Long,
      refreshInterval: FiniteDuration): MinMaxCounter = {
      minMaxCounters.getOrElseUpdate(name, MinMaxCounter(highestTrackableValue, precision, Scale.Unit, refreshInterval, system))
    }

    def buildMinMaxCounter(name: String): MinMaxCounter =
      minMaxCounters.getOrElseUpdate(name, MinMaxCounter.fromConfig(defaultMinMaxCounterPrecisionConfig, system))

    def buildGauge(name: String, precision: Histogram.Precision, highestTrackableValue: Long,
      refreshInterval: FiniteDuration, currentValueCollector: Gauge.CurrentValueCollector): Gauge =
      gauges.getOrElseUpdate(name, Gauge(precision, highestTrackableValue, Scale.Unit, refreshInterval, system)(currentValueCollector))

    def buildGauge(name: String)(currentValueCollector: Gauge.CurrentValueCollector): Gauge =
      gauges.getOrElseUpdate(name, Gauge.fromConfig(defaultGaugePrecisionConfig, system)(currentValueCollector))

    def removeHistogram(name: String): Unit =
      histograms.remove(name)

    def removeCounter(name: String): Unit =
      counters.remove(name)

    def removeMinMaxCounter(name: String): Unit =
      minMaxCounters.remove(name).map(_.cleanup)

    def removeGauge(name: String): Unit =
      gauges.remove(name).map(_.cleanup)

    def collect(context: CollectionContext): UserMetricsSnapshot = {
      val histogramSnapshots = histograms.map {
        case (name, histogram) 
          (UserHistogram(name), histogram.collect(context))
      } toMap

      val counterSnapshots = counters.map {
        case (name, counter) 
          (UserCounter(name), counter.collect(context))
      } toMap

      val minMaxCounterSnapshots = minMaxCounters.map {
        case (name, minMaxCounter) 
          (UserMinMaxCounter(name), minMaxCounter.collect(context))
      } toMap

      val gaugeSnapshots = gauges.map {
        case (name, gauge) 
          (UserGauge(name), gauge.collect(context))
      } toMap

      UserMetricsSnapshot(histogramSnapshots, counterSnapshots, minMaxCounterSnapshots, gaugeSnapshots)
    }

    def cleanup: Unit = {}
  }

  case class UserHistogram(name: String) extends MetricIdentity
  case class UserCounter(name: String) extends MetricIdentity
  case class UserMinMaxCounter(name: String) extends MetricIdentity
  case class UserGauge(name: String) extends MetricIdentity

  case class UserMetricsSnapshot(histograms: Map[UserHistogram, Histogram.Snapshot],
    counters: Map[UserCounter, Counter.Snapshot],
    minMaxCounters: Map[UserMinMaxCounter, Histogram.Snapshot],
    gauges: Map[UserGauge, Histogram.Snapshot])
      extends MetricGroupSnapshot {

    type GroupSnapshotType = UserMetricsSnapshot

    def merge(that: UserMetricsSnapshot, context: CollectionContext): UserMetricsSnapshot =
      UserMetricsSnapshot(
        combineMaps(histograms, that.histograms)((l, r)  l.merge(r, context)),
        combineMaps(counters, that.counters)((l, r)  l.merge(r, context)),
        combineMaps(minMaxCounters, that.minMaxCounters)((l, r)  l.merge(r, context)),
        combineMaps(gauges, that.gauges)((l, r)  l.merge(r, context)))

    def metrics: Map[MetricIdentity, MetricSnapshot] = histograms ++ counters ++ minMaxCounters ++ gauges
  }

}