Skip to content

Commit

Permalink
speed: removed debug logging to aa_debug.log (#320)
Browse files Browse the repository at this point in the history
For every incoming request to the cloud, we do an extra read from the
database.
For every outgoing request, we do the same.

Anyway, we haven’t used this thing once in a year; it’s easier to remove
it than to transfer this flag to the ex_app table to optimize speed.

Signed-off-by: Alexander Piskun <[email protected]>
  • Loading branch information
bigcat88 authored Jul 10, 2024
1 parent 0733a5a commit 7820f65
Showing 1 changed file with 0 additions and 63 deletions.
63 changes: 0 additions & 63 deletions lib/Service/AppAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IPromise;
use OCP\Http\Client\IResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Log\ILogFactory;
use OCP\Security\Bruteforce\IThrottler;
use Psr\Log\LoggerInterface;

Expand All @@ -32,9 +30,7 @@ class AppAPIService {

public function __construct(
private readonly LoggerInterface $logger,
private readonly ILogFactory $logFactory,
private readonly IThrottler $throttler,
private readonly IConfig $config,
IClientService $clientService,
private readonly IUserSession $userSession,
private readonly ISession $session,
Expand All @@ -44,7 +40,6 @@ public function __construct(
private readonly ExAppService $exAppService,
private readonly ExAppUsersService $exAppUsersService,
private readonly ExAppApiScopeService $exAppApiScopeService,
private readonly ExAppConfigService $exAppConfigService,
private readonly DockerActions $dockerActions,
private readonly ManualActions $manualActions,
private readonly AppAPICommonService $commonService,
Expand Down Expand Up @@ -216,7 +211,6 @@ private function prepareRequestToExApp(
array $options,
?IRequest $request,
): array {
$this->handleExAppDebug($exApp->getAppid(), $request, true);
$auth = [];
$url = $this->getExAppUrl($exApp, $exApp->getPort(), $auth);
if (str_starts_with($route, '/')) {
Expand Down Expand Up @@ -263,7 +257,6 @@ private function prepareRequestToExApp2(
array $options,
?IRequest $request,
): array {
$this->handleExAppDebug($exApp->getAppid(), $request, true);
$auth = [];
$url = $this->getExAppUrl($exApp, $exApp->getPort(), $auth);
if (str_starts_with($route, '/')) {
Expand Down Expand Up @@ -341,8 +334,6 @@ public function validateExAppRequestToNC(IRequest $request, bool $isDav = false)
return false;
}

$this->handleExAppDebug($exApp->getAppid(), $request, false);

$authorization = base64_decode($request->getHeader('AUTHORIZATION-APP-API'));
if ($authorization === false) {
$this->logger->error('Failed to parse AUTHORIZATION-APP-API');
Expand Down Expand Up @@ -460,60 +451,6 @@ public function passesScopeCheck(ExApp $exApp, int $apiScope): bool {
return false;
}

private function buildRequestInfo(IRequest $request): array {
$headers = [];
$aeHeadersList = [
'AA-VERSION',
'EX-APP-VERSION',
];
foreach ($aeHeadersList as $header) {
if ($request->getHeader($header) !== '') {
$headers[$header] = $request->getHeader($header);
}
}
return [
'headers' => $headers,
'params' => $request->getParams(),
];
}

private function getExAppDebugSettings(string $appId): array {
$exAppConfigs = $this->exAppConfigService->getAppConfigValues($appId, ['debug', 'loglevel']);
$debug = false;
$level = $this->config->getSystemValue('loglevel', 2);
foreach ($exAppConfigs as $exAppConfig) {
if ($exAppConfig['configkey'] === 'debug') {
$debug = $exAppConfig['configvalue'] === 1;
}
if ($exAppConfig['configkey'] === 'loglevel') {
$level = intval($exAppConfig['configvalue']);
}
}
return [
'debug' => $debug,
'level' => $level,
];
}

private function getCustomLogger(string $name): LoggerInterface {
$path = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $name;
return $this->logFactory->getCustomPsrLogger($path);
}

private function handleExAppDebug(string $appId, ?IRequest $request, bool $fromNextcloud = true): void {
$exAppDebugSettings = $this->getExAppDebugSettings($appId);
if ($exAppDebugSettings['debug']) {
$message = $fromNextcloud
? '[' . Application::APP_ID . '] Nextcloud --> ' . $appId
: '[' . Application::APP_ID . '] ' . $appId . ' --> Nextcloud';
$aeDebugLogger = $this->getCustomLogger('aa_debug.log');
$aeDebugLogger->log($exAppDebugSettings['level'], $message, [
'app' => $appId,
'request_info' => $request instanceof IRequest ? $this->buildRequestInfo($request) : 'CLI request',
]);
}
}

/**
* Check if ExApp version changed and update it in database.
* Immediately disable ExApp and send notifications to the administrators (users of admins group).
Expand Down

0 comments on commit 7820f65

Please sign in to comment.