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

Breadcrumb

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

class ParentBased

This implementation of the SamplerInterface that respects parent context's sampling decision and delegates for the root span. Example: ``` use OpenTelemetry\API\Trace\ParentBased; use OpenTelemetry\API\Trace\AlwaysOnSampler

$rootSampler = new AlwaysOnSampler(); $sampler = new ParentBased($rootSampler); ```

Hierarchy

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

Expanded class hierarchy of ParentBased

2 files declare their use of ParentBased
SamplerFactory.php in vendor/open-telemetry/sdk/Trace/SamplerFactory.php
TracerProvider.php in vendor/open-telemetry/sdk/Trace/TracerProvider.php

File

vendor/open-telemetry/sdk/Trace/Sampler/ParentBased.php, line 30

Namespace

OpenTelemetry\SDK\Trace\Sampler
View source
class ParentBased implements SamplerInterface {
    private readonly SamplerInterface $remoteParentSampler;
    private readonly SamplerInterface $remoteParentNotSampler;
    private readonly SamplerInterface $localParentSampler;
    private readonly SamplerInterface $localParentNotSampler;
    
    /**
     * ParentBased sampler delegates the sampling decision based on the parent context.
     *
     * @param SamplerInterface $root Sampler called for the span with no parent (root span).
     * @param SamplerInterface|null $remoteParentSampler Sampler called for the span with the remote sampled parent. When null, `AlwaysOnSampler` is used.
     * @param SamplerInterface|null $remoteParentNotSampler Sampler called for the span with the remote not sampled parent. When null, `AlwaysOffSampler` is used.
     * @param SamplerInterface|null $localParentSampler Sampler called for the span with local the sampled parent. When null, `AlwaysOnSampler` is used.
     * @param SamplerInterface|null $localParentNotSampler Sampler called for the span with the local not sampled parent. When null, `AlwaysOffSampler` is used.
     */
    public function __construct(SamplerInterface $root, ?SamplerInterface $remoteParentSampler = null, ?SamplerInterface $remoteParentNotSampler = null, ?SamplerInterface $localParentSampler = null, ?SamplerInterface $localParentNotSampler = null) {
        $this->remoteParentSampler = $remoteParentSampler ?? new AlwaysOnSampler();
        $this->remoteParentNotSampler = $remoteParentNotSampler ?? new AlwaysOffSampler();
        $this->localParentSampler = $localParentSampler ?? new AlwaysOnSampler();
        $this->localParentNotSampler = $localParentNotSampler ?? new AlwaysOffSampler();
    }
    
    /**
     * Invokes the respective delegate sampler when parent is set or uses root sampler for the root span.
     * {@inheritdoc}
     */
    public function shouldSample(ContextInterface $parentContext, string $traceId, string $spanName, int $spanKind, AttributesInterface $attributes, array $links) : SamplingResult {
        $parentSpan = Span::fromContext($parentContext);
        $parentSpanContext = $parentSpan->getContext();
        // Invalid parent SpanContext indicates root span is being created
        if (!$parentSpanContext->isValid()) {
            return $this->root
                ->shouldSample(...func_get_args());
        }
        if ($parentSpanContext->isRemote()) {
            return $parentSpanContext->isSampled() ? $this->remoteParentSampler
                ->shouldSample(...func_get_args()) : $this->remoteParentNotSampler
                ->shouldSample(...func_get_args());
        }
        return $parentSpanContext->isSampled() ? $this->localParentSampler
            ->shouldSample(...func_get_args()) : $this->localParentNotSampler
            ->shouldSample(...func_get_args());
    }
    public function getDescription() : string {
        return 'ParentBased+' . $this->root
            ->getDescription();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ParentBased::$localParentNotSampler private property
ParentBased::$localParentSampler private property
ParentBased::$remoteParentNotSampler private property
ParentBased::$remoteParentSampler private property
ParentBased::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: "TraceIdRatioBasedSampler{0.000100}"
Overrides SamplerInterface::getDescription
ParentBased::shouldSample public function Invokes the respective delegate sampler when parent is set or uses root sampler for the root span. Overrides SamplerInterface::shouldSample
ParentBased::__construct public function ParentBased sampler delegates the sampling decision based on the parent context.
RSS feed
Powered by Drupal