Skip to content

Commit

Permalink
Generate HttpRequestContext for log context.
Browse files Browse the repository at this point in the history
  • Loading branch information
westy92 committed Nov 28, 2022
1 parent d416c68 commit 5c96744
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
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;
}
}
49 changes: 49 additions & 0 deletions ErrorReporting/tests/Unit/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,55 @@ public function testExceptionHandler($exception)
Bootstrap::exceptionHandler($exception);
}

/**
* @dataProvider exceptionProvider
*/
public function testExceptionHandlerWithHttpContext($exception)
{
try {
$oldServer = $_SERVER;
$_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);
} finally {
// restore
$_SERVER = $oldServer;
}
}

/**
* @dataProvider exceptionProvider
*/
Expand Down

0 comments on commit 5c96744

Please sign in to comment.