-
Couldn't load subscription status.
- Fork 7
feat(transaction): Track background jobs #707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-only | ||
| */ | ||
|
|
||
| namespace OCA\Sentry\Listener; | ||
|
|
||
| use OCP\BackgroundJob\Events\BeforeJobExecutedEvent; | ||
| use OCP\BackgroundJob\Events\JobExecutedEvent; | ||
| use OCP\EventDispatcher\Event; | ||
| use OCP\EventDispatcher\IEventListener; | ||
| use Sentry\SentrySdk; | ||
| use Sentry\Tracing\SpanStatus; | ||
| use Sentry\Tracing\TransactionContext; | ||
| use function array_key_exists; | ||
| use function get_class; | ||
| use function Sentry\startTransaction; | ||
|
|
||
| class BackgroundJobListener implements IEventListener { | ||
|
Check failure on line 23 in lib/Listener/BackgroundJobListener.php
|
||
|
|
||
| private array $transactions; | ||
|
|
||
| public function handle(Event $event): void { | ||
| if ($event instanceof BeforeJobExecutedEvent) { | ||
|
Check failure on line 28 in lib/Listener/BackgroundJobListener.php
|
||
| $job = $event->getJob(); | ||
| $transactionContext = new TransactionContext(); | ||
| $transactionContext->setName('Background job ' . get_class($job)); | ||
| $transactionContext->setOp('cron'); | ||
| $transactionContext->setData([ | ||
| 'id' => $job->getId(), | ||
| ]); | ||
| $transaction = $this->transactions[$job->getId()] = startTransaction($transactionContext); | ||
| SentrySdk::getCurrentHub()->setSpan($transaction); | ||
| } | ||
| if ($event instanceof JobExecutedEvent) { | ||
| $job = $event->getJob(); | ||
| if (array_key_exists($job->getId(), $this->transactions)) { | ||
| $transaction = $this->transactions[$job->getId()]; | ||
| $transaction->setStatus(SpanStatus::ok()); | ||
| $transaction->finish(); | ||
| unset($this->transactions[$job->getId()]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not be needed, the class name can be used without existance, just psalm complains
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it for Psalm but it complains in any case