|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Ekino New Relic bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Ekino - Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ekino\Bundle\NewRelicBundle\DependencyInjection; |
|
13
|
|
|
|
|
14
|
|
|
use Monolog\Handler\NewRelicHandler; |
|
15
|
|
|
use Symfony\Component\Config\FileLocator; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
19
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This is the class that loads and manages your bundle configuration |
|
23
|
|
|
* |
|
24
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
|
25
|
|
|
*/ |
|
26
|
|
|
class EkinoNewRelicExtension extends Extension |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
32
|
|
|
{ |
|
33
|
|
|
$configuration = new Configuration(); |
|
34
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
35
|
|
|
|
|
36
|
|
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
37
|
|
|
$loader->load('services.xml'); |
|
38
|
|
|
|
|
39
|
|
|
if (!$config['enabled']) { |
|
40
|
|
|
$interactor = 'ekino.new_relic.interactor.blackhole'; |
|
41
|
|
|
} elseif (isset($config['interactor'])) { |
|
42
|
|
|
$interactor = $config['interactor']; |
|
43
|
|
|
} else { |
|
44
|
|
|
// Fallback to see if the extension is loaded or not |
|
45
|
|
|
$interactor = extension_loaded('newrelic') |
|
46
|
|
|
? 'ekino.new_relic.interactor.real' |
|
47
|
|
|
: 'ekino.new_relic.interactor.blackhole'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($config['logging']) { |
|
51
|
|
|
$container->setAlias('ekino.new_relic.interactor', 'ekino.new_relic.interactor.logger'); |
|
52
|
|
|
$container->getDefinition('ekino.new_relic.interactor.logger') |
|
53
|
|
|
->replaceArgument(0, new Reference($interactor)); |
|
54
|
|
|
} else { |
|
55
|
|
|
$container->setAlias('ekino.new_relic.interactor', $interactor); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (!$config['deployment_names']) { |
|
59
|
|
|
$config['deployment_names'] = array_values(array_filter(explode(';', $config['application_name']))); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$container->getDefinition('ekino.new_relic') |
|
63
|
|
|
->replaceArgument(0, $config['application_name']) |
|
64
|
|
|
->replaceArgument(1, $config['api_key']) |
|
65
|
|
|
->replaceArgument(2, $config['license_key']) |
|
66
|
|
|
->replaceArgument(3, $config['xmit']) |
|
67
|
|
|
->replaceArgument(4, $config['deployment_names']) |
|
68
|
|
|
; |
|
69
|
|
|
|
|
70
|
|
|
if (!$config['enabled']) { |
|
71
|
|
|
$config['log_logs']['enabled'] = false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($config['http']['enabled']) { |
|
75
|
|
|
$loader->load('http_listener.xml'); |
|
76
|
|
|
$container->getDefinition('ekino.new_relic.request_listener') |
|
77
|
|
|
->replaceArgument(2, $config['ignored_routes']) |
|
78
|
|
|
->replaceArgument(3, $config['ignored_paths']) |
|
79
|
|
|
->replaceArgument(4, $this->getTransactionNamingService($config)) |
|
80
|
|
|
->replaceArgument(5, $config['using_symfony_cache']) |
|
81
|
|
|
; |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
$container->getDefinition('ekino.new_relic.response_listener') |
|
85
|
|
|
->replaceArgument(2, $config['instrument']) |
|
86
|
|
|
->replaceArgument(3, $config['using_symfony_cache']) |
|
87
|
|
|
; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ($config['log_commands']) { |
|
91
|
|
|
$loader->load('command_listener.xml'); |
|
92
|
|
|
$container->getDefinition('ekino.new_relic.command_listener') |
|
93
|
|
|
->replaceArgument(2, $config['ignored_commands']); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($config['log_exceptions']) { |
|
97
|
|
|
$loader->load('exception_listener.xml'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($config['log_deprecations']) { |
|
101
|
|
|
$loader->load('deprecation_listener.xml'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (!$config['twig']) { |
|
105
|
|
|
$loader->load('twig.xml'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$container->setParameter('ekino.new_relic.log_logs', $config['log_logs']); |
|
109
|
|
|
if ($config['log_logs']['enabled']) { |
|
110
|
|
|
if (!class_exists(NewRelicHandler::class)) { |
|
111
|
|
|
throw new \LogicException('The "symfony/monolog-bundle" package must be installed in order to use "log_logs" option.'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$loader->load('monolog.xml'); |
|
115
|
|
|
$container->setAlias('ekino.new_relic.logs_handler', $config['log_logs']['service']); |
|
116
|
|
|
|
|
117
|
|
|
$level = $config['log_logs']['level']; |
|
118
|
|
|
$container->findDefinition('ekino.new_relic.logs_handler') |
|
119
|
|
|
->replaceArgument(0, is_int($level) ? $level : constant('Monolog\Logger::'.strtoupper($level))) |
|
120
|
|
|
->replaceArgument(2, $config['application_name']); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
private function getTransactionNamingService(array $config): string |
|
125
|
|
|
{ |
|
126
|
|
|
switch ($config['transaction_naming']) { |
|
127
|
|
|
case 'controller': |
|
128
|
|
|
$serviceId = new Reference('ekino.new_relic.transaction_naming_strategy.controller'); |
|
129
|
|
|
break; |
|
130
|
|
|
case 'route': |
|
131
|
|
|
$serviceId = new Reference('ekino.new_relic.transaction_naming_strategy.route'); |
|
132
|
|
|
break; |
|
133
|
|
|
case 'service': |
|
134
|
|
|
if (!isset($config['transaction_naming_service'])) { |
|
135
|
|
|
throw new \LogicException( |
|
136
|
|
|
'When using the "service", transaction naming scheme, the "transaction_naming_service" config parameter must be set.' |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$serviceId = new Reference($config['transaction_naming_service']); |
|
141
|
|
|
break; |
|
142
|
|
|
default: |
|
143
|
|
|
throw new \InvalidArgumentException( |
|
144
|
|
|
sprintf( |
|
145
|
|
|
'Invalid transaction naming scheme "%s", must be "route", "controller" or "service".', |
|
146
|
|
|
$config['transaction_naming'] |
|
147
|
|
|
) |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $serviceId; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|