forked from getsentry/sentry-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentryExtension.php
356 lines (298 loc) · 14 KB
/
SentryExtension.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
declare(strict_types=1);
namespace Sentry\SentryBundle\DependencyInjection;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Psr\Log\NullLogger;
use Sentry\Client;
use Sentry\ClientBuilder;
use Sentry\Integration\IgnoreErrorsIntegration;
use Sentry\Integration\IntegrationInterface;
use Sentry\Integration\RequestFetcherInterface;
use Sentry\Integration\RequestIntegration;
use Sentry\Options;
use Sentry\SentryBundle\EventListener\ConsoleListener;
use Sentry\SentryBundle\EventListener\ErrorListener;
use Sentry\SentryBundle\EventListener\MessengerListener;
use Sentry\SentryBundle\EventListener\TracingConsoleListener;
use Sentry\SentryBundle\EventListener\TracingRequestListener;
use Sentry\SentryBundle\EventListener\TracingSubRequestListener;
use Sentry\SentryBundle\Integration\IntegrationConfigurator;
use Sentry\SentryBundle\Monitor\MonitorFactory;
use Sentry\SentryBundle\Monitor\MonitorFactoryInterface;
use Sentry\SentryBundle\SentryBundle;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\ConnectionConfigurator;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverMiddleware;
use Sentry\SentryBundle\Tracing\Twig\TwigTracingExtension;
use Sentry\Serializer\RepresentationSerializer;
use Sentry\Serializer\Serializer;
use Sentry\Transport\TransportFactoryInterface;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Debug\Exception\FatalErrorException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ErrorHandler\Error\FatalError;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
final class SentryExtension extends ConfigurableExtension
{
public function getXsdValidationBasePath(): string
{
return __DIR__ . '/../Resources/config/schema';
}
public function getNamespace(): string
{
return 'https://sentry.io/schema/dic/sentry-symfony';
}
/**
* @param array<array-key, mixed> $mergedConfig
*/
protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
$this->registerConfiguration($container, $mergedConfig);
$this->registerErrorListenerConfiguration($container, $mergedConfig);
$this->registerMessengerListenerConfiguration($container, $mergedConfig['messenger']);
$this->registerTracingConfiguration($container, $mergedConfig['tracing']);
$this->registerDbalTracingConfiguration($container, $mergedConfig['tracing']);
$this->registerTwigTracingConfiguration($container, $mergedConfig['tracing']);
$this->registerCacheTracingConfiguration($container, $mergedConfig['tracing']);
$this->registerHttpClientTracingConfiguration($container, $mergedConfig['tracing']);
}
/**
* @param array<string, mixed> $config
*/
private function registerConfiguration(ContainerBuilder $container, array $config): void
{
$options = $config['options'];
if (\array_key_exists('dsn', $config)) {
$options['dsn'] = $config['dsn'];
}
if (!$container->hasParameter('kernel.build_dir')) {
$options['in_app_exclude'] = array_filter($options['in_app_exclude'], static function (string $value): bool {
return '%kernel.build_dir%' !== $value;
});
}
if (isset($options['traces_sampler'])) {
$options['traces_sampler'] = new Reference($options['traces_sampler']);
}
if (isset($options['before_send'])) {
$options['before_send'] = new Reference($options['before_send']);
}
if (isset($options['before_send_transaction'])) {
$options['before_send_transaction'] = new Reference($options['before_send_transaction']);
}
if (isset($options['before_breadcrumb'])) {
$options['before_breadcrumb'] = new Reference($options['before_breadcrumb']);
}
if (isset($options['class_serializers'])) {
$options['class_serializers'] = array_map(static function (string $value): Reference {
return new Reference($value);
}, $options['class_serializers']);
}
$container->getDefinition(IntegrationConfigurator::class)
->setArgument(0, $this->configureIntegrationsOption($options['integrations'], $config))
->setArgument(1, $config['register_error_handler']);
$options['integrations'] = new Reference(IntegrationConfigurator::class);
$container
->register('sentry.client.options', Options::class)
->setPublic(false)
->setArgument(0, $options);
$serializer = (new Definition(Serializer::class))
->setPublic(false)
->setArgument(0, new Reference('sentry.client.options'));
$representationSerializerDefinition = (new Definition(RepresentationSerializer::class))
->setPublic(false)
->setArgument(0, new Reference('sentry.client.options'));
$loggerReference = null === $config['logger']
? new Reference(NullLogger::class, ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)
: new Reference($config['logger']);
$factoryBuilderDefinition = $container->getDefinition(TransportFactoryInterface::class);
$factoryBuilderDefinition->setArgument('$logger', $loggerReference);
$clientBuilderDefinition = (new Definition(ClientBuilder::class))
->setArgument(0, new Reference('sentry.client.options'))
->addMethodCall('setSdkIdentifier', [SentryBundle::SDK_IDENTIFIER])
->addMethodCall('setSdkVersion', [SentryBundle::SDK_VERSION])
->addMethodCall('setTransportFactory', [new Reference($config['transport_factory'])])
->addMethodCall('setSerializer', [$serializer])
->addMethodCall('setRepresentationSerializer', [$representationSerializerDefinition])
->addMethodCall('setLogger', [$loggerReference]);
$container
->setDefinition('sentry.client', new Definition(Client::class))
->setPublic(false)
->setFactory([$clientBuilderDefinition, 'getClient']);
// Setup Monitors.
$environment = '';
if (\is_array($options) && isset($options['environment'])) {
$environment = $options['environment'];
}
$release = null;
if (\is_array($options) && isset($options['release'])) {
$release = $options['release'];
}
$container
->register(MonitorFactoryInterface::class, MonitorFactory::class)
->setPublic(true)
->setArguments([$environment, $release])
;
}
/**
* @param array<string, mixed> $config
*/
private function registerErrorListenerConfiguration(ContainerBuilder $container, array $config): void
{
if (!$config['register_error_listener']) {
$container->removeDefinition(ErrorListener::class);
}
$container->getDefinition(ConsoleListener::class)->setArgument(1, $config['register_error_listener']);
}
/**
* @param array<string, mixed> $config
*/
private function registerMessengerListenerConfiguration(ContainerBuilder $container, array $config): void
{
if (!$this->isConfigEnabled($container, $config)) {
$container->removeDefinition(MessengerListener::class);
return;
}
$container->getDefinition(MessengerListener::class)->setArgument(1, $config['capture_soft_fails']);
}
/**
* @param array<string, mixed> $config
*/
private function registerTracingConfiguration(ContainerBuilder $container, array $config): void
{
$container->setParameter('sentry.tracing.enabled', $config['enabled']);
if (!$this->isConfigEnabled($container, $config)) {
$container->removeDefinition(TracingRequestListener::class);
$container->removeDefinition(TracingSubRequestListener::class);
$container->removeDefinition(TracingConsoleListener::class);
return;
}
$container->getDefinition(TracingConsoleListener::class)->replaceArgument(1, $config['console']['excluded_commands']);
}
/**
* @param array<string, mixed> $config
*/
private function registerDbalTracingConfiguration(ContainerBuilder $container, array $config): void
{
$isConfigEnabled = $this->isConfigEnabled($container, $config)
&& $this->isConfigEnabled($container, $config['dbal']);
if ($isConfigEnabled && !class_exists(DoctrineBundle::class)) {
throw new \LogicException('DBAL tracing support cannot be enabled because the doctrine/doctrine-bundle Composer package is not installed.');
}
$container->setParameter('sentry.tracing.dbal.enabled', $isConfigEnabled);
$container->setParameter('sentry.tracing.dbal.connections', $isConfigEnabled ? $config['dbal']['connections'] : []);
if (!$isConfigEnabled) {
$container->removeDefinition(ConnectionConfigurator::class);
$container->removeDefinition(TracingDriverMiddleware::class);
}
}
/**
* @param array<string, mixed> $config
*/
private function registerTwigTracingConfiguration(ContainerBuilder $container, array $config): void
{
$isConfigEnabled = $this->isConfigEnabled($container, $config)
&& $this->isConfigEnabled($container, $config['twig']);
if ($isConfigEnabled && !class_exists(TwigBundle::class)) {
throw new \LogicException('Twig tracing support cannot be enabled because the symfony/twig-bundle Composer package is not installed.');
}
if (!$isConfigEnabled) {
$container->removeDefinition(TwigTracingExtension::class);
}
}
/**
* @param array<string, mixed> $config
*/
private function registerCacheTracingConfiguration(ContainerBuilder $container, array $config): void
{
$isConfigEnabled = $this->isConfigEnabled($container, $config)
&& $this->isConfigEnabled($container, $config['cache']);
if ($isConfigEnabled && !class_exists(CacheItem::class)) {
throw new \LogicException('Cache tracing support cannot be enabled because the symfony/cache Composer package is not installed.');
}
$container->setParameter('sentry.tracing.cache.enabled', $isConfigEnabled);
}
/**
* @param array<string, mixed> $config
*/
private function registerHttpClientTracingConfiguration(ContainerBuilder $container, array $config): void
{
$isConfigEnabled = $this->isConfigEnabled($container, $config)
&& $this->isConfigEnabled($container, $config['http_client']);
if ($isConfigEnabled && !class_exists(HttpClient::class)) {
throw new \LogicException('Http client tracing support cannot be enabled because the symfony/http-client Composer package is not installed.');
}
$container->setParameter('sentry.tracing.http_client.enabled', $isConfigEnabled);
}
/**
* @param string[] $integrations
* @param array<string, mixed> $config
*
* @return array<Reference|Definition>
*/
private function configureIntegrationsOption(array $integrations, array $config): array
{
$integrations = array_map(static function (string $value): Reference {
return new Reference($value);
}, $integrations);
$integrations = $this->configureErrorListenerIntegration($integrations, $config['register_error_listener']);
$integrations = $this->configureRequestIntegration($integrations, $config['options']['default_integrations'] ?? true);
return $integrations;
}
/**
* @param array<Reference|Definition> $integrations
*
* @return array<Reference|Definition>
*/
private function configureErrorListenerIntegration(array $integrations, bool $registerErrorListener): array
{
if ($registerErrorListener && !$this->isIntegrationEnabled(IgnoreErrorsIntegration::class, $integrations)) {
// Prepend this integration to the beginning of the array so that
// we can save some performance by skipping the rest of the integrations
// if the error must be ignored
array_unshift($integrations, new Definition(IgnoreErrorsIntegration::class, [
[
'ignore_exceptions' => [
FatalError::class,
FatalErrorException::class,
],
],
]));
}
return $integrations;
}
/**
* @param array<Reference|Definition> $integrations
*
* @return array<Reference|Definition>
*/
private function configureRequestIntegration(array $integrations, bool $useDefaultIntegrations): array
{
if ($useDefaultIntegrations && !$this->isIntegrationEnabled(RequestIntegration::class, $integrations)) {
$integrations[] = new Definition(RequestIntegration::class, [new Reference(RequestFetcherInterface::class)]);
}
return $integrations;
}
/**
* @param class-string<IntegrationInterface> $integrationClass
* @param array<Reference|Definition> $integrations
*/
private function isIntegrationEnabled(string $integrationClass, array $integrations): bool
{
foreach ($integrations as $integration) {
if ($integration instanceof Reference && $integrationClass === (string) $integration) {
return true;
}
if ($integration instanceof Definition && $integrationClass === $integration->getClass()) {
return true;
}
}
return false;
}
}