aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/trace/Sampler.scala
blob: 60a400f8b45b9e585676f6dcf588d0aed9080234 (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
package kamon.trace

import java.util.concurrent.atomic.AtomicLong

import scala.concurrent.forkjoin.ThreadLocalRandom

trait Sampler {
  def shouldTrace: Boolean
  def shouldReport(traceElapsedNanoTime: Long): Boolean
}

object NoSampling extends Sampler {
  def shouldTrace: Boolean = false
  def shouldReport(traceElapsedNanoTime: Long): Boolean = false
}

object SampleAll extends Sampler {
  def shouldTrace: Boolean = true
  def shouldReport(traceElapsedNanoTime: Long): Boolean = true
}

class RandomSampler(chance: Int) extends Sampler {
  require(chance > 0, "kamon.trace.random-sampler.chance cannot be <= 0")
  require(chance <= 100, "kamon.trace.random-sampler.chance cannot be > 100")

  def shouldTrace: Boolean = ThreadLocalRandom.current().nextInt(100) <= chance
  def shouldReport(traceElapsedNanoTime: Long): Boolean = true
}

class OrderedSampler(interval: Int) extends Sampler {
  require(interval > 0, "kamon.trace.ordered-sampler.interval cannot be <= 0")

  private val counter = new AtomicLong(0L)
  def shouldTrace: Boolean = counter.incrementAndGet() % interval == 0
  // TODO: find a more efficient way to do this, protect from long overflow.
  def shouldReport(traceElapsedNanoTime: Long): Boolean = true
}

class ThresholdSampler(thresholdInNanoseconds: Long) extends Sampler {
  require(thresholdInNanoseconds > 0, "kamon.trace.threshold-sampler.minimum-elapsed-time cannot be <= 0")

  def shouldTrace: Boolean = true
  def shouldReport(traceElapsedNanoTime: Long): Boolean = traceElapsedNanoTime >= thresholdInNanoseconds
}