Skip to content

Commit

Permalink
Big update
Browse files Browse the repository at this point in the history
  • Loading branch information
asminog committed Feb 20, 2020
1 parent eab6cc6 commit 7d4a8a7
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 3,249 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ return [
'release' => '[email protected]',
// Options for sentry client
'options' => [],
// Collect additional context from $_GLOBALS, default: ['_SESSION']. To switch off set false.
// Collect additional context from $_GLOBALS, default: ['_SESSION', 'argv']. To switch off set false.
/* @see https://docs.sentry.io/enriching-error-data/context/?platform=php#extra-context
'collectContext' => ['_SERVER', '_COOKIE', '_SESSION'],
'collectContext' => ['_SERVER', '_COOKIE', '_SESSION', 'argv'],
// user attributes to collect, default: ['id', 'username', 'email']. To switch off set false.
/* @see https://docs.sentry.io/enriching-error-data/context/?platform=php#capturing-the-user */
'collectUserAttributes' => ['userId', 'userName', 'email'],
Expand Down
164 changes: 88 additions & 76 deletions src/SentryTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace asminog\yii2sentry;

use phpDocumentor\Reflection\Types\This;
use Sentry\Severity;
use Sentry\State\Scope;
use \Throwable;
use Throwable;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\VarDumper;
Expand Down Expand Up @@ -51,7 +50,7 @@ class SentryTarget extends Target
/**
* @var array Allow to collect context automatically.
*/
public $collectContext = ['_SESSION'];
public $collectContext = ['_SESSION', 'argv'];

/**
* @var callable Callback function that can modify extra's array
Expand All @@ -64,9 +63,17 @@ class SentryTarget extends Target
*/
public function init()
{
$this->logVars = $this->collectContext;

init(array_merge($this->options, ['dsn' => $this->dsn], ['release' => ($this->release == 'auto' ? $this->getRelease() : $this->release)]));
$this->logVars = [];

init(
array_merge(
$this->options,
[
'dsn' => $this->dsn,
'release' => ($this->release == 'auto' ? $this->getRelease() : $this->release)
]
)
);

parent::init();
}
Expand All @@ -82,42 +89,26 @@ private function getRelease()

/**
* @inheritdoc
* @throws \yii\base\InvalidConfigException
*/
public function export()
{
foreach ($this->messages as $message) {
list($message, $level, $category) = $message;

$this->setScopeLevel($level);
$this->clearScope();
$this->setScopeUser();
$this->setScopeTags(['category' => $category]);
$this->setExtraContext();
$this->setScopeLevel($level);
$this->setScopeTags(['category' => $category]);

if ($message instanceof Throwable) {
$this->setScopeExtras($this->runExtraCallback($message, []));
captureException($message);
continue;
}

if (is_array($message)) {
if (isset($message['tags'])) {
$this->setScopeTags($message['tags']);
unset($message['tags']);
}

if (isset($message['extra'])) {
$this->setScopeExtras($this->runExtraCallback($message, $message['extra']));
unset($message['extra']);
}

if (count($message) == 1 and isset($message['msg'])) {
$message = $message['msg'];
}
}

if (!is_string($message)) {
$message = VarDumper::dumpAsString($message);
}
$message = $this->convertMessage($message);

captureMessage($message);
}
Expand All @@ -132,25 +123,49 @@ private function setScopeLevel(int $level)
{
$level = $this->convertLevel($level);

configureScope(function(Scope $scope) use ($level) : void {
configureScope(function (Scope $scope) use ($level) : void {
$scope->setLevel($level);
});
}

/**
* Convert Logger level to Sentry level
* @param int $level
* @return Severity
*/
private function convertLevel(int $level)
{
switch ($level) {
case Logger::LEVEL_ERROR:
return Severity::error();
case Logger::LEVEL_WARNING:
return Severity::warning();
case Logger::LEVEL_INFO:
return Severity::info();
case Logger::LEVEL_TRACE:
case Logger::LEVEL_PROFILE_BEGIN:
case Logger::LEVEL_PROFILE_END:
return Severity::debug();
}

return Severity::fatal();
}

/**
* Set sentry user scope based on yii2 Yii::$app->user
* @throws \yii\base\InvalidConfigException
*/
private function setScopeUser()
{
if (!Yii::$app->request->isConsoleRequest and !empty($this->collectUserAttributes)) {
if (Yii::$app->get('user', false) !== null and !empty($this->collectUserAttributes)) {
$attributes = ['id' => (Yii::$app->user ? Yii::$app->user->getId() : null)];
if (($user = Yii::$app->user->identity) !== null) {
foreach ($this->collectUserAttributes as $collectUserAttribute) {
$attributes[$collectUserAttribute] = ArrayHelper::getValue($user, $collectUserAttribute);
}
}

configureScope(function(Scope $scope) use ($attributes): void {
configureScope(function (Scope $scope) use ($attributes): void {
$scope->setUser($attributes, true);
});
}
Expand All @@ -163,23 +178,33 @@ private function setScopeUser()
*/
private function setScopeTags(array $tags)
{
configureScope(function(Scope $scope) use ($tags): void {
foreach ($tags as $key => $value) {
$scope->setTag((string)$key, (string)$value);
}
configureScope(function (Scope $scope) use ($tags): void {
$scope->setTags($tags);
});
}

/**
* Add extra context if needed
*/
private function setExtraContext()
{
if (!empty($this->collectContext)) {
$this->logVars = $this->collectContext;
$extraContext = $this->getContextMessage();

$this->setScopeExtras(['CONTEXT' => $extraContext]);
$this->logVars = [];
}
}

/**
* Set sentry scope extas
* @param array $extras
*/
private function setScopeExtras(array $extras)
{
configureScope(function(Scope $scope) use ($extras): void {
foreach ($extras as $key => $value) {
$scope->setExtra((string)$key, $value);
}
configureScope(function (Scope $scope) use ($extras): void {
$scope->setExtras($extras);
});
}

Expand All @@ -190,68 +215,55 @@ private function setScopeExtras(array $extras)
* @param array $extra
* @return array
*/
public function runExtraCallback($message, $extra = [])
protected function runExtraCallback($message, $extra = [])
{
if ($this->extraCallback !== false and is_callable($this->extraCallback)) {
$extra = call_user_func($this->extraCallback, $message, $extra);
}

if (!is_array($extra)) {
$extra['extra'] = VarDumper::dumpAsString($extra);
$extra = ['extra' => VarDumper::dumpAsString($extra)];
}

return $extra;
}

/**
* @inheritdoc
* Clear sentry scope for new message
*/
protected function getContextMessage()
private function clearScope()
{
return '';
configureScope(function (Scope $scope): void {
$scope->clear();
});
}

/**
* Convert Logger level to Sentry level
* @param int $level
* @return Severity
* @param $message
* @return array|mixed|string
*/
private function convertLevel(int $level)
private function convertMessage($message)
{
switch ($level) {
case Logger::LEVEL_ERROR:
return Severity::error();
case Logger::LEVEL_WARNING:
return Severity::warning();
case Logger::LEVEL_INFO:
return Severity::info();
case Logger::LEVEL_TRACE:
case Logger::LEVEL_PROFILE_BEGIN:
case Logger::LEVEL_PROFILE_END:
return Severity::debug();
}
return Severity::fatal();
}
if (is_array($message)) {
if (isset($message['tags'])) {
$this->setScopeTags($message['tags']);
unset($message['tags']);
}

/**
* Add extra context if needed
*/
private function setExtraContext()
{
if (!empty($this->collectContext)) {
$context = ArrayHelper::filter($GLOBALS, $this->collectContext);
foreach ($this->maskVars as $var) {
if (ArrayHelper::getValue($context, $var) !== null) {
ArrayHelper::setValue($context, $var, '***');
}
if (isset($message['extra'])) {
$this->setScopeExtras($this->runExtraCallback($message, $message['extra']));
unset($message['extra']);
}
$result = [];
foreach ($context as $key => $value) {
$result[ltrim($key, '_')] = VarDumper::dumpAsString($value);

if (count($message) == 1 and isset($message['msg'])) {
$message = $message['msg'];
}
}

$this->setScopeExtras($result);
if (!is_string($message)) {
$message = VarDumper::dumpAsString($message);
}

return $message;
}
}
26 changes: 0 additions & 26 deletions tests/_support/FunctionalTester.php

This file was deleted.

Loading

0 comments on commit 7d4a8a7

Please sign in to comment.