-
Notifications
You must be signed in to change notification settings - Fork 103
/
RequestListener.php
125 lines (105 loc) · 3.74 KB
/
RequestListener.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
<?php
declare(strict_types=1);
/*
* This file is part of Ekino New Relic bundle.
*
* (c) Ekino - Thomas Rabaix <thomas.rabaix@ekino.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ekino\NewRelicBundle\Listener;
use Ekino\NewRelicBundle\NewRelic\Config;
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
use Ekino\NewRelicBundle\TransactionNamingStrategy\TransactionNamingStrategyInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
class RequestListener implements EventSubscriberInterface
{
private $ignoredRoutes;
private $ignoredPaths;
private $config;
private $interactor;
private $transactionNamingStrategy;
private $symfonyCache;
public function __construct(
Config $config,
NewRelicInteractorInterface $interactor,
array $ignoreRoutes,
array $ignoredPaths,
TransactionNamingStrategyInterface $transactionNamingStrategy,
bool $symfonyCache = false
) {
$this->config = $config;
$this->interactor = $interactor;
$this->ignoredRoutes = $ignoreRoutes;
$this->ignoredPaths = $ignoredPaths;
$this->transactionNamingStrategy = $transactionNamingStrategy;
$this->symfonyCache = $symfonyCache;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => [
['setApplicationName', 255],
['setIgnoreTransaction', 31],
['setTransactionName', -10],
],
];
}
public function setApplicationName(KernelRequestEvent $event): void
{
if (!$this->isEventValid($event)) {
return;
}
$appName = $this->config->getName();
if (!$appName) {
return;
}
if ($this->symfonyCache) {
$this->interactor->startTransaction($appName);
}
// Set application name if different from ini configuration
if ($appName !== ini_get('newrelic.appname')) {
$this->interactor->setApplicationName($appName, $this->config->getLicenseKey(), $this->config->getXmit());
}
}
public function setTransactionName(KernelRequestEvent $event): void
{
if (!$this->isEventValid($event)) {
return;
}
$transactionName = $this->transactionNamingStrategy->getTransactionName($event->getRequest());
$this->interactor->setTransactionName($transactionName);
}
public function setIgnoreTransaction(KernelRequestEvent $event): void
{
if (!$this->isEventValid($event)) {
return;
}
$request = $event->getRequest();
if (\in_array($request->get('_route'), $this->ignoredRoutes, true)) {
$this->interactor->ignoreTransaction();
}
if (\in_array($request->getPathInfo(), $this->ignoredPaths, true)) {
$this->interactor->ignoreTransaction();
}
}
/**
* Make sure we should consider this event. Example: make sure it is a master request.
*/
private function isEventValid(KernelRequestEvent $event): bool
{
return HttpKernelInterface::MASTER_REQUEST === $event->getRequestType();
}
}
if (!class_exists(KernelRequestEvent::class)) {
if (class_exists(RequestEvent::class)) {
class_alias(RequestEvent::class, KernelRequestEvent::class);
} else {
class_alias(GetResponseEvent::class, KernelRequestEvent::class);
}
}