Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
feature(Sentry): add more data (user locale, request + transaction id)
Browse files Browse the repository at this point in the history
also fix some Log/Formatter phpstan issues
  • Loading branch information
pschuele committed Sep 6, 2021
1 parent 2d4ee90 commit 57ef516
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 36 deletions.
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9879,26 +9879,6 @@ parameters:
count: 1
path: tine20/Tinebase/Log.php

-
message: "#^Method Tinebase_Log_Formatter\\:\\:reset\\(\\) should return string but return statement is missing\\.$#"
count: 1
path: tine20/Tinebase/Log/Formatter.php

-
message: "#^Method Tinebase_Log_Formatter\\:\\:resetUsername\\(\\) should return string but return statement is missing\\.$#"
count: 1
path: tine20/Tinebase/Log/Formatter.php

-
message: "#^PHPDoc tag @param has invalid value \\(\\$logPrio\\)\\: Unexpected token \"\\$logPrio\", expected type at offset 18$#"
count: 1
path: tine20/Tinebase/Log/Formatter.php

-
message: "#^PHPDoc tag @param has invalid value \\(\\$output\\)\\: Unexpected token \"\\$output\", expected type at offset 18$#"
count: 1
path: tine20/Tinebase/Log/Formatter.php

-
message: "#^Access to an undefined property Zend_Mail_Message\\:\\:\\$contentType\\.$#"
count: 1
Expand Down
1 change: 1 addition & 0 deletions tine20/Tinebase/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ public function initUser(Tinebase_Model_FullUser $_user, $fixCookieHeader = true
'id' => $_user->getId(),
'email' => $_user->accountEmailAddress,
'login' => $_user->accountLoginName,
'locale' => Tinebase_Core::getLocale()->toString(),
]);
});
}
Expand Down
10 changes: 6 additions & 4 deletions tine20/Tinebase/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,18 @@ public static function dispatchRequest()
$request = self::getRequest();

// we need to initialize sentry at the very beginning to catch ALL errors
$ravenClient = self::setupSentry();
self::setupSentry();

// check transaction header
if ($request->getHeaders()->has('X-TINE20-TRANSACTIONID')) {
$transactionId = $request->getHeaders()->get('X-TINE20-TRANSACTIONID')->getFieldValue();
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
. " Client transaction $transactionId");
Tinebase_Log_Formatter::setTransactionId(substr($transactionId, 0, 5));
if ($ravenClient) {
$ravenClient->tags['transaction_id'] = $transactionId;
if (Tinebase_Core::isRegistered('SENTRY')) {
Sentry\configureScope(function (\Sentry\State\Scope $scope) use ($transactionId): void {
$scope->setContext('transaction_id', $transactionId);
});
}
}

Expand Down Expand Up @@ -2435,7 +2437,7 @@ public static function setupSentry()
'tags' => array(
'php_version' => phpversion(),
'tine_url' => Tinebase_Config::getInstance()->get(Tinebase_Config::TINE20_URL) ?: 'unknown',
// TODO add more tags?
'request_id' => Tinebase_Log_Formatter::getRequestId(),
),
]);
self::set('SENTRY', true);
Expand Down
29 changes: 17 additions & 12 deletions tine20/Tinebase/Log/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function __construct($format = null)
parent::__construct($format);

if (!self::$_requestId || self::$_requestId === '-') {
self::$_requestId = Tinebase_Record_Abstract::generateUID($this->_requestIdLength);
$this->setRequestId();
}

if (self::$_starttime === NULL) {
Expand All @@ -142,6 +142,17 @@ public function __construct($format = null)
}
}
}

/**
* @param string|null $requestId
*/
public function setRequestId(string $requestId = null)
{
if (! $requestId) {
$requestId = Tinebase_Record_Abstract::generateUID($this->_requestIdLength);
}
self::$_requestId = $requestId;
}

/**
* add strings to replace in log output (passwords for example)
Expand Down Expand Up @@ -219,11 +230,11 @@ protected function _getLogDiffTime($format = true)
}

/**
* @param $output
* @param string $output
* @param array $event
* @return string
*/
protected function _getFormattedOutput($output, array $event)
protected function _getFormattedOutput(string $output, array $event): string
{
if (self::$_colorize) {
$color = $this->_getColorByPrio($event['priority']);
Expand All @@ -234,10 +245,10 @@ protected function _getFormattedOutput($output, array $event)
}

/**
* @param $logPrio
* @param integer $logPrio
* @return string
*/
protected function _getColorByPrio($logPrio)
protected function _getColorByPrio($logPrio): string
{
switch ($logPrio) {
case 0:
Expand Down Expand Up @@ -277,9 +288,7 @@ public static function getUsername()
if (self::$_username === NULL) {
$user = Tinebase_Core::getUser();
self::$_username = ($user && is_object($user))
? (isset($user->accountLoginName)
? $user->accountLoginName
: (isset($user->accountDisplayName) ? $user->accountDisplayName : NULL))
? ($user->accountLoginName ?? ($user->accountDisplayName ?? NULL))
: NULL;
}

Expand All @@ -295,8 +304,6 @@ public static function getUsername()

/**
* reset current username
*
* @return string
*/
public static function resetUsername()
{
Expand All @@ -305,8 +312,6 @@ public static function resetUsername()

/**
* reset username and options
*
* @return string
*/
public static function reset()
{
Expand Down

0 comments on commit 57ef516

Please sign in to comment.