Skip to content
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

feat(ErrorReporting): Generate HttpRequestContext for log context. #5656

Merged
merged 3 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions ErrorReporting/src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static function exceptionHandler($ex)
if (self::$psrLogger) {
$service = self::$psrLogger->getMetadataProvider()->serviceId();
$version = self::$psrLogger->getMetadataProvider()->versionId();
self::$psrLogger->error($message, [
$context = [
'context' => [
'reportLocation' => [
'filePath' => $ex->getFile(),
Expand All @@ -142,7 +142,12 @@ public static function exceptionHandler($ex)
'service' => $service,
'version' => $version,
]
]);
];
$httpRequest = self::getHttpRequest();
if (!empty($httpRequest)) {
$context['context']['httpRequest'] = self::getHttpRequest();
}
self::$psrLogger->error($message, $context);
} else {
$stderr = defined('STDERR') ? STDERR : fopen('php://stderr', 'w');
fwrite($stderr, $message . PHP_EOL);
Expand Down Expand Up @@ -186,6 +191,10 @@ public static function errorHandler($level, $message, $file, $line)
'version' => $version
]
];
$httpRequest = self::getHttpRequest();
if (!empty($httpRequest)) {
$context['context']['httpRequest'] = self::getHttpRequest();
}
self::$psrLogger->log(
self::getErrorLevelString($level),
$message,
Expand Down Expand Up @@ -232,6 +241,10 @@ public static function shutdownHandler()
'version' => $version
]
];
$httpRequest = self::getHttpRequest();
if (!empty($httpRequest)) {
$context['context']['httpRequest'] = self::getHttpRequest();
}
if (self::$psrLogger) {
self::$psrLogger->log(
self::getErrorLevelString($err['type']),
Expand Down Expand Up @@ -268,4 +281,36 @@ private static function getFunctionNameForReport(array $trace = null)
}
return implode('', array_reverse($functionName));
}

/**
* Builds an HttpRequestContext from available server information.
*
* @return array An HttpRequestContext.
*/
private static function getHttpRequest()
{
$httpRequest = [];
if (isset($_SERVER)) {
if (isset($_SERVER['REQUEST_METHOD'])) {
$httpRequest['method'] = $_SERVER['REQUEST_METHOD'];
}
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$httpRequest['userAgent'] = $_SERVER['HTTP_USER_AGENT'];
}
if (isset($_SERVER['HTTP_REFERER'])) {
$httpRequest['referrer'] = $_SERVER['HTTP_REFERER'];
}
if (isset($_SERVER['REMOTE_ADDR'])) {
$httpRequest['remoteIp'] = $_SERVER['REMOTE_ADDR'];
}
if (isset($_SERVER['HTTP_HOST']) && isset($_SERVER['REQUEST_URI'])) {
$httpRequest['url'] = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http')
. '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
if (http_response_code() !== false) {
$httpRequest['responseStatusCode'] = http_response_code();
}
}
return $httpRequest;
}
}
44 changes: 44 additions & 0 deletions ErrorReporting/tests/Unit/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,50 @@ public function testExceptionHandler($exception)
Bootstrap::exceptionHandler($exception);
}

/**
* @dataProvider exceptionProvider
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
* @runInSeparateProcess
*/
public function testExceptionHandlerWithHttpContext($exception)
{
$_SERVER = [
'REQUEST_METHOD' => 'GET',
'HTTP_REFERER' => 'ref',
'HTTP_HOST' => 'google.com',
'REQUEST_URI' => '/index.php',
'REMOTE_ADDR' => '1.2.3.4',
'HTTP_USER_AGENT' => 'UA',
];
$expectedMessage = sprintf('PHP Notice: %s', (string)$exception);
$expectedContext = [
'context' => [
'reportLocation' => [
'filePath' => $exception->getFile(),
'lineNumber' => $exception->getLine(),
'functionName' => 'Google\Cloud\ErrorReporting\Tests\Unit\BootstrapTest->exceptionProvider',
],
'httpRequest' => [
'method' => 'GET',
'url' => 'http://google.com/index.php',
'referrer' => 'ref',
'remoteIp' => '1.2.3.4',
'userAgent' => 'UA',
]
],
'serviceContext' => [
'service' => '',
'version' => ''
]
];
$this->psrBatchLogger->error($expectedMessage, $expectedContext)
->shouldBeCalledTimes(1);
$this->psrBatchLogger->getMetadataProvider()
->willReturn(new SimpleMetadataProvider())
->shouldBeCalledTimes(2);
Bootstrap::$psrLogger = $this->psrBatchLogger->reveal();
Bootstrap::exceptionHandler($exception);
}

/**
* @dataProvider exceptionProvider
*/
Expand Down