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