Skip to content

Commit

Permalink
Move logging logic of guzzle logging to private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett committed Dec 11, 2024
1 parent ffaff38 commit 6aaf4de
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 52 deletions.
8 changes: 4 additions & 4 deletions src/ApplicationDefaultCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
*/
class ApplicationDefaultCredentials
{
private const SDK_DEBUG_FLAG = 'GOOGLE_SDK_PHP_LOGGING';
private const SDK_DEBUG_ENV_VAR = 'GOOGLE_SDK_PHP_LOGGING';

/**
* @deprecated
Expand Down Expand Up @@ -333,10 +333,10 @@ public static function getIdTokenCredentials(
*/
public static function getDefaultLogger(): null|LoggerInterface
{
$loggingFlag = getenv(self::SDK_DEBUG_FLAG);
$loggingFlag = getenv(self::SDK_DEBUG_ENV_VAR);

// Env var is not set
if (!$loggingFlag) {
if (empty($loggingFlag)) {
return null;
}

Expand All @@ -345,7 +345,7 @@ public static function getDefaultLogger(): null|LoggerInterface
// Env Var is not true
if ($loggingFlag !== 'true') {
if ($loggingFlag !== 'false') {
trigger_error('The ' . self::SDK_DEBUG_FLAG . ' is set, but it is set to another value than false or true. Logging is disabled');
trigger_error('The ' . self::SDK_DEBUG_ENV_VAR . ' is set, but it is set to another value than false or true. Logging is disabled');
}

return null;
Expand Down
81 changes: 38 additions & 43 deletions src/HttpHandler/Guzzle6HttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,13 @@ public function __invoke(RequestInterface $request, array $options = [])
$requestEvent = null;

if ($this->logger) {
$requestEvent = new RpcLogEvent();

$requestEvent->method = $request->getMethod();
$requestEvent->url = (string) $request->getUri();
$requestEvent->headers = $request->getHeaders();
$requestEvent->payload = $request->getBody()->getContents();
$requestEvent->retryAttempt = $options['retryAttempt'] ?? null;
$requestEvent->serviceName = $options['serviceName'] ?? null;
$requestEvent->processId = (int) getmypid();
$requestEvent->requestId = $options['requestId'] ?? crc32((string) spl_object_id($request) . getmypid());

$this->logRequest($requestEvent);
$requestEvent = $this->requestLog($request, $options);
}

$response = $this->client->send($request, $options);

if ($this->logger) {
$responseEvent = new RpcLogEvent($requestEvent->milliseconds);

$responseEvent->headers = $response->getHeaders();
$responseEvent->payload = $response->getBody()->getContents();
$responseEvent->status = $response->getStatusCode();
$responseEvent->processId = $requestEvent->processId;
$responseEvent->requestId = $requestEvent->requestId;

$this->logResponse($responseEvent);
$this->responseLog($response, $requestEvent);
}

return $response;
Expand All @@ -103,38 +84,52 @@ public function async(RequestInterface $request, array $options = [])
$requestEvent = null;

if ($this->logger) {
$requestEvent = new RpcLogEvent();

$requestEvent->method = $request->getMethod();
$requestEvent->url = (string) $request->getUri();
$requestEvent->headers = $request->getHeaders();
$requestEvent->payload = $request->getBody()->getContents();
$requestEvent->retryAttempt = $options['retryAttempt'] ?? null;
$requestEvent->serviceName = $options['serviceName'] ?? null;
$requestEvent->processId = (int) getmypid();
$requestEvent->requestId = $options['requestId'] ?? crc32((string) spl_object_id($request) . getmypid());

$this->logRequest($requestEvent);
$requestEvent = $this->requestLog($request, $options);
}

$promise = $this->client->sendAsync($request, $options);

if ($this->logger) {
$promise->then(function (ResponseInterface $response) use ($requestEvent) {
$responseEvent = new RpcLogEvent($requestEvent->milliseconds);

$responseEvent->headers = $response->getHeaders();
$responseEvent->payload = $response->getBody()->getContents();
$responseEvent->status = $response->getStatusCode();
$responseEvent->processId = $requestEvent->processId;
$responseEvent->requestId = $requestEvent->requestId;

$this->logResponse($responseEvent);

$this->responseLog($response, $requestEvent);
return $response;
});
}

return $promise;
}

/**
* @internal
*/
public function requestLog(RequestInterface $request, array $options): RpcLogEvent

Check failure on line 105 in src/HttpHandler/Guzzle6HttpHandler.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis / PHPStan Static Analysis

Method Google\Auth\HttpHandler\Guzzle6HttpHandler::requestLog() has parameter $options with no value type specified in iterable type array.
{
$requestEvent = new RpcLogEvent();

$requestEvent->method = $request->getMethod();
$requestEvent->url = (string) $request->getUri();
$requestEvent->headers = $request->getHeaders();
$requestEvent->payload = $request->getBody()->getContents();
$requestEvent->retryAttempt = $options['retryAttempt'] ?? null;
$requestEvent->serviceName = $options['serviceName'] ?? null;
$requestEvent->processId = (int) getmypid();
$requestEvent->requestId = $options['requestId'] ?? crc32((string) spl_object_id($request) . getmypid());

$this->logRequest($requestEvent);

return $requestEvent;
}

public function responseLog(ResponseInterface $response, RpcLogEvent $requestEvent): void
{
$responseEvent = new RpcLogEvent($requestEvent->milliseconds);

$responseEvent->headers = $response->getHeaders();
$responseEvent->payload = $response->getBody()->getContents();
$responseEvent->status = $response->getStatusCode();
$responseEvent->processId = $requestEvent->processId;
$responseEvent->requestId = $requestEvent->requestId;

$this->logResponse($responseEvent);
}
}
10 changes: 5 additions & 5 deletions tests/ApplicationDefaultCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ApplicationDefaultCredentialsTest extends TestCase
private $targetAudience = 'a target audience';
private $quotaProject = 'a-quota-project';
private $originalServiceAccount;
private const SDK_DEBUG_FLAG = 'GOOGLE_SDK_PHP_LOGGING';
private const SDK_DEBUG_ENV_VAR = 'GOOGLE_SDK_PHP_LOGGING';

public function testGetCredentialsFailsIfEnvSpecifiesNonExistentFile()
{
Expand Down Expand Up @@ -777,27 +777,27 @@ public function testExternalAccountCredentials(string $jsonFile, string $expecte

public function testGetDefaultLoggerReturnStdOutLoggerIfEnvVarIsPresent()
{
putenv($this::SDK_DEBUG_FLAG . '=true');
putenv($this::SDK_DEBUG_ENV_VAR . '=true');
$logger = ApplicationDefaultCredentials::getDefaultLogger();
$this->assertTrue($logger instanceof StdOutLogger);
}

public function testGetDefaultLoggerReturnsNullIfNotEnvVar()
{
putenv($this::SDK_DEBUG_FLAG . '=false');
putenv($this::SDK_DEBUG_ENV_VAR . '=false');
$logger = ApplicationDefaultCredentials::getDefaultLogger();

$this->assertNull($logger);

putenv($this::SDK_DEBUG_FLAG . '=0');
putenv($this::SDK_DEBUG_ENV_VAR . '=0');
$logger = ApplicationDefaultCredentials::getDefaultLogger();

$this->assertNull($logger);
}

public function testGetDefaultLoggerRaiseAWarningIfMisconfiguredAndReturnsNull()
{
putenv($this::SDK_DEBUG_FLAG . '=invalid');
putenv($this::SDK_DEBUG_ENV_VAR . '=invalid');
$this->expectException(Notice::class);
$logger = ApplicationDefaultCredentials::getDefaultLogger();

Expand Down

0 comments on commit 6aaf4de

Please sign in to comment.