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

Breadcrumb

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

class TraceIdRatioBasedSampler

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

Hierarchy

  • class \OpenTelemetry\SDK\Trace\Sampler\TraceIdRatioBasedSampler implements \OpenTelemetry\SDK\Trace\SamplerInterface

Expanded class hierarchy of TraceIdRatioBasedSampler

1 file declares its use of TraceIdRatioBasedSampler
SamplerFactory.php in vendor/open-telemetry/sdk/Trace/SamplerFactory.php
1 string reference to 'TraceIdRatioBasedSampler'
TraceIdRatioBasedSampler::getDescription in vendor/open-telemetry/sdk/Trace/Sampler/TraceIdRatioBasedSampler.php
Returns the sampler name or short description with the configuration. This may be displayed on debug pages or in the logs. Example: "TraceIdRatioBasedSampler{0.000100}"

File

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

Namespace

OpenTelemetry\SDK\Trace\Sampler
View source
class TraceIdRatioBasedSampler implements SamplerInterface {
    private readonly float $probability;
    
    /**
     * @param float $probability Probability float value between 0.0 and 1.0.
     */
    public function __construct(float $probability) {
        if ($probability < 0.0 || $probability > 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);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
TraceIdRatioBasedSampler::$probability private property
TraceIdRatioBasedSampler::getDescription public function Returns the sampler name or short description with the configuration.
This may be displayed on debug pages or in the logs.
Example: &quot;TraceIdRatioBasedSampler{0.000100}&quot;
Overrides SamplerInterface::getDescription
TraceIdRatioBasedSampler::shouldSample public function Returns `SamplingResult` based on probability. Respects the parent `SampleFlag` Overrides SamplerInterface::shouldSample
TraceIdRatioBasedSampler::__construct public function
RSS feed
Powered by Drupal