Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. TraceIdRatioBasedSampler.php

function TraceIdRatioBasedSampler::shouldSample

Returns `SamplingResult` based on probability. Respects the parent `SampleFlag`

Overrides SamplerInterface::shouldSample

File

vendor/open-telemetry/sdk/Trace/Sampler/TraceIdRatioBasedSampler.php, line 41

Class

TraceIdRatioBasedSampler
This implementation of the SamplerInterface records with given probability. Example: ``` use OpenTelemetry\API\Trace\TraceIdRatioBasedSampler; $sampler = new TraceIdRatioBasedSampler(0.01); ```

Namespace

OpenTelemetry\SDK\Trace\Sampler

Code

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);
}
RSS feed
Powered by Drupal