summaryrefslogtreecommitdiff
path: root/vendor/opentracing/opentracing/src/OpenTracing/GlobalTracer.php
blob: aafdba121a57ae8aa225d9065b5a93ea0e90d47b (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
<?php

declare(strict_types=1);

namespace OpenTracing;

final class GlobalTracer
{
    /**
     * @var Tracer
     */
    private static $instance;

    /**
     * @var bool
     */
    private static $isRegistered = false;

    /**
     * GlobalTracer::set sets the [singleton] Tracer returned by get().
     * Those who use GlobalTracer (rather than directly manage a Tracer instance)
     * should call GlobalTracer::set as early as possible in bootstrap, prior to
     * start a new span. Prior to calling GlobalTracer::set, any Spans started
     * via the `Tracer::startActiveSpan` (etc) globals are noops.
     *
     * @param Tracer $tracer
     * @return void
     */
    public static function set(Tracer $tracer): void
    {
        self::$instance = $tracer;
        self::$isRegistered = true;
    }

    /**
     * GlobalTracer::get returns the global singleton `Tracer` implementation.
     * Before `GlobalTracer::set` is called, the `GlobalTracer::get` is a noop
     * implementation that drops all data handed to it.
     *
     * @return Tracer
     */
    public static function get(): Tracer
    {
        if (self::$instance === null) {
            self::$instance = new NoopTracer();
        }

        return self::$instance;
    }

    /**
     * Returns true if a global tracer has been registered, otherwise returns false.
     *
     * @return bool
     */
    public static function isRegistered(): bool
    {
        return self::$isRegistered;
    }
}