Skip to content

Commit 6b9febb

Browse files
pascalchevrelclaude
andcommitted
Fix Sentry setup: init early, add release, drop captureLastError()
captureLastError() sent the request's last PHP error to Sentry even when it was silenced with @, e.g. the harmless unlink() race when concurrent requests delete an expired cache file (WHAT-TRAIN-IS-IT-NOW-8R). Sentry's own handlers already report non-silenced errors, exceptions and fatals. Initialise Sentry right after the autoloader, as the Sentry docs recommend, so that errors while fetching Product Details at startup are reported, and set the release (deployed commit) and environment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 31ebe1b commit 6b9febb

2 files changed

Lines changed: 45 additions & 39 deletions

File tree

‎app/inc/config.php‎

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,6 @@
3434
// Autoloading of classes (both /vendor/ and /app/classes)
3535
require_once INSTALL_ROOT . 'vendor/autoload.php';
3636

37-
// PHP sends no User-Agent at all on stream based requests. Set ours as the
38-
// default so that anything we didn't route through Utils::httpClient() or
39-
// Utils::streamContext() is still identifiable by the sites we query.
40-
ini_set('user_agent', ReleaseInsights\Utils::USER_AGENT);
41-
42-
// Get Firefox Versions from Product Details library, default cache duration
43-
$firefox_versions = new Data()->getFirefoxVersions();
44-
45-
// Exact version numbers (strings) from product-details
46-
define('ESR', $firefox_versions['FIREFOX_ESR']);
47-
define('ESR_NEXT', $firefox_versions['FIREFOX_ESR_NEXT']);
48-
define('ESR115', $firefox_versions['FIREFOX_ESR115']);
49-
define('FIREFOX_NIGHTLY', $firefox_versions['FIREFOX_NIGHTLY']);
50-
define('DEV_EDITION', $firefox_versions['FIREFOX_DEVEDITION']);
51-
define('FIREFOX_BETA', $firefox_versions['LATEST_FIREFOX_RELEASED_DEVEL_VERSION']);
52-
define('FIREFOX_RELEASE', $firefox_versions['LATEST_FIREFOX_VERSION']);
53-
54-
// Major version numbers (integers), used across the app
55-
define('NIGHTLY', (int) FIREFOX_NIGHTLY);
56-
define('BETA', (int) FIREFOX_BETA);
57-
define('RELEASE', (int) FIREFOX_RELEASE);
58-
define('NEXT_ESR', (int) (ESR_NEXT ?: null));
59-
define('CURRENT_ESR', (int) ESR);
60-
define('WIN7_ESR', ESR115 ? (int) ESR115 : null);
61-
6237
// Are we on one of our staging sites
6338
$http_host = isset($_SERVER['HTTP_HOST']) ? (string) $_SERVER['HTTP_HOST'] : null;
6439

@@ -84,17 +59,52 @@
8459
// Are we in a dev mode where debug is activated and twig templates not cached?
8560
define('IS_DEV_MODE', getenv('DEV_MODE') === 'true');
8661

87-
// Define a Nonce for inline scripts
88-
define('NONCE', bin2hex(random_bytes(20)));
89-
90-
// Set up Sentry endpoint, don't send errors while in dev mode
91-
if (STAGING) {
92-
init(['dsn' => 'https://e17dcdc892db4ee08a6937603e407f76@o1069899.ingest.sentry.io/4505243444772864']);
62+
/*
63+
Set up Sentry as early as possible so that errors during startup (such as
64+
fetching Product Details below) are reported. Not used on localhost.
65+
deployed-version.txt is generated at build time on all our deployments.
66+
*/
67+
if (STAGING || PRODUCTION) {
68+
$deployed_version = is_file(WEB_ROOT . 'deployed-version.txt')
69+
? trim((string) file_get_contents(WEB_ROOT . 'deployed-version.txt'))
70+
: '';
71+
72+
init([
73+
'dsn' => PRODUCTION
74+
? 'https://20bef71984594e16add1d2c69146ad88@o1069899.ingest.sentry.io/4505243430092800'
75+
: 'https://e17dcdc892db4ee08a6937603e407f76@o1069899.ingest.sentry.io/4505243444772864',
76+
'environment' => PRODUCTION ? 'production' : 'staging',
77+
'release' => preg_match('/^[0-9a-f]{40}$/', $deployed_version) ? $deployed_version : null,
78+
]);
9379
}
9480

95-
if (PRODUCTION) {
96-
init(['dsn' => 'https://20bef71984594e16add1d2c69146ad88@o1069899.ingest.sentry.io/4505243430092800']);
97-
}
81+
// PHP sends no User-Agent at all on stream based requests. Set ours as the
82+
// default so that anything we didn't route through Utils::httpClient() or
83+
// Utils::streamContext() is still identifiable by the sites we query.
84+
ini_set('user_agent', ReleaseInsights\Utils::USER_AGENT);
85+
86+
// Get Firefox Versions from Product Details library, default cache duration
87+
$firefox_versions = new Data()->getFirefoxVersions();
88+
89+
// Exact version numbers (strings) from product-details
90+
define('ESR', $firefox_versions['FIREFOX_ESR']);
91+
define('ESR_NEXT', $firefox_versions['FIREFOX_ESR_NEXT']);
92+
define('ESR115', $firefox_versions['FIREFOX_ESR115']);
93+
define('FIREFOX_NIGHTLY', $firefox_versions['FIREFOX_NIGHTLY']);
94+
define('DEV_EDITION', $firefox_versions['FIREFOX_DEVEDITION']);
95+
define('FIREFOX_BETA', $firefox_versions['LATEST_FIREFOX_RELEASED_DEVEL_VERSION']);
96+
define('FIREFOX_RELEASE', $firefox_versions['LATEST_FIREFOX_VERSION']);
97+
98+
// Major version numbers (integers), used across the app
99+
define('NIGHTLY', (int) FIREFOX_NIGHTLY);
100+
define('BETA', (int) FIREFOX_BETA);
101+
define('RELEASE', (int) FIREFOX_RELEASE);
102+
define('NEXT_ESR', (int) (ESR_NEXT ?: null));
103+
define('CURRENT_ESR', (int) ESR);
104+
define('WIN7_ESR', ESR115 ? (int) ESR115 : null);
105+
106+
// Define a Nonce for inline scripts
107+
define('NONCE', bin2hex(random_bytes(20)));
98108

99109
// Clean up temp variables from global space
100-
unset($firefox_versions, $http_host);
110+
unset($firefox_versions, $http_host, $deployed_version);

‎app/inc/init.php‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
declare(strict_types=1);
44

5-
use function Sentry\captureLastError;
65
use Tracy\Debugger;
76

87
// This is our production CSP
@@ -32,8 +31,5 @@
3231
// Dispatch urls. The $url object is defined in router.php
3332
$url->loadController();
3433

35-
// Send the last error to Sentry
36-
captureLastError();
37-
3834
// Make sure web request stops here
3935
exit;

0 commit comments

Comments
 (0)