Skip to content

Commit

Permalink
Merge pull request #37838 from nextcloud/event-logger-http
Browse files Browse the repository at this point in the history
log performance events for http requests
  • Loading branch information
icewind1991 authored May 9, 2023
2 parents 416f632 + e7ab30f commit 904fdf3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
21 changes: 17 additions & 4 deletions lib/private/Http/Client/ClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Middleware;
use OCP\Diagnostics\IEventLogger;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\ICertificateManager;
use OCP\IConfig;
use OCP\Security\IRemoteHostValidator;
use Psr\Http\Message\RequestInterface;

/**
* Class ClientService
Expand All @@ -48,15 +51,20 @@ class ClientService implements IClientService {
/** @var DnsPinMiddleware */
private $dnsPinMiddleware;
private IRemoteHostValidator $remoteHostValidator;
private IEventLogger $eventLogger;

public function __construct(IConfig $config,
ICertificateManager $certificateManager,
DnsPinMiddleware $dnsPinMiddleware,
IRemoteHostValidator $remoteHostValidator) {
public function __construct(
IConfig $config,
ICertificateManager $certificateManager,
DnsPinMiddleware $dnsPinMiddleware,
IRemoteHostValidator $remoteHostValidator,
IEventLogger $eventLogger,
) {
$this->config = $config;
$this->certificateManager = $certificateManager;
$this->dnsPinMiddleware = $dnsPinMiddleware;
$this->remoteHostValidator = $remoteHostValidator;
$this->eventLogger = $eventLogger;
}

/**
Expand All @@ -66,6 +74,11 @@ public function newClient(): IClient {
$handler = new CurlHandler();
$stack = HandlerStack::create($handler);
$stack->push($this->dnsPinMiddleware->addDnsPinning());
$stack->push(Middleware::tap(function (RequestInterface $request) {
$this->eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget());
}, function () {
$this->eventLogger->end('http:request');
}), 'event logger');

$client = new GuzzleClient(['handler' => $stack]);

Expand Down
12 changes: 11 additions & 1 deletion tests/lib/Http/Client/ClientServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Middleware;
use OC\Http\Client\Client;
use OC\Http\Client\ClientService;
use OC\Http\Client\DnsPinMiddleware;
use OCP\Diagnostics\IEventLogger;
use OCP\ICertificateManager;
use OCP\IConfig;
use OCP\Security\IRemoteHostValidator;
use Psr\Http\Message\RequestInterface;

/**
* Class ClientServiceTest
Expand All @@ -37,17 +40,24 @@ public function testNewClient(): void {
->willReturn(function () {
});
$remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
$eventLogger = $this->createMock(IEventLogger::class);

$clientService = new ClientService(
$config,
$certificateManager,
$dnsPinMiddleware,
$remoteHostValidator
$remoteHostValidator,
$eventLogger
);

$handler = new CurlHandler();
$stack = HandlerStack::create($handler);
$stack->push($dnsPinMiddleware->addDnsPinning());
$stack->push(Middleware::tap(function (RequestInterface $request) use ($eventLogger) {
$eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget());
}, function () use ($eventLogger) {
$eventLogger->end('http:request');
}), 'event logger');
$guzzleClient = new GuzzleClient(['handler' => $stack]);

$this->assertEquals(
Expand Down

0 comments on commit 904fdf3

Please sign in to comment.