-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Debug Logging prototype #570
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8f4d137
Add default logger to the Auth library
Hectorhammett 23dc341
Add default logger to the HTTP Handler
Hectorhammett f1f1693
Fix namespace for LoggingTrait
Hectorhammett 4ab59ac
Add startTime variable declaration
Hectorhammett 6271d0f
Refactor the logging trait
Hectorhammett 4b9f2ab
Fix typings
Hectorhammett 3f136b1
Fix typing
Hectorhammett 6da8e82
Fix Styling
Hectorhammett e936e02
Add ServiceName to the LogEvent
Hectorhammett a133706
Change payload type to string
Hectorhammett 3fe52dd
Fix code Style
Hectorhammett 795c4ce
Add clientId and RequestId to the LogEvent class
Hectorhammett 315b070
Fix type hit for requestId
Hectorhammett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,23 +16,34 @@ | |
*/ | ||
namespace Google\Auth\HttpHandler; | ||
|
||
use Google\Auth\Logger\LogEvent; | ||
use Google\Auth\Logger\LoggingTrait; | ||
use GuzzleHttp\ClientInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class Guzzle6HttpHandler | ||
{ | ||
use LoggingTrait; | ||
|
||
/** | ||
* @var ClientInterface | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @param ClientInterface $client | ||
*/ | ||
public function __construct(ClientInterface $client) | ||
public function __construct(ClientInterface $client, LoggerInterface $logger = null) | ||
{ | ||
$this->client = $client; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
|
@@ -57,6 +68,37 @@ public function __invoke(RequestInterface $request, array $options = []) | |
*/ | ||
public function async(RequestInterface $request, array $options = []) | ||
{ | ||
return $this->client->sendAsync($request, $options); | ||
$requestEvent = null; | ||
|
||
if ($this->logger) { | ||
$requestEvent = new LogEvent(); | ||
|
||
$requestEvent->method = $request->getMethod(); | ||
$requestEvent->url = $request->getUri()->__toString(); | ||
$requestEvent->headers = $request->getHeaders(); | ||
$requestEvent->payload = $request->getBody()->getContents(); | ||
$requestEvent->retryAttempt = $options['retryAttempt'] ?? null; | ||
$requestEvent->serviceName = $options['serviceName']; | ||
$requestEvent->clientId = $options['clientId']; | ||
$requestEvent->requestId = spl_object_id($request); | ||
|
||
$this->logRequest($requestEvent); | ||
} | ||
|
||
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) use ($requestEvent) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, but this could be attached only when the logger is defined, which would look a little more cleaner. e.g. $promise = $this->client->sendAsync($request, $options);
if ($this->logger) {
$promise->then( /** ... */);
}
return $promise; |
||
if ($this->logger) { | ||
$responseEvent = new LogEvent($requestEvent->timestamp); | ||
|
||
$responseEvent->headers = $response->getHeaders(); | ||
$responseEvent->payload = json_decode($response->getBody()->getContents()) ?: null; | ||
$responseEvent->status = $response->getStatusCode(); | ||
$responseEvent->clientId = $requestEvent->clientId; | ||
$responseEvent->requestId = $requestEvent->requestId; | ||
|
||
$this->logResponse($responseEvent); | ||
} | ||
|
||
return $response; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,11 +16,13 @@ | |||||||||
*/ | ||||||||||
namespace Google\Auth\HttpHandler; | ||||||||||
|
||||||||||
use Google\Auth\ApplicationDefaultCredentials; | ||||||||||
use GuzzleHttp\BodySummarizer; | ||||||||||
use GuzzleHttp\Client; | ||||||||||
use GuzzleHttp\ClientInterface; | ||||||||||
use GuzzleHttp\HandlerStack; | ||||||||||
use GuzzleHttp\Middleware; | ||||||||||
use Psr\Log\LoggerInterface; | ||||||||||
|
||||||||||
class HttpHandlerFactory | ||||||||||
{ | ||||||||||
|
@@ -31,7 +33,7 @@ class HttpHandlerFactory | |||||||||
* @return Guzzle6HttpHandler|Guzzle7HttpHandler | ||||||||||
* @throws \Exception | ||||||||||
*/ | ||||||||||
public static function build(ClientInterface $client = null) | ||||||||||
public static function build(ClientInterface $client = null, ?LoggerInterface $logger = null) | ||||||||||
{ | ||||||||||
if (is_null($client)) { | ||||||||||
$stack = null; | ||||||||||
|
@@ -42,6 +44,7 @@ public static function build(ClientInterface $client = null) | |||||||||
$stack->remove('http_errors'); | ||||||||||
$stack->unshift(Middleware::httpErrors($bodySummarizer), 'http_errors'); | ||||||||||
} | ||||||||||
|
||||||||||
$client = new Client(['handler' => $stack]); | ||||||||||
} | ||||||||||
|
||||||||||
|
@@ -52,11 +55,15 @@ public static function build(ClientInterface $client = null) | |||||||||
$version = (int) substr(ClientInterface::VERSION, 0, 1); | ||||||||||
} | ||||||||||
|
||||||||||
if (!$logger) { | ||||||||||
$logger = ApplicationDefaultCredentials::getDefaultLogger(); | ||||||||||
} | ||||||||||
Comment on lines
+58
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit (suggestion) - make this one line
Suggested change
|
||||||||||
|
||||||||||
switch ($version) { | ||||||||||
case 6: | ||||||||||
return new Guzzle6HttpHandler($client); | ||||||||||
return new Guzzle6HttpHandler($client, $logger); | ||||||||||
case 7: | ||||||||||
return new Guzzle7HttpHandler($client); | ||||||||||
return new Guzzle7HttpHandler($client, $logger); | ||||||||||
default: | ||||||||||
throw new \Exception('Version not supported'); | ||||||||||
} | ||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
/** | ||
* Copyright 2024 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Auth\Logger; | ||
|
||
class LogEvent | ||
{ | ||
/** | ||
* Timestamp in format RFC3339 representing when this event ocurred | ||
* | ||
* @var null|string | ||
*/ | ||
public null|string $timestamp = null; | ||
|
||
/** | ||
* Rest method type | ||
* | ||
* @var null|string | ||
*/ | ||
public null|string $method = null; | ||
|
||
/** | ||
* URL representing the rest URL endpoint | ||
* | ||
* @var null|string | ||
*/ | ||
public null|string $url = null; | ||
|
||
/** | ||
* An array that contains the headers for the response or request | ||
* | ||
* @var null|array<mixed> | ||
*/ | ||
public null|array $headers = null; | ||
|
||
/** | ||
* An array representation of JSON for the response or request | ||
* | ||
* @var null|string | ||
*/ | ||
public null|string $payload = null; | ||
|
||
/** | ||
* Status code for REST or gRPC methods | ||
* | ||
* @var null|int|string | ||
*/ | ||
public null|int|string $status = null; | ||
|
||
/** | ||
* The latency in miliseconds | ||
* | ||
* @var null|int | ||
*/ | ||
public null|int $latency = null; | ||
|
||
/** | ||
* The retry attempt number | ||
* | ||
* @var null|int | ||
*/ | ||
public null|int $retryAttempt = null; | ||
|
||
/** | ||
* The name of the gRPC method being called | ||
* | ||
* @var null|string | ||
*/ | ||
public null|string $rpcName = null; | ||
|
||
/** | ||
* The Service Name of the gRPC | ||
* | ||
* @var null|string $serviceName | ||
*/ | ||
public null|string $serviceName; | ||
|
||
/** | ||
* The Client Id for easy trace | ||
* | ||
* @var int $clientId | ||
*/ | ||
public int $clientId; | ||
|
||
/** | ||
* The Request id for easy trace | ||
* | ||
* @var int $requestId; | ||
*/ | ||
public int $requestId; | ||
|
||
/** | ||
* Creates an object with all the fields required for logging | ||
* | ||
* @param null|string $startTime (Optional) Parameter to calculate the latency | ||
*/ | ||
public function __construct(null|string $startTime = null) | ||
{ | ||
$this->timestamp = date(DATE_RFC3339); | ||
|
||
if ($startTime) { | ||
// We should check for false in here | ||
$this->latency = (int)strtotime($this->timestamp) - (int)strtotime($startTime); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit
I've added a CS rule for this in GoogleCloudPlatform/php-tools#149