forked from cloud-solutions/zend-sentry
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Module.php
293 lines (253 loc) · 10.7 KB
/
Module.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
<?php
declare(strict_types=1);
/**
* Andre Cardoso LaminasSentry
*
* This source file is part of the Andre Cardoso LaminasSentry package
*
* @package LaminasSentry\Module
* @license MIT License {@link /docs/LICENSE}
* @copyright Copyright (c) 2023, Andre Cardoso
*/
namespace LaminasSentry;
use Laminas\EventManager\EventManager;
use Laminas\Log\Logger;
use Laminas\Mvc\MvcEvent;
use Laminas\Mvc\View\Http\ExceptionStrategy;
use Laminas\ServiceManager\ServiceManager;
use Laminas\View\Helper\HeadScript;
use LaminasSentry\Mvc\View\Console\ExceptionStrategy as SentryConsoleStrategy;
use LaminasSentry\Mvc\View\Http\ExceptionStrategy as SentryHttpStrategy;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Raven_Client as Raven;
use Raven_Exception;
/**
* Class Module
*
* @package LaminasSentry
*/
class Module
{
const RAVENJS_VERSION = '3.27.0';
/**
* Translates Laminas Framework log levels to Raven log levels.
*/
private $logLevels = [
0 => Raven::FATAL,
1 => Raven::FATAL,
2 => Raven::FATAL,
3 => Raven::ERROR,
4 => Raven::WARNING,
5 => Raven::INFO,
6 => Raven::INFO,
7 => Raven::DEBUG,
];
/**
* @var Raven $ravenClient
*/
protected $ravenClient;
/**
* @var LaminasSentry $laminasSentry
*/
protected $laminasSentry;
/**
* @var $config
*/
protected $config;
/**
* @var EventManager $eventManager
*/
protected $eventManager;
/**
* @param MvcEvent $event
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Raven_Exception
*/
public function onBootstrap(MvcEvent $event)
{
// Setup RavenClient (provided by Sentry) and Sentry (provided by this module)
$this->config = $event->getApplication()->getServiceManager()->get('Config');
if (!$this->config['laminas-sentry']['use-module']) {
return;
}
if (isset($this->config['laminas-sentry']['raven-config']) && \is_array($this->config['laminas-sentry']['raven-config'])) {
$ravenConfig = $this->config['laminas-sentry']['raven-config'];
} else {
$ravenConfig = [];
}
$sentryApiKey = $this->config['laminas-sentry']['sentry-api-key'];
// Do preliminary checks only, Raven will parse the string further
if (!$sentryApiKey || '' === $sentryApiKey || \is_null($sentryApiKey)) {
throw new Raven_Exception('Missing Sentry API key.');
}
$ravenClient = new Raven($sentryApiKey, $ravenConfig);
// Register the RavenClient as a application wide service
/** @var ServiceManager $serviceManager */
$serviceManager = $event->getApplication()->getServiceManager();
$serviceManager->setService('raven', $ravenClient);
$this->ravenClient = $ravenClient;
$this->laminasSentry = new LaminasSentry($ravenClient);
// Get the eventManager and set it as a member for convenience
$this->eventManager = $event->getApplication()->getEventManager();
// If LaminasSentry is configured to use the custom logger, attach the listener
if ($this->config['laminas-sentry']['attach-log-listener']) {
$this->setupBasicLogging($event);
}
// If LaminasSentry is configured to log exceptions, a few things need to be set up
if ($this->config['laminas-sentry']['handle-exceptions']) {
$this->setupExceptionLogging($event);
}
// If LaminasSentry is configured to log errors, register it as error handler
if ($this->config['laminas-sentry']['handle-errors']) {
$errorReportingLevel = $this->config['laminas-sentry']['error-reporting'] ?? -1;
$this->laminasSentry->registerErrorHandler($this->config['laminas-sentry']['call-existing-error-handler'], $errorReportingLevel);
}
// If LaminasSentry is configured to log shutdown errors, register it
if ($this->config['laminas-sentry']['handle-shutdown-errors']) {
$this->laminasSentry->registerShutdownFunction();
}
// If LaminasSentry is configured to log Javascript errors, add needed scripts to the view
if ($this->config['laminas-sentry']['handle-javascript-errors']) {
$this->setupJavascriptLogging($event);
}
}
/**
* @return array
*/
public function getAutoloaderConfig(): array
{
return [
'Laminas\Loader\StandardAutoloader' => [
'namespaces' => [
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
]
]
];
}
/**
* @return mixed
*/
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
/**
* Gives us the possibility to write logs to Sentry from anywhere in the application
* Doesn't use the ZF compatible Log Writer because we want to return the Sentry event ID
* ZF Logging doesn't provide the possibility to return values from writers
*
* @param MvcEvent $event
*/
protected function setupBasicLogging(MvcEvent $event)
{
// Get the shared event manager and attach a logging listener for the log event on application level
$sharedManager = $this->eventManager->getSharedManager();
$raven = $this->ravenClient;
$logLevels = $this->logLevels;
$sharedManager->attach(
'*', 'log', function($event) use ($raven, $logLevels) {
/** @var $event MvcEvent */
if (\is_object($event->getTarget())) {
$target = \get_class($event->getTarget());
} else {
$target = (string)$event->getTarget();
}
$message = $event->getParam('message', 'No message provided');
$priority = (int)$event->getParam('priority', Logger::INFO);
$message = sprintf('%s: %s', $target, $message);
$tags = $event->getParam('tags', []);
$extra = $event->getParam('extra', []);
$eventID = $raven->captureMessage(
$message, [], ['tags' => $tags, 'level' => $logLevels[$priority], 'extra' => $extra]
);
return $eventID;
}, 2
);
}
/**
* 1. Registers Sentry as exception handler
* 2. Replaces the default ExceptionStrategy so Exception that are caught by Laminas Framework can still be logged
*
* @param MvcEvent $event
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function setupExceptionLogging(MvcEvent $event)
{
// Register Sentry as exception handler for exception that bubble up to the top
$this->laminasSentry->registerExceptionHandler($this->config['laminas-sentry']['call-existing-exception-handler']);
// Replace the default ExceptionStrategy with LaminasSentry's strategy
if ($event->getApplication()->getServiceManager()->has('HttpExceptionStrategy')) {
/** @var $exceptionStrategy ExceptionStrategy */
$exceptionStrategy = $event->getApplication()->getServiceManager()->get('HttpExceptionStrategy');
$exceptionStrategy->detach($this->eventManager);
}
// Check if script is running in console
$exceptionStrategy = (PHP_SAPI == 'cli') ? new SentryConsoleStrategy() : new SentryHttpStrategy();
$exceptionStrategy->attach($this->eventManager);
$exceptionStrategy->setDisplayExceptions($this->config['laminas-sentry']['display-exceptions']);
$exceptionStrategy->setDefaultExceptionMessage($this->config['laminas-sentry'][(PHP_SAPI == 'cli') ? 'default-exception-console-message' : 'default-exception-message']);
if ($exceptionStrategy instanceof SentryHttpStrategy && isset($this->config['view_manager']['exception_template'])) {
$exceptionStrategy->setExceptionTemplate($this->config['view_manager']['exception_template']);
}
$ravenClient = $this->ravenClient;
// Attach an exception listener for the LaminasSentry exception strategy, can be triggered from anywhere else too
$this->eventManager->getSharedManager()->attach(
'*', 'logException', function ($event) use ($ravenClient) {
/** @var $event MvcEvent */
$exception = $event->getParam('exception');
$tags = $event->getParam('tags', []);
return $ravenClient->captureException($exception, ['tags' => $tags]);
}
);
}
/**
* Adds the necessary javascript, tries to prepend
*
* @param MvcEvent $event
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function setupJavascriptLogging(MvcEvent $event)
{
/** @var HeadScript $headScript */
$headScript = $event->getApplication()->getServiceManager()->get('ViewHelperManager')->get('headscript');
$useRavenjsCDN = $this->config['laminas-sentry']['use-ravenjs-cdn'] ?? false;
if ($useRavenjsCDN) {
$ravenjsVersion = $this->config['laminas-sentry']['ravenjs-version'] ?? self::RAVENJS_VERSION;
$cdnUri = sprintf('//cdn.ravenjs.com/%s/raven.min.js', $ravenjsVersion);
$headScript->offsetSetFile(0, $cdnUri);
} else {
$ravenjsSource = $this->config['laminas-sentry']['ravenjs-source'] ?? false;
if ($ravenjsSource) {
$headScript->offsetSetFile(0, $ravenjsSource);
}
}
$publicApiKey = $this->convertKeyToPublic($this->config['laminas-sentry']['sentry-api-key']);
$ravenjsConfig = json_encode($this->config['laminas-sentry']['ravenjs-config']);
$attributes = \is_null($this->laminasSentry->getCSPNonce()) ? [] : ['nonce' => $this->laminasSentry->getCSPNonce()];
$headScript->offsetSetScript(1, sprintf("if (typeof Raven !== 'undefined') Raven.config('%s', %s).install()", $publicApiKey, $ravenjsConfig), 'text/javascript', $attributes);
}
/**
* @param string $key
*
* @return string $publicKey
*/
private function convertKeyToPublic($key): string
{
// If new DSN is configured, no converting is needed
if (substr_count($key, ':') == 1) {
return $key;
}
// If legacy DSN with private part is configured...
// ...find private part
$start = strpos($key, ':', 6);
$end = strpos($key, '@');
$privatePart = substr($key, $start, $end - $start);
// ... replace it with an empty string
return str_replace($privatePart, '', $key);
}
}