Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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.
Expand Down
27 changes: 25 additions & 2 deletions src/SentryTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down