Skip to content

Commit bc3a004

Browse files
authored
Merge pull request #57433 from nextcloud/backport/55265/stable32
2 parents 5e6b315 + b0cfb45 commit bc3a004

File tree

6 files changed

+80
-29
lines changed

6 files changed

+80
-29
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,7 @@
16041604
'OC\\Diagnostics\\EventLogger' => $baseDir . '/lib/private/Diagnostics/EventLogger.php',
16051605
'OC\\Diagnostics\\Query' => $baseDir . '/lib/private/Diagnostics/Query.php',
16061606
'OC\\Diagnostics\\QueryLogger' => $baseDir . '/lib/private/Diagnostics/QueryLogger.php',
1607+
'OC\\Diagnostics\\TLogSlowOperation' => $baseDir . '/lib/private/Diagnostics/TLogSlowOperation.php',
16071608
'OC\\DirectEditing\\Manager' => $baseDir . '/lib/private/DirectEditing/Manager.php',
16081609
'OC\\DirectEditing\\Token' => $baseDir . '/lib/private/DirectEditing/Token.php',
16091610
'OC\\EmojiHelper' => $baseDir . '/lib/private/EmojiHelper.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
16451645
'OC\\Diagnostics\\EventLogger' => __DIR__ . '/../../..' . '/lib/private/Diagnostics/EventLogger.php',
16461646
'OC\\Diagnostics\\Query' => __DIR__ . '/../../..' . '/lib/private/Diagnostics/Query.php',
16471647
'OC\\Diagnostics\\QueryLogger' => __DIR__ . '/../../..' . '/lib/private/Diagnostics/QueryLogger.php',
1648+
'OC\\Diagnostics\\TLogSlowOperation' => __DIR__ . '/../../..' . '/lib/private/Diagnostics/TLogSlowOperation.php',
16481649
'OC\\DirectEditing\\Manager' => __DIR__ . '/../../..' . '/lib/private/DirectEditing/Manager.php',
16491650
'OC\\DirectEditing\\Token' => __DIR__ . '/../../..' . '/lib/private/DirectEditing/Token.php',
16501651
'OC\\EmojiHelper' => __DIR__ . '/../../..' . '/lib/private/EmojiHelper.php',
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-only
8+
*/
9+
10+
namespace OC\Diagnostics;
11+
12+
use OCP\ILogger;
13+
use Psr\Log\LoggerInterface;
14+
use function microtime;
15+
16+
trait TLogSlowOperation {
17+
18+
/**
19+
* @template R
20+
* @param LoggerInterface $logger
21+
* @param string $operation
22+
* @param callable $fn
23+
* @psalm-param callable(): R $fn
24+
*
25+
* @return mixed
26+
*/
27+
public function monitorAndLog(LoggerInterface $logger, string $operation, callable $fn): mixed {
28+
$timeBefore = microtime(true);
29+
$result = $fn();
30+
$timeAfter = microtime(true);
31+
$timeSpent = $timeAfter - $timeBefore;
32+
if ($timeSpent > 0.1) {
33+
$logLevel = match (true) {
34+
$timeSpent > 25 => ILogger::ERROR,
35+
$timeSpent > 10 => ILogger::WARN,
36+
$timeSpent > 0.5 => ILogger::INFO,
37+
default => ILogger::DEBUG,
38+
};
39+
$logger->log(
40+
$logLevel,
41+
"Slow $operation detected",
42+
[
43+
'timeSpent' => $timeSpent,
44+
],
45+
);
46+
}
47+
return $result;
48+
}
49+
50+
}

lib/private/Http/Client/DnsPinMiddleware.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@
88
*/
99
namespace OC\Http\Client;
1010

11+
use OC\Diagnostics\TLogSlowOperation;
1112
use OC\Net\IpAddressClassifier;
1213
use OCP\Http\Client\LocalServerException;
1314
use Psr\Http\Message\RequestInterface;
15+
use Psr\Log\LoggerInterface;
1416

1517
class DnsPinMiddleware {
1618

19+
use TLogSlowOperation;
20+
1721
public function __construct(
1822
private NegativeDnsCache $negativeDnsCache,
1923
private IpAddressClassifier $ipAddressClassifier,
24+
private LoggerInterface $logger,
2025
) {
2126
}
2227

@@ -88,7 +93,11 @@ private function dnsResolve(string $target, int $recursionCount) : array {
8893
* Wrapper for dns_get_record
8994
*/
9095
protected function dnsGetRecord(string $hostname, int $type): array|false {
91-
return \dns_get_record($hostname, $type);
96+
return $this->monitorAndLog(
97+
$this->logger,
98+
'dns_get_record',
99+
fn () => \dns_get_record($hostname, $type),
100+
);
92101
}
93102

94103
public function addDnsPinning(): callable {

lib/private/Session/Internal.php

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
namespace OC\Session;
1111

1212
use OC\Authentication\Token\IProvider;
13+
use OC\Diagnostics\TLogSlowOperation;
1314
use OCP\Authentication\Exceptions\InvalidTokenException;
14-
use OCP\ILogger;
1515
use OCP\Session\Exceptions\SessionNotAvailableException;
1616
use Psr\Log\LoggerInterface;
1717
use function call_user_func_array;
18-
use function microtime;
1918

2019
/**
2120
* Class Internal
@@ -25,6 +24,9 @@
2524
* @package OC\Session
2625
*/
2726
class Internal extends Session {
27+
28+
use TLogSlowOperation;
29+
2830
/**
2931
* @param string $name
3032
* @throws \Exception
@@ -191,31 +193,17 @@ public function trapError(int $errorNumber, string $errorString) {
191193
*/
192194
private function invoke(string $functionName, array $parameters = [], bool $silence = false) {
193195
try {
194-
$timeBefore = microtime(true);
195-
if ($silence) {
196-
$result = @call_user_func_array($functionName, $parameters);
197-
} else {
198-
$result = call_user_func_array($functionName, $parameters);
199-
}
200-
$timeAfter = microtime(true);
201-
$timeSpent = $timeAfter - $timeBefore;
202-
if ($timeSpent > 0.1) {
203-
$logLevel = match (true) {
204-
$timeSpent > 25 => ILogger::ERROR,
205-
$timeSpent > 10 => ILogger::WARN,
206-
$timeSpent > 0.5 => ILogger::INFO,
207-
default => ILogger::DEBUG,
208-
};
209-
$this->logger?->log(
210-
$logLevel,
211-
"Slow session operation $functionName detected",
212-
[
213-
'parameters' => $parameters,
214-
'timeSpent' => $timeSpent,
215-
],
216-
);
217-
}
218-
return $result;
196+
return $this->monitorAndLog(
197+
$this->logger,
198+
$functionName,
199+
function () use ($silence, $functionName, $parameters) {
200+
if ($silence) {
201+
return @call_user_func_array($functionName, $parameters);
202+
} else {
203+
return call_user_func_array($functionName, $parameters);
204+
}
205+
}
206+
);
219207
} catch (\Error $e) {
220208
$this->trapError($e->getCode(), $e->getMessage());
221209
}

tests/lib/Http/Client/DnsPinMiddlewareTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use OCP\Http\Client\LocalServerException;
2121
use OCP\ICacheFactory;
2222
use Psr\Http\Message\RequestInterface;
23+
use Psr\Log\NullLogger;
2324
use Test\TestCase;
2425

2526
class DnsPinMiddlewareTest extends TestCase {
@@ -35,9 +36,10 @@ protected function setUp(): void {
3536

3637
$ipAddressClassifier = new IpAddressClassifier();
3738
$negativeDnsCache = new NegativeDnsCache($cacheFactory);
39+
$logger = new NullLogger();
3840

3941
$this->dnsPinMiddleware = $this->getMockBuilder(DnsPinMiddleware::class)
40-
->setConstructorArgs([$negativeDnsCache, $ipAddressClassifier])
42+
->setConstructorArgs([$negativeDnsCache, $ipAddressClassifier, $logger])
4143
->onlyMethods(['dnsGetRecord'])
4244
->getMock();
4345
}

0 commit comments

Comments
 (0)