• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FriendsOfOpenTelemetry / opentelemetry-bundle / 8314846445

17 Mar 2024 10:42AM UTC coverage: 88.058% (-0.4%) from 88.505%
8314846445

push

github

gaelreyrol
feat(otel/trace): add attribute based sampler

1 of 12 new or added lines in 2 files covered. (8.33%)

1895 of 2152 relevant lines covered (88.06%)

6.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/OpenTelemetry/Trace/Sampler/AttributeBasedSampler.php
1
<?php
2

3
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Trace\Sampler;
4

5
use OpenTelemetry\Context\ContextInterface;
6
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
7
use OpenTelemetry\SDK\Trace\SamplerInterface;
8
use OpenTelemetry\SDK\Trace\SamplingResult;
9
use OpenTelemetry\SDK\Trace\Span;
10

11
class AttributeBasedSampler implements SamplerInterface
12
{
13
    public function __construct(
14
        private readonly string $attributeName,
15
        private readonly mixed $attributeValue = null,
16
    ) {
NEW
17
    }
×
18

19
    /**
20
     * @param AttributesInterface<mixed> $attributes
21
     */
22
    public function shouldSample(
23
        ContextInterface $parentContext,
24
        string $traceId,
25
        string $spanName,
26
        int $spanKind,
27
        AttributesInterface $attributes,
28
        array $links
29
    ): SamplingResult {
NEW
30
        $parentSpan = Span::fromContext($parentContext);
×
NEW
31
        $parentSpanContext = $parentSpan->getContext();
×
NEW
32
        $traceState = $parentSpanContext->getTraceState();
×
33

NEW
34
        if ($attributes->has($this->attributeName)) {
×
NEW
35
            $attributeValue = $attributes->get($this->attributeName);
×
NEW
36
            if (null !== $attributeValue && $attributeValue !== $this->attributeValue) {
×
NEW
37
                return new SamplingResult(SamplingResult::DROP, [], $traceState);
×
38
            }
39

NEW
40
            return new SamplingResult(SamplingResult::RECORD_AND_SAMPLE, [], $traceState);
×
41
        }
42

NEW
43
        return new SamplingResult(SamplingResult::DROP, [], $traceState);
×
44
    }
45

46
    public function getDescription(): string
47
    {
NEW
48
        return sprintf('AttributeBasedSampler{%s, %s}', $this->attributeName, $this->attributeValue);
×
49
    }
50
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc