1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-22 06:42:42 +01:00

Guarantee the existence of the Phabricator access log

Summary:
We have a fair number of conditionals on the existence of the access log. Instead, always build it and just don't write it if the user doesn't want a version on disk.

Also, formalize logged-in user PHID (avoids object existence juggling) in the access log and move microseconds-since-startup to PhabricatorStartup (simplifies index.php).

Depends on D5532. Fixes T2860. Ref T2870.

Test Plan: Disabled access log, verified XHProf writes occurred correctly.

Reviewers: btrahan, chad

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T2860, T2870

Differential Revision: https://secure.phabricator.com/D5533
This commit is contained in:
epriestley 2013-04-02 09:53:56 -07:00
parent 0f9bfa3bfd
commit cde1416446
6 changed files with 56 additions and 64 deletions

View file

@ -113,7 +113,8 @@ return array(
// - %r The remote IP. // - %r The remote IP.
// - %T The request duration, in microseconds. // - %T The request duration, in microseconds.
// - %U The request path. // - %U The request path.
// - %u The logged-in user, if one is logged in. // - %u The logged-in username, if one is logged in.
// - %P The logged-in user PHID, if one is logged in.
// - %M The HTTP method. // - %M The HTTP method.
// - %m For conduit, the Conduit method which was invoked. // - %m For conduit, the Conduit method which was invoked.
// //

View file

@ -68,9 +68,7 @@ final class DarkConsoleXHProfPluginAPI {
} }
public static function saveProfilerSample( public static function saveProfilerSample(PhutilDeferredLog $access_log) {
AphrontRequest $request,
$access_log) {
if (!self::isProfilerStarted()) { if (!self::isProfilerStarted()) {
return; return;
@ -86,16 +84,13 @@ final class DarkConsoleXHProfPluginAPI {
$sample_rate = PhabricatorEnv::getEnvConfig('debug.profile-rate'); $sample_rate = PhabricatorEnv::getEnvConfig('debug.profile-rate');
} }
$profile_sample->setSampleRate($sample_rate);
if ($access_log) {
$profile_sample $profile_sample
->setSampleRate($sample_rate)
->setUsTotal($access_log->getData('T')) ->setUsTotal($access_log->getData('T'))
->setHostname($access_log->getData('h')) ->setHostname($access_log->getData('h'))
->setRequestPath($access_log->getData('U')) ->setRequestPath($access_log->getData('U'))
->setController($access_log->getData('C')) ->setController($access_log->getData('C'))
->setUserPHID($request->getUser()->getPHID()); ->setUserPHID($access_log->getData('P'));
}
$profile_sample->save(); $profile_sample->save();
} }

View file

@ -23,7 +23,8 @@ final class PhabricatorAccessLogConfigOptions
'r' => pht("The remote IP."), 'r' => pht("The remote IP."),
'T' => pht("The request duration, in microseconds."), 'T' => pht("The request duration, in microseconds."),
'U' => pht("The request path."), 'U' => pht("The request path."),
'u' => pht("The logged-in user, if one is logged in."), 'u' => pht("The logged-in username, if one is logged in."),
'P' => pht("The logged-in user PHID, if one is logged in."),
'M' => pht("The HTTP method."), 'M' => pht("The HTTP method."),
'm' => pht("For conduit, the Conduit method which was invoked."), 'm' => pht("For conduit, the Conduit method which was invoked."),
); );

View file

@ -19,9 +19,9 @@ final class PhabricatorAccessLog {
$format, $format,
"[%D]\t%p\t%h\t%r\t%u\t%C\t%m\t%U\t%R\t%c\t%T"); "[%D]\t%p\t%h\t%r\t%u\t%C\t%m\t%U\t%R\t%c\t%T");
if (!$path) { // NOTE: Path may be null. We still create the log, it just won't write
return null; // anywhere.
} $path = null;
$log = new PhutilDeferredLog($path, $format); $log = new PhutilDeferredLog($path, $format);
$log->setData( $log->setData(

View file

@ -31,6 +31,14 @@ final class PhabricatorStartup {
} }
/**
* @task info
*/
public static function getMicrosecondsSinceStart() {
return (int)(1000000 * (microtime(true) - self::getStartTime()));
}
/** /**
* @task info * @task info
*/ */
@ -183,7 +191,6 @@ final class PhabricatorStartup {
self::endOutputCapture(); self::endOutputCapture();
$access_log = self::getGlobal('log.access'); $access_log = self::getGlobal('log.access');
if ($access_log) {
try { try {
$access_log->setData( $access_log->setData(
array( array(
@ -193,7 +200,6 @@ final class PhabricatorStartup {
} catch (Exception $ex) { } catch (Exception $ex) {
$message .= "\n(Moreover, unable to write to access log.)"; $message .= "\n(Moreover, unable to write to access log.)";
} }
}
header( header(
'Content-Type: text/plain; charset=utf-8', 'Content-Type: text/plain; charset=utf-8',

View file

@ -11,7 +11,6 @@ try {
// This is the earliest we can get away with this, we need env config first. // This is the earliest we can get away with this, we need env config first.
PhabricatorAccessLog::init(); PhabricatorAccessLog::init();
$access_log = PhabricatorAccessLog::getLog(); $access_log = PhabricatorAccessLog::getLog();
if ($access_log) {
PhabricatorStartup::setGlobal('log.access', $access_log); PhabricatorStartup::setGlobal('log.access', $access_log);
$access_log->setData( $access_log->setData(
array( array(
@ -19,7 +18,6 @@ try {
'r' => idx($_SERVER, 'REMOTE_ADDR', '-'), 'r' => idx($_SERVER, 'REMOTE_ADDR', '-'),
'M' => idx($_SERVER, 'REQUEST_METHOD', '-'), 'M' => idx($_SERVER, 'REQUEST_METHOD', '-'),
)); ));
}
DarkConsoleXHProfPluginAPI::hookProfiler(); DarkConsoleXHProfPluginAPI::hookProfiler();
@ -62,13 +60,11 @@ try {
$application->setRequest($request); $application->setRequest($request);
list($controller, $uri_data) = $application->buildController(); list($controller, $uri_data) = $application->buildController();
if ($access_log) {
$access_log->setData( $access_log->setData(
array( array(
'U' => (string)$request->getRequestURI()->getPath(), 'U' => (string)$request->getRequestURI()->getPath(),
'C' => get_class($controller), 'C' => get_class($controller),
)); ));
}
// If execution throws an exception and then trying to render that exception // If execution throws an exception and then trying to render that exception
// throws another exception, we want to show the original exception, as it is // throws another exception, we want to show the original exception, as it is
@ -77,14 +73,13 @@ try {
try { try {
$response = $controller->willBeginExecution(); $response = $controller->willBeginExecution();
if ($access_log) {
if ($request->getUser() && $request->getUser()->getPHID()) { if ($request->getUser() && $request->getUser()->getPHID()) {
$access_log->setData( $access_log->setData(
array( array(
'u' => $request->getUser()->getUserName(), 'u' => $request->getUser()->getUserName(),
'P' => $request->getUser()->getPHID(),
)); ));
} }
}
if (!$response) { if (!$response) {
$controller->willProcessRequest($uri_data); $controller->willProcessRequest($uri_data);
@ -120,9 +115,7 @@ try {
$sink->writeResponse($response); $sink->writeResponse($response);
} catch (Exception $ex) { } catch (Exception $ex) {
$write_guard->dispose(); $write_guard->dispose();
if ($access_log) {
$access_log->write(); $access_log->write();
}
if ($original_exception) { if ($original_exception) {
$ex = new PhutilAggregateException( $ex = new PhutilAggregateException(
"Multiple exceptions during processing and rendering.", "Multiple exceptions during processing and rendering.",
@ -136,17 +129,13 @@ try {
$write_guard->dispose(); $write_guard->dispose();
if ($access_log) {
$request_start = PhabricatorStartup::getStartTime();
$access_log->setData( $access_log->setData(
array( array(
'c' => $response->getHTTPResponseCode(), 'c' => $response->getHTTPResponseCode(),
'T' => (int)(1000000 * (microtime(true) - $request_start)), 'T' => PhabricatorStartup::getMicrosecondsSinceStart(),
)); ));
$access_log->write();
}
DarkConsoleXHProfPluginAPI::saveProfilerSample($request, $access_log); DarkConsoleXHProfPluginAPI::saveProfilerSample($access_log);
} catch (Exception $ex) { } catch (Exception $ex) {
PhabricatorStartup::didFatal("[Exception] ".$ex->getMessage()); PhabricatorStartup::didFatal("[Exception] ".$ex->getMessage());