This repository was archived by the owner on Apr 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathMetricsCommandBase.php
More file actions
473 lines (401 loc) · 18.3 KB
/
Copy pathMetricsCommandBase.php
File metadata and controls
473 lines (401 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<?php
namespace Platformsh\Cli\Command\Metrics;
use Platformsh\Cli\Model\Metrics\Field;
use Platformsh\Cli\Model\Metrics\Format;
use Platformsh\Cli\Model\Metrics\SourceField;
use Platformsh\Cli\Model\Metrics\SourceFieldPercentage;
use Platformsh\Cli\Service\Io;
use Platformsh\Cli\Service\PropertyFormatter;
use Platformsh\Cli\Service\Config;
use Platformsh\Cli\Service\Api;
use GuzzleHttp\Exception\BadResponseException;
use Khill\Duration\Duration;
use Platformsh\Cli\Command\CommandBase;
use Platformsh\Cli\Console\AdaptiveTableCell;
use Platformsh\Cli\Console\ArrayArgument;
use Platformsh\Cli\Model\Metrics\Query;
use Platformsh\Cli\Model\Metrics\TimeSpec;
use Platformsh\Cli\Util\Wildcard;
use Platformsh\Client\Exception\ApiResponseException;
use Platformsh\Client\Model\Environment;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
abstract class MetricsCommandBase extends CommandBase
{
const MIN_INTERVAL = 60; // 1 minute
const MIN_RANGE = 300; // 5 minutes
const DEFAULT_RANGE = 600;
/**
* @var bool whether services have been identified that use high memory
*/
private $foundHighMemoryServices = false;
public function isEnabled()
{
if (!$this->config()->getWithDefault('api.metrics', false)) {
return false;
}
return parent::isEnabled();
}
protected function addMetricsOptions()
{
$duration = new Duration();
$this->addOption('range', 'r', InputOption::VALUE_REQUIRED,
'The time range. Metrics will be loaded for this duration until the end time (--to).'
. "\n" . 'You can specify units: hours (h), minutes (m), or seconds (s).'
. "\n" . \sprintf(
'Minimum <comment>%s</comment>, maximum <comment>8h</comment> or more (depending on the project), default <comment>%s</comment>.',
$duration->humanize(self::MIN_RANGE),
$duration->humanize(self::DEFAULT_RANGE)
)
);
// The $default is left at null so the lack of input can be detected.
$this->addOption('interval', 'i', InputOption::VALUE_REQUIRED,
'The time interval. Defaults to a division of the range.'
. "\n" . 'You can specify units: hours (h), minutes (m), or seconds (s).'
. "\n" . \sprintf('Minimum <comment>%s</comment>.', $duration->humanize(self::MIN_INTERVAL))
);
$this->addOption('to', null, InputOption::VALUE_REQUIRED, 'The end time. Defaults to now.');
$this->addOption('latest', '1', InputOption::VALUE_NONE, 'Show only the latest single data point');
$this->addOption('service', 's', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Filter by service or application name' . "\n" . Wildcard::HELP);
$this->addOption('type', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Filter by service type (if --service is not provided). The version is not required.' . "\n" . Wildcard::HELP);
return $this;
}
/**
* Returns the resources overview URL for the selected environment.
*
* @param Environment $environment
* @return string|false The resources overview URL, or false if not available
* @throws \GuzzleHttp\Exception\GuzzleException if there is an error in fetching observability metadata
*/
private function getResourcesOverviewUrl(Environment $environment)
{
$entrypointUrl = rtrim($environment->getUri(), '/') . '/observability/';
$client = $this->api()->getHttpClient();
$request = $client->createRequest('GET', $entrypointUrl);
try {
$response = $client->send($request);
} catch (BadResponseException $e) {
return false;
}
$data = json_decode($response->getBody()->__toString(), true);
if (empty($data['_links']['resources_overview']['href'])) {
return false;
}
return $data['_links']['resources_overview']['href'];
}
/**
* @param InputInterface $input
* @param array $metricTypes
* @param array $metricAggs
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function processQuery(InputInterface $input, array $metricTypes, array $metricAggs)
{
// Common
$this->chooseEnvFilter = $this->filterEnvsMaybeActive();
$this->validateInput($input, false, true);
$environment = $this->getSelectedEnvironment();
// Common
$timeSpec = $this->validateTimeInput($input);
if (false === $timeSpec) {
throw new \InvalidArgumentException('Invalid time input. Please check the --range, --to, and --interval options.');
}
if (!$link = $this->getResourcesOverviewUrl($environment)) {
throw new \InvalidArgumentException('Observability API link not found for the environment.');
}
$query = Query::fromTimeSpec($timeSpec);
$metricsQueryUrl = $link;
$selectedServiceNames = $this->getServices($input, $environment);
if (!empty($selectedServiceNames)) {
$this->debug('Selected service(s): ' . implode(', ', $selectedServiceNames));
$query->setServices($selectedServiceNames);
}
$this->debug('Selected type(s): ' . implode(', ', $metricTypes));
$query->setTypes($metricTypes);
$this->debug('Selected agg(s): ' . implode(', ', $metricAggs));
$query->setAggs($metricAggs);
if ($this->stdErr->isDebug()) {
$this->debug('Metrics query: ' . json_encode($query->asArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
}
// Perform the metrics query.
$client = $this->api()->getHttpClient();
$request = $client->createRequest('GET', $metricsQueryUrl . $query->asString());
try {
$result = $client->send($request);
} catch (BadResponseException $e) {
throw ApiResponseException::create($request, $e->getResponse(), $e);
}
// Decode the response.
$content = $result->getBody()->__toString();
$items = json_decode($content, true);
if (empty($items)) {
$this->stdErr->writeln('No data points found.');
throw new \RuntimeException('No data points were found in the metrics response.');
}
// Filter to only the latest timestamp if --latest is given.
if ($input->getOption('latest')) {
foreach (array_reverse($items['data']) as $item) {
if (isset($item['services'])) {
$items['data'] = [$item];
break;
}
}
}
// It's possible that there is nothing to display, e.g. if the router
// has been filtered out and no metrics were available for the other
// services, perhaps because the environment was paused.
if (empty($items['data'])) {
$this->stdErr->writeln('No values were found to display.');
if ('paused' === $environment->status) {
$this->stdErr->writeln('');
$this->stdErr->writeln('The environment is currently paused.');
$this->stdErr->writeln('Metrics collection will start when the environment is redeployed.');
}
throw new \RuntimeException('No data points were found in the metrics response.');
}
return [$items, $environment];
}
/**
* @param InputInterface $input
* @param Environment $environment
* @return array
*/
private function getServices(InputInterface $input, Environment $environment)
{
// Select services based on the --service or --type options.
$deployment = $this->api()->getCurrentDeployment($environment);
$allServices = array_merge($deployment->webapps, $deployment->services, $deployment->workers);
$servicesInput = ArrayArgument::getOption($input, 'service');
$selectedServiceNames = [];
if (!empty($servicesInput)) {
$selectedServiceNames = Wildcard::select(array_merge(array_keys($allServices), ['router']), $servicesInput);
if (!$selectedServiceNames) {
$this->stdErr->writeln('No services were found matching the name(s): <error>' . implode(', ', $servicesInput) . '</error>');
throw new \RuntimeException('No services were found matching the name(s): ' . implode(', ', $servicesInput));
}
} elseif ($typeInput = ArrayArgument::getOption($input, 'type')) {
$byType = [];
foreach ($allServices as $name => $service) {
$type = $service->type;
list($prefix) = explode(':', $service->type, 2);
$byType[$type][] = $name;
$byType[$prefix][] = $name;
}
$selectedKeys = Wildcard::select(array_merge(array_keys($byType), ['router']), $typeInput);
if (!$selectedKeys) {
$this->stdErr->writeln('No services were found matching the type(s): <error>' . implode(', ', $typeInput) . '</error>');
throw new \RuntimeException('No services were found matching the type(s): ' . implode(', ', $typeInput));
}
foreach ($selectedKeys as $selectedKey) {
$selectedServiceNames = array_merge($selectedServiceNames, $byType[$selectedKey]);
}
$selectedServiceNames = array_unique($selectedServiceNames);
}
return $selectedServiceNames;
}
/**
* Validates the interval and range input, and finds defaults.
*
* Sets the startTime, endTime, and interval properties.
*
* @param InputInterface $input
* @return TimeSpec|false
*
* @see self::startTime, self::$endTime, self::$interval
*/
protected function validateTimeInput(InputInterface $input)
{
if ($to = $input->getOption('to')) {
$endTime = \strtotime($to);
if (!$endTime) {
$this->stdErr->writeln('Failed to parse --to time: ' . $to);
return false;
}
} else {
$endTime = time();
}
if ($rangeStr = $input->getOption('range')) {
$rangeSeconds = (new Duration())->toSeconds($rangeStr);
if (empty($rangeSeconds)) {
$this->stdErr->writeln('Invalid --range: <error>' . $rangeStr . '</error>');
return false;
} elseif ($rangeSeconds < self::MIN_RANGE) {
$this->stdErr->writeln(\sprintf('The --range <error>%s</error> is too short: it must be at least %d seconds (%s).', $rangeStr, self::MIN_RANGE, (new Duration())->humanize(self::MIN_RANGE)));
return false;
}
$rangeSeconds = (int) $rangeSeconds;
} else {
$rangeSeconds = self::DEFAULT_RANGE;
}
$startTime = $endTime - $rangeSeconds;
$interval = null;
if ($intervalString = $input->getOption('interval')) {
$interval = (int) (new Duration())->toSeconds($intervalString);
if (empty($interval)) {
$this->stdErr->writeln('Invalid --range: <error>' . $intervalString . '</error>');
return false;
}
if ($interval > $endTime - $startTime) {
$this->stdErr->writeln(\sprintf('The --interval <error>%s</error> is invalid. It cannot be greater than the selected time range', $intervalString));
return false;
}
}
return new TimeSpec($startTime, $endTime, $interval);
}
/**
* @param array $values
* @param array $fieldMapping
* @param Environment $environment
* @return array
* @throws \Exception
*/
protected function buildRows(array $values, array $fieldMapping, Environment $environment)
{
/** @var \Platformsh\Cli\Service\PropertyFormatter $formatter */
$formatter = $this->getService('property_formatter');
$sortServices = $this->getSortedServices($environment);
$serviceTypes = [];
$rows = [];
$lastCountPerTimestamp = 0;
foreach ($values['data'] as $point) {
$timestamp = $point['timestamp'];
if (!isset($point['services'])) {
continue;
}
$byService = $point['services'];
// Add a separator if there was more than one row for the previous timestamp.
if ($lastCountPerTimestamp > 1) {
$rows[] = new TableSeparator();
}
$startCount = count($rows);
$formattedTimestamp = $formatter->formatDate($timestamp);
uksort($byService, $sortServices);
foreach ($byService as $service => $byDimension) {
if (!isset($serviceTypes[$service])) {
$serviceTypes[$service] = $this->getServiceType($environment, $service);
}
$row = [];
$row['timestamp'] = new AdaptiveTableCell($formattedTimestamp, ['wrap' => false]);
$row['service'] = $service;
$row['type'] = $formatter->format($serviceTypes[$service], 'service_type');
foreach ($fieldMapping as $field => $fieldDefinition) {
/* @var Field $fieldDefinition */
$row[$field] = Format::format($fieldDefinition->format, $this->getValueFromSource($byDimension, $fieldDefinition->value), $fieldDefinition->warn);
}
$rows[] = $row;
}
$lastCountPerTimestamp = count($rows) - $startCount;
}
return $rows;
}
/**
* @param Environment $environment
* @param string $service
* @return string
*/
private function getServiceType(Environment $environment, $service)
{
$deployment = $this->api()->getCurrentDeployment($environment);
if (isset($deployment->services[$service])) {
$type = $deployment->services[$service]->type;
} elseif (isset($deployment->webapps[$service])) {
$type = $deployment->webapps[$service]->type;
} elseif (isset($deployment->workers[$service])) {
$type = $deployment->workers[$service]->type;
} else {
$type = '';
}
return $type;
}
/**
* @param Environment $environment
* @return \Closure
*/
private function getSortedServices(Environment $environment)
{
/** @var \Platformsh\Cli\Service\PropertyFormatter $formatter */
$formatter = $this->getService('property_formatter');
$deployment = $this->api()->getCurrentDeployment($this->getSelectedEnvironment());
// Create a closure which can sort services by name, putting apps and
// workers first.
$appAndWorkerNames = array_keys(array_merge($deployment->webapps, $deployment->workers));
sort($appAndWorkerNames, SORT_NATURAL);
$serviceNames = array_keys($deployment->services);
sort($serviceNames, SORT_NATURAL);
$nameOrder = array_flip(array_merge($appAndWorkerNames, $serviceNames, ['router']));
$sortServices = function ($a, $b) use ($nameOrder) {
$aPos = isset($nameOrder[$a]) ? $nameOrder[$a] : 1000;
$bPos = isset($nameOrder[$b]) ? $nameOrder[$b] : 1000;
return $aPos > $bPos ? 1 : ($aPos < $bPos ? -1 : 0);
};
return $sortServices;
}
/**
* @param array $point
* @param SourceField|SourceFieldPercentage $fieldDefinition
* @return float|null
*/
private function getValueFromSource(array $point, $fieldDefinition)
{
if ($fieldDefinition instanceof SourceFieldPercentage) {
$value = $this->extractValue($point, $fieldDefinition->value);
$limit = $this->extractValue($point, $fieldDefinition->limit);
return $limit > 0 ? $value / $limit * 100 : null;
}
return $this->extractValue($point, $fieldDefinition);
}
/**
* @param array $point
* @param SourceField $sourceField
* @return float|null
*/
private function extractValue(array $point, SourceField $sourceField)
{
if (isset($sourceField->mountpoint)) {
if (!isset($point['mountpoints'][$sourceField->mountpoint])) {
return null;
}
if (!isset($point['mountpoints'][$sourceField->mountpoint][$sourceField->source])) {
throw new \RuntimeException(\sprintf('Source "%s" not found in the mountpoint "%s".', $sourceField->source, $sourceField->mountpoint));
}
if (!isset($point['mountpoints'][$sourceField->mountpoint][$sourceField->source][$sourceField->aggregation])) {
throw new \RuntimeException(\sprintf('Aggregation "%s" not found for source "%s" in mountpoint "%s".', $sourceField->aggregation, $sourceField->source, $sourceField->mountpoint));
}
return $point['mountpoints'][$sourceField->mountpoint][$sourceField->source][$sourceField->aggregation];
}
if (!isset($point[$sourceField->source])) {
throw new \RuntimeException(\sprintf('Source "%s" not found in the data point.', $sourceField->source));
}
if (!isset($point[$sourceField->source][$sourceField->aggregation])) {
return null;
}
return $point[$sourceField->source][$sourceField->aggregation];
}
/**
* Displays the current project and environment, if not already displayed.
*
* @return void
*/
protected function displayEnvironmentHeader()
{
if (!$this->printedSelectedEnvironment) {
$this->stdErr->writeln('Selected project: ' . $this->api()->getProjectLabel($this->getSelectedProject()));
$this->stdErr->writeln('Selected environment: ' . $this->api()->getEnvironmentLabel($this->getSelectedEnvironment()));
}
$this->stdErr->writeln('');
}
/**
* Shows an explanation if services were found that use high memory.
*
* @return void
*/
protected function explainHighMemoryServices()
{
if ($this->foundHighMemoryServices) {
$this->stdErr->writeln('');
$this->stdErr->writeln('<comment>Note:</comment> it is possible for service memory usage to appear high even in normal circumstances.');
}
}
}