Skip to content

Logging improvements #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 74 additions & 8 deletions Model/Cache/InvalidateLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,93 @@

namespace Magento\CloudComponents\Model\Cache;

use Magento\Framework\App\Request\Http as HttpRequest;
use Psr\Log\LoggerInterface as Logger;
use Magento\CloudComponents\Model\DebugTrace;

/**
* Log cache invalidation to a file
*/
class InvalidateLogger extends \Magento\Framework\Cache\InvalidateLogger
{
/**
* @var string[]
*/
private $tagsToLog = [
'cat_p',
'cat_c',
'PRODUCT_PRICE',
'cms_b',
'cms_p',
'config_scopes',
'eav',
'eav_attribute',
'fpc',
'review_block',
'SEARCH_QUERY',
'search_query',
'store_group',
'store',
'store_relations',
'website',
'CORE_DESIGN',
'core_design',
'WEBSERVICE',
'webservice',
'banner',
'catalog_event',
'config',
'block_html',
'COLLECTION_DATA',
'collection_data',
'collections',
'layout_general_cache_tag',
'layout',
'compiled_config',
'acl_cache',
'reflection',
'db_ddl',
'all'
];

/**
* @var DebugTrace
*/
private $debugTrace;

/**
* @param HttpRequest $request
* @param Logger $logger
* @param DebugTrace $debugTrace
*/
public function __construct(
HttpRequest $request,
Logger $logger,
DebugTrace $debugTrace
) {
parent::__construct($request, $logger);
$this->debugTrace = $debugTrace;
}

/**
* Log cache invalidation to a file
*
* @param mixed $invalidateInfo
*/
public function execute($invalidateInfo)
{
if (is_array($invalidateInfo)) {
$invalidateInfo['trace'] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
} elseif (is_string($invalidateInfo)) {
$invalidateInfo = [
'main' => $invalidateInfo,
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
];
}
$needTrace = false;
if (is_array($invalidateInfo) && isset($invalidateInfo['tags'])) {
foreach ($invalidateInfo['tags'] as $tag) {
if (in_array(strtolower($tag), $this->tagsToLog)) {
$needTrace = true;
}
}

if ($needTrace) {
$invalidateInfo['trace'] = $this->debugTrace->getTrace();
}
}
parent::execute($invalidateInfo);
}
}
87 changes: 87 additions & 0 deletions Model/DebugTrace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\CloudComponents\Model;

/**
* Class to get compressed debug back trace
*/
class DebugTrace
{
/**
* List of useless classes
*
* @var string[]
*/
private $notAllowedClasses = [
'Symfony\Component\Console\Application',
'Magento\Framework\Console\Cli',
'Symfony\Component\Console\Command\Command',
'Magento\Staging\Model\Event\Manager\Proxy',
'Magento\Staging\Model\Event\Manager',
'Magento\Framework\Event\Invoker\InvokerDefault',
'Magento\CloudComponents\Model\Observer\CacheFlushAll',
'Magento\CloudComponents\Model\Cache\InvalidateLogger',
'Magento\CloudComponents\Model\Cache\InvalidateLogger',
'Magento\CloudComponents\Model\Indexation\Logger',
'Magento\Framework\Cache\Frontend\Decorator\Logger',
'Magento\Framework\Cache\Frontend\Decorator\Bare',
'Magento\Framework\App\Cache\Type\AccessProxy',
'Magento\Framework\Cache\Frontend\Decorator\TagScope',
'Magento\Framework\ObjectManager\ObjectManager',
'Magento\Framework\ObjectManager\Config\Compiled',
'Magento\Framework\ObjectManager\Config\Config',
'Magento\Framework\ObjectManager\Factory\Dynamic\Developer',
'Magento\Framework\ObjectManager\Factory\Dynamic\Production'
];

/**
* List of useless functions
*
* @var string[]
*/
private $notAllowedFunctions = [
'___callPlugins',
'___callParent',
'Magento\Framework\Interception\{closure}'
];

/**
* Returns debug back trace
*
* @return array
*/
public function getTrace()
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
foreach ($trace as $index => $line) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add validation if elements of array $line is set.

$line['function']
$line['class']
$line['file']

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I reproduce case when the file key undefined?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

if (!isset($line['function'], $line['class'], $line['file'])) {
continue;
}
if (in_array($line['function'], $this->notAllowedFunctions)
|| in_array($line['class'], $this->notAllowedClasses)
|| strpos($line['file'], 'Interceptor.php') !== false
) {
unset($trace[$index]);
}
unset($trace[$index]['type']);
}

if (function_exists('gzcompress')) {
return bin2hex(
gzcompress(
print_r(
$trace,
true
)
)
);
}
return $trace;
}
}
16 changes: 13 additions & 3 deletions Model/Indexation/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Magento\CloudComponents\Model\Indexation;

use Magento\CloudComponents\Model\DebugTrace;
use Magento\Framework\Indexer\ActionInterface;
use Psr\Log\LoggerInterface;

Expand All @@ -21,12 +22,21 @@ class Logger
*/
private $logger;

/**
* @var DebugTrace
*/
private $debugTrace;

/**
* @param LoggerInterface $logger
* @param DebugTrace $debugTrace
*/
public function __construct(LoggerInterface $logger)
{
public function __construct(
LoggerInterface $logger,
DebugTrace $debugTrace
) {
$this->logger = $logger;
$this->debugTrace = $debugTrace;
}

/**
Expand All @@ -39,7 +49,7 @@ public function afterExecuteFull(ActionInterface $subject)
$this->logger->debug(
'full_indexation: ' . get_class($subject),
[
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
'trace' => $this->debugTrace->getTrace()
]
);
}
Expand Down