summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/api/Instrumentation/InstrumentationTrait.php
blob: 1e695adb5035fb563a6f135819325a43cf9d45ef (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php

declare(strict_types=1);

namespace OpenTelemetry\API\Instrumentation;

use OpenTelemetry\API\Metrics\MeterInterface;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Metrics\Noop\NoopMeter;
use OpenTelemetry\API\Trace\NoopTracer;
use OpenTelemetry\API\Trace\NoopTracerProvider;
use OpenTelemetry\API\Trace\TracerInterface;
use OpenTelemetry\API\Trace\TracerProviderInterface;
use OpenTelemetry\Context\Propagation\NoopTextMapPropagator;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use RuntimeException;

/**
 This trait in conjunction with the InstrumentationInterface is meant as a base for instrumentations for the
 OpenTelemetry API.
 Instrumentations need to implement the abstract methods of this trait (besides any instrumentation specific code)

 A very simplified instrumentation could look like this:

class Instrumentation implements InstrumentationInterface
{
    use InstrumentationTrait;

    public function getName(): string
    {
        return 'foo-instrumentation';
    }

    public function getVersion(): ?string
    {
        return '0.0.1';
    }

    public function getSchemaUrl(): ?string
    {
        return null;
    }

    public function init(): bool
    {
        // This is just an example. In a real-world scenario one should only create spans in reaction of things
        // happening in the instrumented code, not just for the sake of it.
        $span = $this->getTracer()->spanBuilder($this->getName())->startSpan();
        // do stuff
        $span->end();
    }
}

An user of the instrumentation and API/SDK would the call:

$instrumentation = new Instrumentation;
$instrumentation->activate()

to activate and use the instrumentation with the API/SDK.
 **/

trait InstrumentationTrait
{
    private TextMapPropagatorInterface $propagator;
    private TracerProviderInterface $tracerProvider;
    private TracerInterface $tracer;
    private MeterInterface $meter;
    private LoggerInterface $logger;

    public function __construct()
    {
        $this->initDefaults();
    }

    /**
     * The name of the instrumenting/instrumented library/package/project.
     * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.12.0/specification/glossary.md#instrumentation-scope
     * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.12.0/specification/glossary.md#instrumentation-library
     */
    abstract public function getName(): string;

    /**
     * The version of the instrumenting/instrumented library/package/project.
     * If unknown or a lookup is too expensive simply return NULL.
     * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.12.0/specification/glossary.md#instrumentation-scope
     * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.12.0/specification/glossary.md#instrumentation-library
     */
    abstract public function getVersion(): ?string;

    /**
     * The version of the instrumenting/instrumented library/package/project.
     * If unknown simply return NULL.
     * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.12.0/specification/glossary.md#instrumentation-scope
     * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.12.0/specification/glossary.md#instrumentation-library
     */
    abstract public function getSchemaUrl(): ?string;

    /**
     * This method will be called from the API when the instrumentation has been activated (via activate()).
     * Here you can put any bootstrapping code needed by the instrumentation.
     * If not needed simply implement a method which returns TRUE.
     */
    abstract public function init(): bool;

    /**
     * This method registers and activates the instrumentation with the OpenTelemetry API/SDK and thus
     * the instrumentation will be used to generate telemetry data.
     */
    public function activate(): bool
    {
        $this->validateImplementation();
        // activate instrumentation with the API. not implemented yet.
        return true;
    }

    public function setPropagator(TextMapPropagatorInterface $propagator): void
    {
        $this->propagator = $propagator;
    }

    public function getPropagator(): TextMapPropagatorInterface
    {
        return $this->propagator;
    }

    public function setTracerProvider(TracerProviderInterface $tracerProvider): void
    {
        $this->tracerProvider = $tracerProvider;
        // @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.12.0/specification/trace/api.md#get-a-tracer
        $this->tracer = $tracerProvider->getTracer(
            $this->getName(),
            $this->getVersion(),
            $this->getSchemaUrl(),
        );
    }

    public function getTracerProvider(): TracerProviderInterface
    {
        return $this->tracerProvider;
    }

    public function getTracer(): TracerInterface
    {
        return $this->tracer;
    }

    public function setMeterProvider(MeterProviderInterface $meterProvider): void
    {
        // @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.12.0/specification/metrics/api.md#get-a-meter
        $this->meter = $meterProvider->getMeter(
            $this->getName(),
            $this->getVersion(),
        );
    }

    public function getMeter(): MeterInterface
    {
        return $this->meter;
    }

    public function setLogger(LoggerInterface $logger): void
    {
        $this->logger = $logger;
    }

    public function getLogger(): LoggerInterface
    {
        return $this->logger;
    }

    private function validateImplementation(): void
    {
        if (!$this instanceof InstrumentationInterface) {
            throw new RuntimeException(sprintf(
                '"%s" is meant to implement "%s"',
                InstrumentationTrait::class,
                InstrumentationInterface::class
            ));
        }
    }

    private function initDefaults(): void
    {
        $this->propagator = new NoopTextMapPropagator();
        $this->tracer = new NoopTracer();
        $this->tracerProvider = new NoopTracerProvider();
        /** @phan-suppress-next-line PhanAccessMethodInternal */
        $this->meter = new NoopMeter();
        $this->logger = new NullLogger();
    }
}