-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Sampler type and implementation that aligns with Jaeger sampling (#…
- Loading branch information
1 parent
7f2a476
commit e3a884d
Showing
5 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
misk-jaeger/src/main/kotlin/misk/tracing/backends/jaeger/SpanSampler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package misk.tracing.backends.jaeger | ||
|
||
import com.uber.jaeger.samplers.ProbabilisticSampler | ||
import io.opentracing.Span | ||
import io.opentracing.Tracer | ||
import misk.sampling.Sampler | ||
import com.uber.jaeger.Span as JaegerSpan | ||
|
||
/** | ||
* A sampler that deterministically samples based on the current tracing span ID. | ||
* Sampling rate when no span is present is 1.0. | ||
* | ||
* @param samplingRate Sampling rate. 0 is never and 1 is always. | ||
*/ | ||
class SpanSampler( | ||
private val tracer: Tracer, | ||
samplingRate: Double | ||
) : Sampler { | ||
|
||
init { | ||
check(samplingRate in 0.0..1.0) { "sampling rate must be in range [0, 1]" } | ||
} | ||
|
||
private val sampler = ProbabilisticSampler(samplingRate) | ||
|
||
/** Returns true if the sampling threshold is met for the current Jaeger span or there is no current span */ | ||
override fun sample(): Boolean { | ||
val spanId: Long? = when (val span: Span? = tracer.activeSpan()) { | ||
is JaegerSpan -> span.context().spanId | ||
else -> null | ||
} | ||
|
||
return spanId == null || sampler.sample("irrelevant", spanId).isSampled | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
misk-jaeger/src/main/kotlin/misk/tracing/backends/jaeger/Tracing.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package misk.tracing.backends.jaeger | ||
|
||
import javax.inject.Qualifier | ||
|
||
@Qualifier | ||
@Target(AnnotationTarget.FIELD, AnnotationTarget.FUNCTION, AnnotationTarget.VALUE_PARAMETER) | ||
annotation class Tracing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package misk.sampling | ||
|
||
import java.util.concurrent.ThreadLocalRandom | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
interface Sampler { | ||
/** If an action should be taken based on the implementation's policy, returns true */ | ||
fun sample(): Boolean | ||
|
||
/** If [sample] returns true, runs the given lambda */ | ||
fun sampledCall(f: () -> Unit) { | ||
if (sample()) { | ||
f() | ||
} | ||
} | ||
} | ||
|
||
/** A [Sampler] randomly invokes an action based on a sample percentage */ | ||
class PercentSampler( | ||
val samplePercentage: () -> Int, | ||
val random: () -> Int = { ThreadLocalRandom.current().nextInt(0, 100) } | ||
) : Sampler { | ||
constructor(samplePercentage: Int, random: () -> Int) : this({ samplePercentage }, random) | ||
constructor(samplePercentage: Int) : this({ samplePercentage }) | ||
|
||
override fun sample(): Boolean = random() < samplePercentage() | ||
} | ||
|
||
/** Sampler that always invokes an action */ | ||
@Singleton | ||
class AlwaysSampler @Inject constructor() : Sampler { | ||
override fun sample(): Boolean = true | ||
} |