Created
September 9, 2021 17:41
-
-
Save Gunni/a8fb23faf215bb147e02493485f5e428 to your computer and use it in GitHub Desktop.
Observium integration for Sentry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This file is silently disabled if not configured | |
if ( ! isset($config['sentry_dsn'])) | |
{ | |
return; | |
} | |
if (defined('SENTRY_LOADED')) | |
{ | |
return; | |
} | |
define('SENTRY_LOADED', 1); | |
if (is_null($_SESSION)) | |
{ | |
// Session is randomly not loaded, just calling this fixes that... | |
// I have yet to find out why that happens... | |
session_start(); | |
} | |
require_once(sprintf('%s/vendor/autoload.php', $config['install_dir'])); | |
\Sentry\init([ | |
'dsn' => $config['sentry_dsn'], | |
'release' => sprintf('observium@%s', OBSERVIUM_VERSION), | |
// Use environment to indicate what observium instance this is | |
'environment' => explode('/', $config['base_url'])[2], | |
]); | |
\Sentry\configureScope(function (\Sentry\State\Scope $scope): void { | |
global $config; | |
$user_email = NULL; | |
if (isset($_SESSION['username']) && $config['auth_mechanism'] != 'ldap') | |
{ | |
$user_email = sprintf('%s@%s', $_SESSION['username'], $config['auth_mechanism']); | |
} | |
else if (isset($_SESSION['username'])) | |
{ | |
$auth_suffix = $config['auth_ldap_suffix']; | |
$domain = implode('.', array_map(function($v) { return explode('=', $v)[1]; }, explode(',', $auth_suffix))); | |
$user_email = sprintf('%s@%s', $_SESSION['username'], $domain); | |
} | |
$scope->setUser([ | |
'id' => $_SESSION['user_id'] ?? NULL, | |
'username' => $_SESSION['username'] ?? NULL, | |
'email' => $user_email, | |
'ip' => $_SESSION['PREV_REMOTE_ADDR'] ?? NULL, | |
]); | |
$scope->setTag('user_level', $_SESSION['userlevel'] ?? 'UNKNOWN'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment