generated from dunglas/symfony-docker
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOtelKernelSubscriber.php
200 lines (165 loc) · 6.07 KB
/
OtelKernelSubscriber.php
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
194
195
196
197
198
199
200
<?php
namespace App\EventSubscriber;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\SpanBuilderInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\TracerInterface;
use OpenTelemetry\SDK\Trace\TracerProvider;
use OpenTelemetry\SDK\Trace\SamplerInterface;
use OpenTelemetry\SDK\Trace\RandomIdGenerator;
use OpenTelemetry\SDK\Trace\SamplingResult;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Symfony\OtelSdkBundle;
use Symfony\Contracts\EventDispatcher\Event as BaseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event;
use Symfony\Component\HttpFoundation\Request;
class OtelKernelSubscriber implements EventSubscriberInterface
{
private const DEFAULT_ROOT_SPAN_NAME = 'main';
private TracerProvider $provider;
private ?SpanInterface $mainSpan = null;
private ?SpanInterface $requestSpan = null;
private ?bool $shouldSample = null;
public function __construct(TracerProvider $provider)
{
$this->setTracerProvider($provider);
if($this->shouldSample()){
$this->mainSpan = $this->startSpan(self::DEFAULT_ROOT_SPAN_NAME);
$this->mainSpan->activate();
}
}
public static function getSubscribedEvents(): array
{
// return the subscribed events, their methods and priorities
return [
KernelEvents::REQUEST => [['onRequestEvent', 10000]],
KernelEvents::CONTROLLER => [['onControllerEvent', 10000]],
KernelEvents::CONTROLLER_ARGUMENTS => [['onControllerArgumentsEvent', 10000]],
KernelEvents::VIEW => [['onViewEvent', 10000]],
KernelEvents::RESPONSE => [['onResponseEvent', 10000]],
KernelEvents::FINISH_REQUEST => [['onFinishRequestEvent', -10000]],
KernelEvents::TERMINATE => [['onTerminateEvent', -10000]],
KernelEvents::EXCEPTION => [['onExceptionEvent', 10000]],
];
}
public function onRequestEvent(Event\RequestEvent $event): void
{
if ($this->shouldSample() === false) {
return;
}
$this->addEvent($event);
if ($event->isMainRequest()) {
$this->requestSpan = $this->startSpan('kernel');
$this->mainSpan->updateName($event->getRequest()->getRequestUri());
$this->populateRequestAttributes($event->getRequest());
}
}
public function onControllerEvent(Event\ControllerEvent $event): void
{
if ($this->shouldSample() === false) {
return;
}
$this->addEvent($event);
}
public function onControllerArgumentsEvent(Event\ControllerArgumentsEvent $event): void
{
if ($this->shouldSample() === false) {
return;
}
$this->addEvent($event);
}
public function onViewEvent(Event\ViewEvent $event): void
{
if ($this->shouldSample() === false) {
return;
}
$this->addEvent($event);
}
public function onResponseEvent(Event\ResponseEvent $event): void
{
if ($this->shouldSample() === false) {
return;
}
$this->addEvent($event);
}
public function onFinishRequestEvent(Event\FinishRequestEvent $event): void
{
if ($this->shouldSample() === false) {
return;
}
$this->addEvent($event);
}
public function onTerminateEvent(Event\TerminateEvent $event): void
{
if ($this->shouldSample() === false) {
return;
}
$this->addEvent($event);
if ($event->isMainRequest()) {
$this->requestSpan->end();
$this->mainSpan->end();
}
}
public function onExceptionEvent(Event\ExceptionEvent $event): void
{
if ($this->shouldSample() === false) {
return;
}
$this->mainSpan->recordException($event->getThrowable());
}
private function addEvent(BaseEvent $event): void
{
$this->mainSpan->addEvent(get_class($event));
}
private function populateRequestAttributes(Request $request): void
{
$this->mainSpan->setAttribute('http.method', $request->getMethod());
$this->mainSpan->setAttribute('http.url', $request->getUri());
$this->mainSpan->setAttribute('http.target', $request->getPathInfo());
$this->mainSpan->setAttribute('http.host', $request->getHttpHost());
$this->mainSpan->setAttribute('http.scheme', $request->getScheme());
$this->mainSpan->setAttribute('http.flavor', $request->getProtocolVersion());
$this->mainSpan->setAttribute('http.user_agent', $request->headers->get('user-agent'));
$this->mainSpan->setAttribute('http.request_content_length', $request->headers->get('content-length'));
}
private function setTracerProvider(TracerProvider $provider): void
{
$this->provider = $provider;
}
private function getTracerProvider(): TracerProvider
{
return $this->provider;
}
private function getTracer(): TracerInterface
{
return $this->getTracerProvider()->getTracer(
OtelSdkBundle\DependencyInjection\Tracer::DEFAULT_KEY
);
}
private function getSampler(): SamplerInterface
{
return $this->getTracerProvider()->getSampler();
}
private function getSpanBuilder(string $name): SpanBuilderInterface
{
return $this->getTracer()->spanBuilder($name);
}
private function startSpan(string $name): SpanInterface
{
return $this->getTracer()->spanBuilder($name)->startSpan();
}
private function shouldSample(): bool
{
return is_bool($this->shouldSample)
? $this->shouldSample
: $this->shouldSample =
SamplingResult::RECORD_AND_SAMPLE === $this->getSampler()->shouldSample(
Context::getCurrent(),
(new RandomIdGenerator())->generateTraceId(),
'',
SpanKind::KIND_INTERNAL
)->getDecision();
}
}