Debian Codegraph @

includes/libs/telemetry/ProbabilisticSampler.php

raw symbols SCIP
1 <?php
2 namespace Wikimedia\Telemetry;
3
4 use Wikimedia\Assert\Assert;
5
6 /**
7 * A {@link SamplerInterface} implementation that samples a given percentage of root spans,
8 * while respecting sampling decisions made by other samplers for a given trace.
9 *
10 * @since 1.43
11 * @internal
12 */
13 class ProbabilisticSampler implements SamplerInterface {
14 /**
15 * The chance of sampling a root span, as a percentage (0-100).
16 * @var int
17 */
18 private int $percentChance;
19
20 public function __construct( int $percentChance ) {
21 Assert::parameter(
22 $percentChance >= 0 && $percentChance <= 100,
23 '$percentChance',
24 'must be between 0 and 100 inclusive'
25 );
26 $this->percentChance = $percentChance;
27 }
28
29 /** @inheritDoc */
30 public function shouldSample( ?SpanContext $parentSpanContext ): bool {
31 if ( $parentSpanContext !== null ) {
32 return $parentSpanContext->isSampled();
33 }
34
35 return mt_rand( 1, 100 ) <= $this->percentChance;
36 }
37 }