aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/metric/instrument/Counter.scala
blob: 10b9c3a6daae7e596dcc83c458665fb5b5495273 (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
package kamon
package metric
package instrument

import java.util.concurrent.atomic.LongAdder

import com.typesafe.scalalogging.StrictLogging
import kamon.util.MeasurementUnit

trait Counter {
  def measurementUnit: MeasurementUnit

  def increment(): Unit
  def increment(times: Long): Unit
}

class LongAdderCounter(entity: Entity, name: String, val measurementUnit: MeasurementUnit)
    extends Counter with SingleValueSnapshotInstrument with StrictLogging {

  private val adder = new LongAdder()

  def increment(): Unit =
    adder.increment()

  def increment(times: Long): Unit = {
    if (times >= 0)
      adder.add(times)
    else
      logger.warn(s"Ignored attempt to decrement counter [$name] on entity [$entity]")
  }

  def snapshot(): SingleValueSnapshot =
    SingleValueSnapshot(name, measurementUnit, adder.sumThenReset())
}