aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/metrics/Metrics.scala
blob: f07bf38efab2d4faad0038f90eab3d9556252042 (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
/*
 * =========================================================================================
 * Copyright © 2013 the kamon project <http://kamon.io/>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 * =========================================================================================
 */

package kamon.metrics

import annotation.tailrec
import com.typesafe.config.Config
import kamon.metrics.MetricSnapshot.Measurement
import kamon.metrics.InstrumentTypes.InstrumentType

trait MetricGroupCategory {
  def name: String
}

trait MetricGroupIdentity {
  def name: String
  def category: MetricGroupCategory
}

trait MetricIdentity {
  def name: String
  def tag: String
}

trait MetricGroupRecorder {
  def collect: MetricGroupSnapshot
}

trait MetricGroupSnapshot {
  def metrics: Map[MetricIdentity, MetricSnapshotLike]
}

case class DefaultMetricGroupSnapshot(metrics: Map[MetricIdentity, MetricSnapshotLike]) extends MetricGroupSnapshot

trait MetricRecorder {
  def record(value: Long)
  def collect(): MetricSnapshotLike
}

object InstrumentTypes {
  sealed trait InstrumentType
  case object Histogram extends InstrumentType
  case object Gauge extends InstrumentType
  case object Counter extends InstrumentType
}

trait MetricSnapshotLike {
  def instrumentType: InstrumentType
  def numberOfMeasurements: Long
  def scale: Scale
  def measurements: Vector[Measurement]

  def max: Long = measurements.lastOption.map(_.value).getOrElse(0)
  def min: Long = measurements.headOption.map(_.value).getOrElse(0)

  def merge(that: MetricSnapshotLike): MetricSnapshotLike = {
    val mergedMeasurements = Vector.newBuilder[Measurement]

    @tailrec def go(left: Vector[Measurement], right: Vector[Measurement], totalNrOfMeasurements: Long): Long = {
      if (left.nonEmpty && right.nonEmpty) {
        val leftValue = left.head
        val rightValue = right.head

        if (rightValue.value == leftValue.value) {
          val merged = rightValue.merge(leftValue)
          mergedMeasurements += merged
          go(left.tail, right.tail, totalNrOfMeasurements + merged.count)
        } else {
          if (leftValue.value < rightValue.value) {
            mergedMeasurements += leftValue
            go(left.tail, right, totalNrOfMeasurements + leftValue.count)
          } else {
            mergedMeasurements += rightValue
            go(left, right.tail, totalNrOfMeasurements + rightValue.count)
          }
        }
      } else {
        if (left.isEmpty && right.nonEmpty) {
          mergedMeasurements += right.head
          go(left, right.tail, totalNrOfMeasurements + right.head.count)
        } else {
          if (left.nonEmpty && right.isEmpty) {
            mergedMeasurements += left.head
            go(left.tail, right, totalNrOfMeasurements + left.head.count)
          } else totalNrOfMeasurements
        }
      }
    }

    val totalNrOfMeasurements = go(measurements, that.measurements, 0)
    MetricSnapshot(instrumentType, totalNrOfMeasurements, scale, mergedMeasurements.result())
  }
}

case class MetricSnapshot(instrumentType: InstrumentType, numberOfMeasurements: Long, scale: Scale,
                          measurements: Vector[MetricSnapshot.Measurement]) extends MetricSnapshotLike

object MetricSnapshot {
  case class Measurement(value: Long, count: Long) {
    def merge(that: Measurement) = Measurement(value, count + that.count)
  }
}

trait MetricGroupFactory {
  type GroupRecorder <: MetricGroupRecorder
  def create(config: Config): GroupRecorder
}