Skip to content

Commit

Permalink
Sentry integration (#411)
Browse files Browse the repository at this point in the history
* add sentry client library

* client and configuration

* default configuration for sentry

* client usage in certain places

* tests for sentry

* remove forgotten code

* fix index.php , accidental commit of debug files

* track errors from weblclient

* use void client if uninitialized client

* disable sentry during tests

* cover all major web exceptions

* edit config var desc, to be explicit

* add sentry to queue jobs

* movie everything to static sentry class

* fix tests

* set tag values

* more exception tracking

* queue exception reporting

* fix typo

---------

Co-authored-by: Daniel Garcia Briseno <[email protected]>
  • Loading branch information
mudhoney and dgarciabriseno authored Sep 6, 2024
1 parent 31ad7c8 commit 2b4837d
Show file tree
Hide file tree
Showing 20 changed files with 951 additions and 25 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"helioviewer/event-interface": "v0.2.3",
"matomo/device-detector": "dev-master",
"opis/json-schema": "^2.3",
"google/apiclient": "^2.16"
"google/apiclient": "^2.16",
"sentry/sentry": "^4.8"
},
"require-dev": {
"phpunit/phpunit": "^9.6"
Expand Down
219 changes: 217 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 39 additions & 6 deletions docroot/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,17 @@

use Helioviewer\Api\Request\RequestParams;
use Helioviewer\Api\Request\RequestException;
use Helioviewer\Api\Sentry\Sentry;

$config = new Config('../settings/Config.ini');

Sentry::init([
'environment' => HV_APP_ENV ?? 'dev',
'sample_rate' => HV_SENTRY_SAMPLE_RATE ?? 0.1,
'enabled' => HV_SENTRY_ENABLED ?? false,
'dsn' => HV_SENTRY_DSN,
]);

date_default_timezone_set('UTC');
register_shutdown_function('shutdownFunction');

Expand All @@ -41,8 +50,10 @@
}

try {

// Parse request and its variables
$params = RequestParams::collect();

} catch (RequestException $re) {

// Set the content type to JSON
Expand All @@ -56,10 +67,17 @@
'message' => $re->getMessage(),
'data' => [],
]);

// Track exception
Sentry::capture($re);

exit;
}


Sentry::setContext('Helioviewer', [
'params' => $params,
'is_json' => false,
]);

// Redirect to API Documentation if no API request is being made.
if ( !isset($params) || !loadModule($params) ) {
Expand Down Expand Up @@ -163,6 +181,11 @@ function loadModule($params) {
$moduleName = $valid_actions[$params['action']];
$className = 'Module_'.$moduleName;

// Track this request
Sentry::setTag('Module', $moduleName);
Sentry::setTag('Module.Function', $params['action']);
Sentry::setTag('Type', 'web');

include_once HV_ROOT_DIR.'/../src/Module/'.$moduleName.'.php';

$module = new $className($params);
Expand Down Expand Up @@ -201,7 +224,7 @@ function loadModule($params) {
}

} catch (LimitExceeded $exception) {
//limit exceeded
Sentry::capture($e);
}
}
} catch (\InvalidArgumentException $e) {
Expand All @@ -212,6 +235,7 @@ function loadModule($params) {
// Determine the content type of the request
$content_type = $_SERVER['CONTENT_TYPE'] ?? '';


// If the request is posting JSON
if('application/json' === $content_type) {

Expand All @@ -223,17 +247,26 @@ function loadModule($params) {
'message' => $e->getMessage(),
'data' => [],
]);

Sentry::setContext('Helioviewer', [
'is_json' => true,
]);

Sentry::capture($e);

exit;
}

printHTMLErrorMsg($e->getMessage());
} else {
printHTMLErrorMsg($e->getMessage());
}

Sentry::capture($e);
} catch (Exception $e) {

printHTMLErrorMsg($e->getMessage());

Sentry::capture($e);
}


return true;
}

Expand Down
1 change: 0 additions & 1 deletion scripts/resque.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

require_once __DIR__.'/../lib/Resque.php';
require_once __DIR__.'/../lib/Resque/Worker.php';
require_once __DIR__.'/../vendor/autoload.php';

$REDIS_BACKEND = getenv('REDIS_BACKEND');
if(!empty($REDIS_BACKEND)) {
Expand Down
Loading

0 comments on commit 2b4837d

Please sign in to comment.