1.0) { throw new InvalidArgumentException('probability should be be between 0.0 and 1.0.'); } $this->probability = $probability; } /** * Returns `SamplingResult` based on probability. Respects the parent `SampleFlag` * {@inheritdoc} */ public function shouldSample( ContextInterface $parentContext, string $traceId, string $spanName, int $spanKind, AttributesInterface $attributes, array $links ): SamplingResult { // TODO: Add config to adjust which spans get sampled (only default from specification is implemented) $parentSpan = Span::fromContext($parentContext); $parentSpanContext = $parentSpan->getContext(); $traceState = $parentSpanContext->getTraceState(); /** * Since php can only store up to 63 bit positive integers */ $traceIdLimit = (1 << 60) - 1; $lowerOrderBytes = hexdec(substr($traceId, strlen($traceId) - 15, 15)); $traceIdCondition = $lowerOrderBytes < round($this->probability * $traceIdLimit); $decision = $traceIdCondition ? SamplingResult::RECORD_AND_SAMPLE : SamplingResult::DROP; return new SamplingResult($decision, [], $traceState); } public function getDescription(): string { return sprintf('%s{%.6F}', 'TraceIdRatioBasedSampler', $this->probability); } }