1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lamoda\Metric\MetricBundle\Command; |
4
|
|
|
|
5
|
|
|
use Lamoda\Metric\Collector\CollectorRegistry; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Helper\Table; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
12
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
13
|
|
|
|
14
|
|
|
final class DebugMetricsCommand extends Command |
15
|
|
|
{ |
16
|
|
|
protected static $defaultName = 'metrics:debug'; |
17
|
|
|
|
18
|
|
|
/** @var CollectorRegistry */ |
19
|
|
|
private $registry; |
20
|
|
|
|
21
|
|
|
public function __construct(CollectorRegistry $collectorRegistry) |
22
|
|
|
{ |
23
|
|
|
parent::__construct(); |
24
|
|
|
$this->registry = $collectorRegistry; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function configure() |
28
|
|
|
{ |
29
|
|
|
$this->addArgument('collector', InputArgument::REQUIRED, 'Collector name from configuration'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
33
|
|
|
{ |
34
|
|
|
$collector = $input->getArgument('collector'); |
35
|
|
|
$io = new SymfonyStyle($input, $output); |
36
|
|
|
|
37
|
|
|
$stopwatch = new Stopwatch(true); |
38
|
|
|
|
39
|
|
|
$source = $this->registry->getCollector($collector); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$table = new Table($io); |
42
|
|
|
$table->setHeaders(['name', 'value', 'tags', 'resolution time (ms)', 'resolution memory (Mb)']); |
43
|
|
|
|
44
|
|
|
foreach ($source->collect()->getMetrics() as $metric) { |
45
|
|
|
$stopwatch->start($metric->getName()); |
46
|
|
|
|
47
|
|
|
$profile = $stopwatch->stop($metric->getName()); |
48
|
|
|
$table->addRow( |
49
|
|
|
[ |
50
|
|
|
$metric->getName(), |
51
|
|
|
$metric->resolve(), |
52
|
|
|
$this->formatTags($metric->getTags()), |
53
|
|
|
$profile->getDuration(), |
54
|
|
|
($profile->getMemory() / 1024 / 1024), |
55
|
|
|
] |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
$table->render(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function formatTags(array $tags): string |
62
|
|
|
{ |
63
|
|
|
$parts = array_map( |
64
|
|
|
function (string $val, string $key) { |
65
|
|
|
return "$key:$val"; |
66
|
|
|
}, |
67
|
|
|
array_values($tags), |
68
|
|
|
array_keys($tags) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
return implode(', ', $parts); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|