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.
// - %T The request duration, in microseconds.
// - %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 For conduit, the Conduit method which was invoked.
//

View file

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

View file

@ -23,7 +23,8 @@ final class PhabricatorAccessLogConfigOptions
'r' => pht("The remote IP."),
'T' => pht("The request duration, in microseconds."),
'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("For conduit, the Conduit method which was invoked."),
);

View file

@ -19,9 +19,9 @@ final class PhabricatorAccessLog {
$format,
"[%D]\t%p\t%h\t%r\t%u\t%C\t%m\t%U\t%R\t%c\t%T");
if (!$path) {
return null;
}
// NOTE: Path may be null. We still create the log, it just won't write
// anywhere.
$path = null;
$log = new PhutilDeferredLog($path, $format);
$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
*/
@ -183,7 +191,6 @@ final class PhabricatorStartup {
self::endOutputCapture();
$access_log = self::getGlobal('log.access');
if ($access_log) {
try {
$access_log->setData(
array(
@ -193,7 +200,6 @@ final class PhabricatorStartup {
} catch (Exception $ex) {
$message .= "\n(Moreover, unable to write to access log.)";
}
}
header(
'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.
PhabricatorAccessLog::init();
$access_log = PhabricatorAccessLog::getLog();
if ($access_log) {
PhabricatorStartup::setGlobal('log.access', $access_log);
$access_log->setData(
array(
@ -19,7 +18,6 @@ try {
'r' => idx($_SERVER, 'REMOTE_ADDR', '-'),
'M' => idx($_SERVER, 'REQUEST_METHOD', '-'),
));
}
DarkConsoleXHProfPluginAPI::hookProfiler();
@ -62,13 +60,11 @@ try {
$application->setRequest($request);
list($controller, $uri_data) = $application->buildController();
if ($access_log) {
$access_log->setData(
array(
'U' => (string)$request->getRequestURI()->getPath(),
'C' => get_class($controller),
));
}
// 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
@ -77,14 +73,13 @@ try {
try {
$response = $controller->willBeginExecution();
if ($access_log) {
if ($request->getUser() && $request->getUser()->getPHID()) {
$access_log->setData(
array(
'u' => $request->getUser()->getUserName(),
'P' => $request->getUser()->getPHID(),
));
}
}
if (!$response) {
$controller->willProcessRequest($uri_data);
@ -120,9 +115,7 @@ try {
$sink->writeResponse($response);
} catch (Exception $ex) {
$write_guard->dispose();
if ($access_log) {
$access_log->write();
}
if ($original_exception) {
$ex = new PhutilAggregateException(
"Multiple exceptions during processing and rendering.",
@ -136,17 +129,13 @@ try {
$write_guard->dispose();
if ($access_log) {
$request_start = PhabricatorStartup::getStartTime();
$access_log->setData(
array(
'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) {
PhabricatorStartup::didFatal("[Exception] ".$ex->getMessage());