aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/metric/UserMetrics.scala
blob: b511b4bc98a1024c818ea70de978e9e69b859be4 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package kamon.metric

import akka.actor
import akka.actor.{ ExtendedActorSystem, ExtensionIdProvider, ExtensionId }
import kamon.Kamon
import kamon.metric.instrument.{ Gauge, MinMaxCounter, Counter, Histogram }

import scala.concurrent.duration.FiniteDuration

class UserMetricsExtension(system: ExtendedActorSystem) extends Kamon.Extension {
  import UserMetrics._

  lazy val metricsExtension = Kamon(Metrics)(system)
  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")

  def registerHistogram(name: String, precision: Histogram.Precision, highestTrackableValue: Long): Histogram = {
    metricsExtension.storage.getOrElseUpdate(UserHistogram(name), {
      UserHistogramRecorder(Histogram(highestTrackableValue, precision, Scale.Unit))
    }).asInstanceOf[UserHistogramRecorder].histogram
  }

  def registerHistogram(name: String): Histogram = {
    metricsExtension.storage.getOrElseUpdate(UserHistogram(name), {
      UserHistogramRecorder(Histogram.fromConfig(defaultHistogramPrecisionConfig))
    }).asInstanceOf[UserHistogramRecorder].histogram
  }

  def registerCounter(name: String): Counter = {
    metricsExtension.storage.getOrElseUpdate(UserCounter(name), {
      UserCounterRecorder(Counter())
    }).asInstanceOf[UserCounterRecorder].counter
  }

  def registerMinMaxCounter(name: String, precision: Histogram.Precision, highestTrackableValue: Long,
    refreshInterval: FiniteDuration): MinMaxCounter = {
    metricsExtension.storage.getOrElseUpdate(UserMinMaxCounter(name), {
      UserMinMaxCounterRecorder(MinMaxCounter(highestTrackableValue, precision, Scale.Unit, refreshInterval, system))
    }).asInstanceOf[UserMinMaxCounterRecorder].minMaxCounter
  }

  def registerMinMaxCounter(name: String): MinMaxCounter = {
    metricsExtension.storage.getOrElseUpdate(UserMinMaxCounter(name), {
      UserMinMaxCounterRecorder(MinMaxCounter.fromConfig(defaultMinMaxCounterPrecisionConfig, system))
    }).asInstanceOf[UserMinMaxCounterRecorder].minMaxCounter
  }

  def registerGauge(name: String)(currentValueCollector: Gauge.CurrentValueCollector): Gauge = {
    metricsExtension.storage.getOrElseUpdate(UserGauge(name), {
      UserGaugeRecorder(Gauge.fromConfig(defaultGaugePrecisionConfig, system)(currentValueCollector))
    }).asInstanceOf[UserGaugeRecorder].gauge
  }

  def registerGauge(name: String, precision: Histogram.Precision, highestTrackableValue: Long,
    refreshInterval: FiniteDuration)(currentValueCollector: Gauge.CurrentValueCollector): Gauge = {
    metricsExtension.storage.getOrElseUpdate(UserGauge(name), {
      UserGaugeRecorder(Gauge(precision, highestTrackableValue, Scale.Unit, refreshInterval, system)(currentValueCollector))
    }).asInstanceOf[UserGaugeRecorder].gauge
  }

  def removeHistogram(name: String): Unit =
    metricsExtension.unregister(UserHistogram(name))

  def removeCounter(name: String): Unit =
    metricsExtension.unregister(UserCounter(name))

  def removeMinMaxCounter(name: String): Unit =
    metricsExtension.unregister(UserMinMaxCounter(name))

  def removeGauge(name: String): Unit =
    metricsExtension.unregister(UserGauge(name))
}

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

  def createExtension(system: ExtendedActorSystem): UserMetricsExtension = new UserMetricsExtension(system)

  sealed trait UserMetricGroup
  //
  // Histograms
  //

  case class UserHistogram(name: String) extends MetricGroupIdentity with UserMetricGroup {
    val category = UserHistograms
  }

  case class UserHistogramRecorder(histogram: Histogram) extends MetricGroupRecorder {
    def collect(context: CollectionContext): MetricGroupSnapshot =
      UserHistogramSnapshot(histogram.collect(context))

    def cleanup: Unit = histogram.cleanup
  }

  case class UserHistogramSnapshot(histogramSnapshot: Histogram.Snapshot) extends MetricGroupSnapshot {
    type GroupSnapshotType = UserHistogramSnapshot

    def merge(that: UserHistogramSnapshot, context: CollectionContext): UserHistogramSnapshot =
      UserHistogramSnapshot(that.histogramSnapshot.merge(histogramSnapshot, context))

    def metrics: Map[MetricIdentity, MetricSnapshot] = Map((RecordedValues, histogramSnapshot))
  }

  //
  // Counters
  //

  case class UserCounter(name: String) extends MetricGroupIdentity with UserMetricGroup {
    val category = UserCounters
  }

  case class UserCounterRecorder(counter: Counter) extends MetricGroupRecorder {
    def collect(context: CollectionContext): MetricGroupSnapshot =
      UserCounterSnapshot(counter.collect(context))

    def cleanup: Unit = counter.cleanup
  }

  case class UserCounterSnapshot(counterSnapshot: Counter.Snapshot) extends MetricGroupSnapshot {
    type GroupSnapshotType = UserCounterSnapshot

    def merge(that: UserCounterSnapshot, context: CollectionContext): UserCounterSnapshot =
      UserCounterSnapshot(that.counterSnapshot.merge(counterSnapshot, context))

    def metrics: Map[MetricIdentity, MetricSnapshot] = Map((Count, counterSnapshot))
  }

  //
  // MinMaxCounters
  //

  case class UserMinMaxCounter(name: String) extends MetricGroupIdentity with UserMetricGroup {
    val category = UserMinMaxCounters
  }

  case class UserMinMaxCounterRecorder(minMaxCounter: MinMaxCounter) extends MetricGroupRecorder {
    def collect(context: CollectionContext): MetricGroupSnapshot =
      UserMinMaxCounterSnapshot(minMaxCounter.collect(context))

    def cleanup: Unit = minMaxCounter.cleanup
  }

  case class UserMinMaxCounterSnapshot(minMaxCounterSnapshot: Histogram.Snapshot) extends MetricGroupSnapshot {
    type GroupSnapshotType = UserMinMaxCounterSnapshot

    def merge(that: UserMinMaxCounterSnapshot, context: CollectionContext): UserMinMaxCounterSnapshot =
      UserMinMaxCounterSnapshot(that.minMaxCounterSnapshot.merge(minMaxCounterSnapshot, context))

    def metrics: Map[MetricIdentity, MetricSnapshot] = Map((RecordedValues, minMaxCounterSnapshot))
  }

  //
  // Gauges
  //

  case class UserGauge(name: String) extends MetricGroupIdentity with UserMetricGroup {
    val category = UserGauges
  }

  case class UserGaugeRecorder(gauge: Gauge) extends MetricGroupRecorder {
    def collect(context: CollectionContext): MetricGroupSnapshot =
      UserGaugeSnapshot(gauge.collect(context))

    def cleanup: Unit = gauge.cleanup
  }

  case class UserGaugeSnapshot(gaugeSnapshot: Histogram.Snapshot) extends MetricGroupSnapshot {
    type GroupSnapshotType = UserGaugeSnapshot

    def merge(that: UserGaugeSnapshot, context: CollectionContext): UserGaugeSnapshot =
      UserGaugeSnapshot(that.gaugeSnapshot.merge(gaugeSnapshot, context))

    def metrics: Map[MetricIdentity, MetricSnapshot] = Map((RecordedValues, gaugeSnapshot))
  }

  case object UserHistograms extends MetricGroupCategory { val name: String = "histogram" }
  case object UserCounters extends MetricGroupCategory { val name: String = "counter" }
  case object UserMinMaxCounters extends MetricGroupCategory { val name: String = "min-max-counter" }
  case object UserGauges extends MetricGroupCategory { val name: String = "gauge" }

  case object RecordedValues extends MetricIdentity { val name: String = "values" }
  case object Count extends MetricIdentity { val name: String = "count" }

}