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
13 changes: 13 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\CommandListener;
use OCA\Sentry\Listener\CustomCspListener;
use OCA\Sentry\Reporter\ISentryReporter;
use OCA\Sentry\Reporter\RecursionAwareReporter;
Expand All @@ -35,6 +36,9 @@
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Command\Events\BeforeCommandExecutedEvent;
use OCP\Command\Events\CommandExecutedEvent;
use OCP\Command\Events\CommandFailedEvent;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
use OCP\Util;
use Psr\Container\ContainerInterface;
Expand All @@ -60,6 +64,15 @@ public function register(IRegistrationContext $context): void {
/** @psalm-suppress TooManyArguments */
$context->registerMiddleware(PerformanceMonitoringMiddleware::class, true);
$context->registerEventListener(AddContentSecurityPolicyEvent::class, CustomCspListener::class);
}
if (class_exists(BeforeCommandExecutedEvent::class)) {
$context->registerEventListener(BeforeCommandExecutedEvent::class, CommandListener::class);
}
if (class_exists(CommandExecutedEvent::class)) {
$context->registerEventListener(CommandExecutedEvent::class, CommandListener::class);
}
if (class_exists(CommandFailedEvent::class)) {
$context->registerEventListener(CommandFailedEvent::class, CommandListener::class);
$context->registerInitialStateProvider(DsnProvider::class);
}

Expand Down
58 changes: 58 additions & 0 deletions lib/Listener/CommandListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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\Command\Events\BeforeCommandExecutedEvent;
use OCP\Command\Events\CommandExecutedEvent;
use OCP\Command\Events\CommandFailedEvent;
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 CommandListener implements IEventListener {

private array $transactions;

public function handle(Event $event): void {
if ($event instanceof BeforeCommandExecutedEvent) {
$transactionContext = new TransactionContext();
$transactionContext->setName('occ ' . $event->getCommand());
$transactionContext->setOp('cli');
$transaction = $this->transactions[$event->getCommand()] = startTransaction($transactionContext);
SentrySdk::getCurrentHub()->setSpan($transaction);
}
if ($event instanceof CommandExecutedEvent) {
$array_key_exists = array_key_exists($event->getCommand(), $this->transactions);
if ($array_key_exists) {
$transaction = $this->transactions[$event->getCommand()];
$transaction->setStatus(SpanStatus::ok());
$transaction->finish();
unset($this->transactions[$event->getCommand()]);
}
}
if ($event instanceof CommandFailedEvent) {
$array_key_exists = array_key_exists($event->getCommand(), $this->transactions);
if ($array_key_exists) {
$transaction = $this->transactions[$event->getCommand()];
$transaction->setStatus(SpanStatus::unknownError());
$transaction->finish();
unset($this->transactions[$event->getCommand()]);
}
}
}

}