diff --git a/README.md b/README.md index ce765d9..dc1f314 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Writing messages with extra data: ], 'category'); ``` -### Extra callback +### Extra/Tags callback `extraCallback` property can modify extra's data as callable function: @@ -66,11 +66,18 @@ Writing messages with extra data: // some manipulation with data $extra['some_data'] = \Yii::$app->someComponent->someMethod(); return $extra; - } + }, + 'tagsCallback' => function ($message, $tags) { + // some manipulation with tags + $tags['custom_tag'] = \Yii::$app->someComponent->getTag(); + return $tags; + } ], ], ``` + + ### Tags Writing messages with additional tags. If need to add additional tags for event, add `tags` key in message. Tags are various key/value pairs that get assigned to an event, and can later be used as a breakdown or quick access to finding related events. diff --git a/src/SentryTarget.php b/src/SentryTarget.php index 52c00e0..5de3701 100644 --- a/src/SentryTarget.php +++ b/src/SentryTarget.php @@ -47,6 +47,10 @@ class SentryTarget extends Target * @var callable Callback function that can modify extra's array */ public $extraCallback; + /** + * @var callable Callback function that can modify tags array + */ + public $tagsCallback; /** * @inheritDoc @@ -145,12 +149,14 @@ public function export() $data['extra']['context'] = parent::getContextMessage(); } - $data = $this->runExtraCallback($text, $data); - $scope->setUser($data['userData']); + + $data = $this->runExtraCallback($text, $data); foreach ($data['extra'] as $key => $value) { $scope->setExtra((string) $key, $value); } + + $data = $this->runTagsCallback($text, $data); foreach ($data['tags'] as $key => $value) { if ($value) { $scope->setTag($key, $value); @@ -189,6 +195,23 @@ public function runExtraCallback($text, $data) return $data; } + /** + * Calls the tags callback if it exists + * + * @param mixed $text + * @param array $data + * + * @return array + */ + public function runTagsCallback($text, $data) + { + if (is_callable($this->tagsCallback)) { + $data['tags'] = call_user_func($this->tagsCallback, $text, $data['tags'] ?? []); + } + + return $data; + } + /** * Returns the text display of the specified level for the Sentry. *