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