blob: 5e6bef8950aae9883921d70334c87bc7b4b5d272 (
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
|
package kamon.trace
import java.util.concurrent.atomic.AtomicLong
import kamon.NanoInterval
import kamon.util.Sequencer
import scala.concurrent.forkjoin.ThreadLocalRandom
trait Sampler {
def shouldTrace: Boolean
def shouldReport(traceElapsedTime: NanoInterval): Boolean
}
object NoSampling extends Sampler {
def shouldTrace: Boolean = false
def shouldReport(traceElapsedTime: NanoInterval): Boolean = false
}
object SampleAll extends Sampler {
def shouldTrace: Boolean = true
def shouldReport(traceElapsedTime: NanoInterval): 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(traceElapsedTime: NanoInterval): Boolean = true
}
class OrderedSampler(interval: Int) extends Sampler {
require(interval > 0, "kamon.trace.ordered-sampler.interval cannot be <= 0")
private val sequencer = Sequencer()
def shouldTrace: Boolean = sequencer.next() % interval == 0
def shouldReport(traceElapsedTime: NanoInterval): 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(traceElapsedTime: NanoInterval): Boolean = traceElapsedTime.nanos >= thresholdInNanoseconds
}
|