Skip to content

Commit

Permalink
Improve TracingCommandListener (#380)
Browse files Browse the repository at this point in the history
* Improve `TracingCommandListener`

* Update composer.json

* Update sentry.php

---------

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia committed Oct 27, 2023
1 parent 215346e commit 1a44b3c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/sentry/publish/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
'arguments' => 'command.arguments',
'options' => 'command.options',
'exit_code' => 'command.exit_code',
'exception.stack_trace' => 'exception.stack_trace',
],
'coroutine' => [
'id' => 'coroutine.id',
Expand Down
15 changes: 15 additions & 0 deletions src/sentry/src/Tracing/Listener/TracingCommandListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Hyperf\Event\Contract\ListenerInterface;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\Tracing\SpanStatus;
use Sentry\Tracing\TransactionContext;

class TracingCommandListener implements ListenerInterface
Expand Down Expand Up @@ -86,6 +87,20 @@ protected function finishTransaction(HubInterface $sentry, AfterExecute $event):
$tags[$this->tagManager->get('command.exit_code')] = $command->getName();
}
$transaction->setTags($tags);
if (method_exists($event, 'getThrowable') && $exception = $event->getThrowable()) {
$transaction->setStatus(SpanStatus::internalError());
$transaction->setTags([
'error' => true,
'exception.class' => $exception::class,
'exception.message' => $exception->getMessage(),
'exception.code' => $exception->getCode(),
]);
if ($this->tagManager->has('command.exception.stack_trace')) {
$transaction->setData([
$this->tagManager->get('command.exception.stack_trace') => (string) $exception,
]);
}
}
$transaction->finish(microtime(true));
}
}

0 comments on commit 1a44b3c

Please sign in to comment.