|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Ekino New Relic bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Ekino - Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Ekino\NewRelicBundle\Listener; |
|
15
|
|
|
|
|
16
|
|
|
use Ekino\NewRelicBundle\NewRelic\Config; |
|
17
|
|
|
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface; |
|
18
|
|
|
use Ekino\NewRelicBundle\Twig\NewRelicExtension; |
|
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
21
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
|
|
|
|
|
|
22
|
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent; |
|
23
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
24
|
|
|
|
|
25
|
|
|
class ResponseListener implements EventSubscriberInterface |
|
26
|
|
|
{ |
|
27
|
|
|
private $newRelic; |
|
28
|
|
|
private $interactor; |
|
29
|
|
|
private $instrument; |
|
30
|
|
|
private $symfonyCache; |
|
31
|
|
|
private $newRelicTwigExtension; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct( |
|
34
|
|
|
Config $newRelic, |
|
35
|
|
|
NewRelicInteractorInterface $interactor, |
|
36
|
|
|
bool $instrument = false, |
|
37
|
|
|
bool $symfonyCache = false, |
|
38
|
|
|
NewRelicExtension $newRelicTwigExtension = null |
|
39
|
|
|
) { |
|
40
|
|
|
$this->newRelic = $newRelic; |
|
41
|
|
|
$this->interactor = $interactor; |
|
42
|
|
|
$this->instrument = $instrument; |
|
43
|
|
|
$this->symfonyCache = $symfonyCache; |
|
44
|
|
|
$this->newRelicTwigExtension = $newRelicTwigExtension; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public static function getSubscribedEvents(): array |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
KernelEvents::RESPONSE => [ |
|
51
|
|
|
['onKernelResponse', -255], |
|
52
|
|
|
], |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param FilterResponseEvent|ResponseEvent $event |
|
58
|
|
|
*/ |
|
59
|
|
|
public function onKernelResponse($event): void |
|
60
|
|
|
{ |
|
61
|
|
|
if (!$event instanceof FilterResponseEvent && !$event instanceof ResponseEvent) { |
|
|
|
|
|
|
62
|
|
|
throw new \InvalidArgumentException(\sprintf('Expected instance of type %s, %s given', \class_exists(ResponseEvent::class) ? ResponseEvent::class : FilterResponseEvent::class, \is_object($event) ? \get_class($event) : \gettype($event))); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (!$event->isMasterRequest()) { |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isUsed()) { |
|
70
|
|
|
foreach ($this->newRelic->getCustomMetrics() as $name => $value) { |
|
71
|
|
|
$this->interactor->addCustomMetric((string) $name, (float) $value); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
foreach ($this->newRelic->getCustomParameters() as $name => $value) { |
|
75
|
|
|
$this->interactor->addCustomParameter((string) $name, $value); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
foreach ($this->newRelic->getCustomEvents() as $name => $events) { |
|
80
|
|
|
foreach ($events as $attributes) { |
|
81
|
|
|
$this->interactor->addCustomEvent((string) $name, $attributes); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($this->instrument) { |
|
86
|
|
|
if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isUsed()) { |
|
87
|
|
|
$this->interactor->disableAutoRUM(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Some requests might not want to get instrumented |
|
91
|
|
|
if ($event->getRequest()->attributes->get('_instrument', true)) { |
|
92
|
|
|
$response = $event->getResponse(); |
|
93
|
|
|
|
|
94
|
|
|
// We can only instrument HTML responses |
|
95
|
|
|
if (!$response instanceof StreamedResponse |
|
96
|
|
|
&& 'text/html' === \substr($response->headers->get('Content-Type', ''), 0, 9) |
|
97
|
|
|
) { |
|
98
|
|
|
$responseContent = $response->getContent(); |
|
99
|
|
|
$response->setContent(''); // free the memory |
|
100
|
|
|
|
|
101
|
|
|
if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isHeaderCalled()) { |
|
102
|
|
|
$responseContent = \preg_replace('|<head>|i', '$0'.$this->interactor->getBrowserTimingHeader(), $responseContent); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isFooterCalled()) { |
|
106
|
|
|
$responseContent = \preg_replace('|</body>|i', $this->interactor->getBrowserTimingFooter().'$0', $responseContent); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$response->setContent($responseContent); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if ($this->symfonyCache) { |
|
115
|
|
|
$this->interactor->endTransaction(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths