Logger.php
120 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | |
| 9 | namespace Piwik\CronArchive\Performance; |
| 10 | |
| 11 | use Piwik\ArchiveProcessor; |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Config; |
| 14 | use Piwik\Option; |
| 15 | use Piwik\Timer; |
| 16 | use Piwik\Url; |
| 17 | use Psr\Log\LoggerInterface; |
| 18 | |
| 19 | class Logger |
| 20 | { |
| 21 | /** |
| 22 | * @var int |
| 23 | */ |
| 24 | private $isEnabled; |
| 25 | |
| 26 | /** |
| 27 | * @var LoggerInterface |
| 28 | */ |
| 29 | private $logger; |
| 30 | |
| 31 | /** |
| 32 | * @var int |
| 33 | */ |
| 34 | private $archivingRunId; |
| 35 | |
| 36 | public function __construct(Config $config, LoggerInterface $logger = null) |
| 37 | { |
| 38 | $this->isEnabled = $config->Debug['archiving_profile'] == 1; |
| 39 | $this->logger = $logger; |
| 40 | |
| 41 | $this->archivingRunId = $this->getArchivingRunId(); |
| 42 | if (empty($this->archivingRunId)) { |
| 43 | $this->isEnabled = false; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public function logMeasurement($category, $name, ArchiveProcessor\Parameters $activeArchivingParams, Timer $timer) |
| 48 | { |
| 49 | if (!$this->isEnabled || !$this->logger) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $measurement = new Measurement($category, $name, $activeArchivingParams->getSite()->getId(), |
| 54 | $activeArchivingParams->getPeriod()->getRangeString(), $activeArchivingParams->getPeriod()->getLabel(), |
| 55 | $activeArchivingParams->getSegment()->getString(), $timer->getTime(), $timer->getMemoryLeakValue(), |
| 56 | $timer->getPeakMemoryValue()); |
| 57 | |
| 58 | $params = array_merge($_GET); |
| 59 | unset($params['pid']); |
| 60 | unset($params['runid']); |
| 61 | |
| 62 | $this->logger->info("[runid={runid},pid={pid}] {request}: {measurement}", [ |
| 63 | 'pid' => Common::getRequestVar('pid', false), |
| 64 | 'runid' => $this->getArchivingRunId(), |
| 65 | 'request' => Url::getQueryStringFromParameters($params), |
| 66 | 'measurement' => $measurement, |
| 67 | ]); |
| 68 | } |
| 69 | |
| 70 | public static function getMeasurementsFor($runId, $childPid) |
| 71 | { |
| 72 | $profilingLogFile = preg_replace('/[\'"]/', '', Config::getInstance()->Debug['archive_profiling_log']); |
| 73 | if (!is_readable($profilingLogFile)) { |
| 74 | return []; |
| 75 | } |
| 76 | |
| 77 | $runId = self::cleanId($runId); |
| 78 | $childPid = self::cleanId($childPid); |
| 79 | |
| 80 | $lineIdentifier = "[runid=$runId,pid=$childPid]"; |
| 81 | $lines = `grep "$childPid" "$profilingLogFile"`; |
| 82 | $lines = explode("\n", $lines); |
| 83 | $lines = array_map(function ($line) use ($lineIdentifier) { |
| 84 | $index = strpos($line, $lineIdentifier); |
| 85 | if ($index === false) { |
| 86 | return null; |
| 87 | } |
| 88 | $line = substr($line, $index + strlen($lineIdentifier)); |
| 89 | return trim($line); |
| 90 | }, $lines); |
| 91 | $lines = array_filter($lines); |
| 92 | $lines = array_map(function ($line) { |
| 93 | $parts = explode(":", $line, 2); |
| 94 | $parts = array_map('trim', $parts); |
| 95 | return $parts; |
| 96 | }, $lines); |
| 97 | |
| 98 | $data = []; |
| 99 | foreach ($lines as $line) { |
| 100 | if (count($line) != 2) { |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | list($request, $measurement) = $line; |
| 105 | $data[$request][] = $measurement; |
| 106 | } |
| 107 | return $data; |
| 108 | } |
| 109 | |
| 110 | private function getArchivingRunId() |
| 111 | { |
| 112 | return Common::getRequestVar('runid', false); |
| 113 | } |
| 114 | |
| 115 | private static function cleanId($id) |
| 116 | { |
| 117 | return preg_replace('/[^a-zA-Z0-9_-]/', '', $id); |
| 118 | } |
| 119 | } |
| 120 |