Skip to content
Draft
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
10 changes: 10 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCA\Sentry\Config;
use OCA\Sentry\Http\PerformanceMonitoringMiddleware;
use OCA\Sentry\InitialState\DsnProvider;
use OCA\Sentry\Listener\BackgroundJobListener;
use OCA\Sentry\Listener\CustomCspListener;
use OCA\Sentry\Reporter\ISentryReporter;
use OCA\Sentry\Reporter\RecursionAwareReporter;
Expand All @@ -35,9 +36,12 @@
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\BackgroundJob\Events\BeforeJobExecutedEvent;
use OCP\BackgroundJob\Events\JobExecutedEvent;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
use OCP\Util;
use Psr\Container\ContainerInterface;
use function class_exists;
use function Sentry\init as initSentry;

class Application extends App implements IBootstrap {
Expand All @@ -60,6 +64,12 @@
/** @psalm-suppress TooManyArguments */
$context->registerMiddleware(PerformanceMonitoringMiddleware::class, true);
$context->registerEventListener(AddContentSecurityPolicyEvent::class, CustomCspListener::class);
if (class_exists(BeforeJobExecutedEvent::class)) {
Copy link
Collaborator

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

Copy link
Owner Author

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

$context->registerEventListener(BeforeJobExecutedEvent::class, BackgroundJobListener::class);

Check failure on line 68 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable29

InvalidArgument

lib/AppInfo/Application.php:68:14: InvalidArgument: Incompatible types found for T (OCP\BackgroundJob\Events\BeforeJobExecutedEvent|OCP\EventDispatcher\Event is not in OCP\EventDispatcher\Event) (see https://psalm.dev/004)

Check failure on line 68 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable31

InvalidArgument

lib/AppInfo/Application.php:68:14: InvalidArgument: Incompatible types found for T (OCP\BackgroundJob\Events\BeforeJobExecutedEvent|OCP\EventDispatcher\Event is not in OCP\EventDispatcher\Event) (see https://psalm.dev/004)

Check failure on line 68 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidArgument

lib/AppInfo/Application.php:68:14: InvalidArgument: Incompatible types found for T (OCP\BackgroundJob\Events\BeforeJobExecutedEvent|OCP\EventDispatcher\Event is not in OCP\EventDispatcher\Event) (see https://psalm.dev/004)

Check failure on line 68 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable30

InvalidArgument

lib/AppInfo/Application.php:68:14: InvalidArgument: Incompatible types found for T (OCP\BackgroundJob\Events\BeforeJobExecutedEvent|OCP\EventDispatcher\Event is not in OCP\EventDispatcher\Event) (see https://psalm.dev/004)
}
if (class_exists(JobExecutedEvent::class)) {
$context->registerEventListener(JobExecutedEvent::class, BackgroundJobListener::class);

Check failure on line 71 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable29

InvalidArgument

lib/AppInfo/Application.php:71:14: InvalidArgument: Incompatible types found for T (OCP\BackgroundJob\Events\JobExecutedEvent|OCP\EventDispatcher\Event is not in OCP\EventDispatcher\Event) (see https://psalm.dev/004)

Check failure on line 71 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable31

InvalidArgument

lib/AppInfo/Application.php:71:14: InvalidArgument: Incompatible types found for T (OCP\BackgroundJob\Events\JobExecutedEvent|OCP\EventDispatcher\Event is not in OCP\EventDispatcher\Event) (see https://psalm.dev/004)

Check failure on line 71 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidArgument

lib/AppInfo/Application.php:71:14: InvalidArgument: Incompatible types found for T (OCP\BackgroundJob\Events\JobExecutedEvent|OCP\EventDispatcher\Event is not in OCP\EventDispatcher\Event) (see https://psalm.dev/004)

Check failure on line 71 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable30

InvalidArgument

lib/AppInfo/Application.php:71:14: InvalidArgument: Incompatible types found for T (OCP\BackgroundJob\Events\JobExecutedEvent|OCP\EventDispatcher\Event is not in OCP\EventDispatcher\Event) (see https://psalm.dev/004)
}
$context->registerInitialStateProvider(DsnProvider::class);
}

Expand Down
50 changes: 50 additions & 0 deletions lib/Listener/BackgroundJobListener.php
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

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable29

MissingTemplateParam

lib/Listener/BackgroundJobListener.php:23:40: MissingTemplateParam: OCA\Sentry\Listener\BackgroundJobListener has missing template params when extending OCP\EventDispatcher\IEventListener, expecting 1 (see https://psalm.dev/182)

Check failure on line 23 in lib/Listener/BackgroundJobListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable31

MissingTemplateParam

lib/Listener/BackgroundJobListener.php:23:40: MissingTemplateParam: OCA\Sentry\Listener\BackgroundJobListener has missing template params when extending OCP\EventDispatcher\IEventListener, expecting 1 (see https://psalm.dev/182)

Check failure on line 23 in lib/Listener/BackgroundJobListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

MissingTemplateParam

lib/Listener/BackgroundJobListener.php:23:40: MissingTemplateParam: OCA\Sentry\Listener\BackgroundJobListener has missing template params when extending OCP\EventDispatcher\IEventListener, expecting 1 (see https://psalm.dev/182)

Check failure on line 23 in lib/Listener/BackgroundJobListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable30

MissingTemplateParam

lib/Listener/BackgroundJobListener.php:23:40: MissingTemplateParam: OCA\Sentry\Listener\BackgroundJobListener has missing template params when extending OCP\EventDispatcher\IEventListener, expecting 1 (see https://psalm.dev/182)

private array $transactions;

public function handle(Event $event): void {
if ($event instanceof BeforeJobExecutedEvent) {

Check failure on line 28 in lib/Listener/BackgroundJobListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable29

UndefinedClass

lib/Listener/BackgroundJobListener.php:28:25: UndefinedClass: Class, interface or enum named OCP\BackgroundJob\Events\BeforeJobExecutedEvent does not exist (see https://psalm.dev/019)

Check failure on line 28 in lib/Listener/BackgroundJobListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable31

UndefinedClass

lib/Listener/BackgroundJobListener.php:28:25: UndefinedClass: Class, interface or enum named OCP\BackgroundJob\Events\BeforeJobExecutedEvent does not exist (see https://psalm.dev/019)

Check failure on line 28 in lib/Listener/BackgroundJobListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

UndefinedClass

lib/Listener/BackgroundJobListener.php:28:25: UndefinedClass: Class, interface or enum named OCP\BackgroundJob\Events\BeforeJobExecutedEvent does not exist (see https://psalm.dev/019)

Check failure on line 28 in lib/Listener/BackgroundJobListener.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable30

UndefinedClass

lib/Listener/BackgroundJobListener.php:28:25: UndefinedClass: Class, interface or enum named OCP\BackgroundJob\Events\BeforeJobExecutedEvent does not exist (see https://psalm.dev/019)
$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()]);
}
}
}

}
Loading