summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Trace/TracerSharedState.php
blob: d0540cc1f3bf3b9ebbdf70aabc6577aeffa641b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Trace;

use OpenTelemetry\API\Trace as API; /** @phan-suppress-current-line PhanUnreferencedUseNormal */
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Trace\SpanProcessor\MultiSpanProcessor;
use OpenTelemetry\SDK\Trace\SpanProcessor\NoopSpanProcessor;

/**
 * Stores shared state/config between all {@see API\TracerInterface} created via the same {@see API\TracerProviderInterface}.
 */
final class TracerSharedState
{
    /** @readonly */
    private IdGeneratorInterface $idGenerator;

    /** @readonly */
    private ResourceInfo $resource;

    /** @readonly */
    private SpanLimits $spanLimits;

    /** @readonly */
    private SamplerInterface $sampler;

    /** @readonly */
    private SpanProcessorInterface $spanProcessor;

    private ?bool $shutdownResult = null;

    public function __construct(
        IdGeneratorInterface $idGenerator,
        ResourceInfo $resource,
        SpanLimits $spanLimits,
        SamplerInterface $sampler,
        array $spanProcessors
    ) {
        $this->idGenerator = $idGenerator;
        $this->resource = $resource;
        $this->spanLimits = $spanLimits;
        $this->sampler = $sampler;

        switch (count($spanProcessors)) {
            case 0:
                $this->spanProcessor = NoopSpanProcessor::getInstance();

                break;
            case 1:
                $this->spanProcessor = $spanProcessors[0];

                break;
            default:
                $this->spanProcessor = new MultiSpanProcessor(...$spanProcessors);

                break;
        }
    }

    public function hasShutdown(): bool
    {
        return null !== $this->shutdownResult;
    }

    public function getIdGenerator(): IdGeneratorInterface
    {
        return $this->idGenerator;
    }

    public function getResource(): ResourceInfo
    {
        return $this->resource;
    }

    public function getSpanLimits(): SpanLimits
    {
        return $this->spanLimits;
    }

    public function getSampler(): SamplerInterface
    {
        return $this->sampler;
    }

    public function getSpanProcessor(): SpanProcessorInterface
    {
        return $this->spanProcessor;
    }

    /**
     * Returns `false` is the provider is already shutdown, otherwise `true`.
     */
    public function shutdown(?CancellationInterface $cancellation = null): bool
    {
        return $this->shutdownResult ?? ($this->shutdownResult = $this->spanProcessor->shutdown($cancellation));
    }
}