|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Sentry; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Psr\Log\LoggerInterface; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use Sentry\ClientBuilder; |
|
11
|
|
|
use Sentry\Integration as SdkIntegration; |
|
12
|
|
|
use Sentry\Integration\IntegrationInterface; |
|
13
|
|
|
use Sentry\Options; |
|
14
|
|
|
use Sentry\SentrySdk; |
|
15
|
|
|
use Sentry\State\HubInterface; |
|
16
|
|
|
use Sentry\Transport\TransportFactoryInterface; |
|
17
|
|
|
use Yiisoft\Yii\Sentry\Http\YiiRequestFetcher; |
|
18
|
|
|
use Yiisoft\Yii\Sentry\Integration\ExceptionContextIntegration; |
|
19
|
|
|
use Yiisoft\Yii\Sentry\Integration\Integration; |
|
20
|
|
|
|
|
21
|
|
|
use function is_string; |
|
22
|
|
|
|
|
23
|
|
|
final class HubBootstrapper |
|
24
|
|
|
{ |
|
25
|
|
|
public const DEFAULT_INTEGRATIONS = [ |
|
26
|
|
|
ExceptionContextIntegration::class, |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
13 |
|
public function __construct( |
|
30
|
|
|
private Options $options, |
|
31
|
|
|
private YiiSentryConfig $configuration, |
|
32
|
|
|
private TransportFactoryInterface $transportFactory, |
|
33
|
|
|
private LoggerInterface $logger, |
|
34
|
|
|
private HubInterface $hub, |
|
35
|
|
|
private ContainerInterface $container, |
|
36
|
|
|
) { |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
13 |
|
public function bootstrap(): void |
|
40
|
|
|
{ |
|
41
|
13 |
|
$this->options->setIntegrations(fn (array $integrations) => $this->prepareIntegrations($integrations)); |
|
42
|
|
|
|
|
43
|
13 |
|
$clientBuilder = new ClientBuilder($this->options); |
|
44
|
|
|
$clientBuilder |
|
45
|
13 |
|
->setTransportFactory($this->transportFactory) |
|
46
|
13 |
|
->setLogger($this->logger); |
|
47
|
|
|
|
|
48
|
13 |
|
$client = $clientBuilder->getClient(); |
|
49
|
|
|
|
|
50
|
13 |
|
$this->hub->bindClient($client); |
|
51
|
13 |
|
SentrySdk::setCurrentHub($this->hub); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param IntegrationInterface[] $integrations |
|
56
|
|
|
* |
|
57
|
|
|
* @return IntegrationInterface[] |
|
58
|
|
|
*/ |
|
59
|
13 |
|
public function prepareIntegrations(array $integrations): array |
|
60
|
|
|
{ |
|
61
|
13 |
|
$userIntegrations = $this->resolveIntegrationsFromUserConfig(); |
|
62
|
13 |
|
if (!$this->options->hasDefaultIntegrations()) { |
|
63
|
|
|
return array_merge($integrations, $userIntegrations); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
13 |
|
$integrations = array_filter( |
|
67
|
|
|
$integrations, |
|
68
|
|
|
static fn (SdkIntegration\IntegrationInterface $integration): bool => !( |
|
69
|
13 |
|
$integration instanceof SdkIntegration\ErrorListenerIntegration || |
|
70
|
|
|
$integration instanceof SdkIntegration\ExceptionListenerIntegration || |
|
71
|
|
|
$integration instanceof SdkIntegration\FatalErrorListenerIntegration || |
|
72
|
|
|
// We also remove the default request integration so it can be readded after with a Yii3 |
|
73
|
|
|
// specific request fetcher. This way we can resolve the request from Yii3 instead of |
|
74
|
|
|
// constructing it from the global state. |
|
75
|
|
|
$integration instanceof SdkIntegration\RequestIntegration |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
13 |
|
$integrations[] = new SdkIntegration\RequestIntegration( |
|
79
|
13 |
|
new YiiRequestFetcher($this->container) |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
13 |
|
return array_merge($integrations, $userIntegrations); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Resolve the integrations from the user configuration with the container. |
|
87
|
|
|
* |
|
88
|
|
|
* @return SdkIntegration\IntegrationInterface[] |
|
89
|
|
|
*/ |
|
90
|
13 |
|
private function resolveIntegrationsFromUserConfig(): array |
|
91
|
|
|
{ |
|
92
|
|
|
// Default Sentry SDK integrations |
|
93
|
13 |
|
$integrations = [ |
|
94
|
13 |
|
new Integration(), |
|
95
|
|
|
]; |
|
96
|
|
|
|
|
97
|
13 |
|
$integrationsToResolve = $this->configuration->getIntegrations(); |
|
98
|
|
|
|
|
99
|
13 |
|
$enableDefaultTracingIntegrations = array_key_exists('default_integrations', $this->configuration->getTracing()) |
|
100
|
11 |
|
&& (bool)$this->configuration->getTracing()['default_integrations']; |
|
101
|
|
|
|
|
102
|
|
|
if ( |
|
103
|
13 |
|
$enableDefaultTracingIntegrations |
|
104
|
13 |
|
&& $this->configuration->couldHavePerformanceTracingEnabled() |
|
105
|
|
|
) { |
|
106
|
1 |
|
$integrationsToResolve = array_merge( |
|
107
|
|
|
$integrationsToResolve, |
|
108
|
|
|
self::DEFAULT_INTEGRATIONS |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
/** @psalm-suppress MixedAssignment */ |
|
112
|
13 |
|
foreach ($integrationsToResolve as $userIntegration) { |
|
113
|
|
|
if ( |
|
114
|
|
|
$userIntegration instanceof |
|
115
|
|
|
SdkIntegration\IntegrationInterface |
|
116
|
|
|
) { |
|
117
|
|
|
$integrations[] = $userIntegration; |
|
118
|
1 |
|
} elseif (is_string($userIntegration)) { |
|
119
|
|
|
/** @psalm-suppress MixedAssignment */ |
|
120
|
1 |
|
$resolvedIntegration = $this->container->get($userIntegration); |
|
121
|
|
|
|
|
122
|
|
|
if ( |
|
123
|
|
|
!$resolvedIntegration instanceof |
|
124
|
|
|
SdkIntegration\IntegrationInterface |
|
125
|
|
|
) { |
|
126
|
|
|
throw new RuntimeException( |
|
127
|
|
|
sprintf( |
|
128
|
|
|
'Sentry integration must be an instance of `%s` got `%s`.', |
|
129
|
|
|
SdkIntegration\IntegrationInterface::class, |
|
130
|
|
|
get_debug_type($resolvedIntegration) |
|
131
|
|
|
) |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
1 |
|
$integrations[] = $resolvedIntegration; |
|
136
|
|
|
} else { |
|
137
|
|
|
throw new RuntimeException( |
|
138
|
|
|
sprintf( |
|
139
|
|
|
'Sentry integration must either be a valid container reference or an instance of `%s`.', |
|
140
|
|
|
SdkIntegration\IntegrationInterface::class |
|
141
|
|
|
) |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
13 |
|
return $integrations; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|