Skip to content

Commit 21a4e7d

Browse files
authored
Merge pull request #6 from ensi-platform/aecom-1055
AECOM-1055 remove redis metrics when labelValues identical, but keys not
2 parents 405ac6b + 3cc5257 commit 21a4e7d

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

src/Storage/Redis.php

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,6 @@ public function updateCounter(array $data): void
407407
);
408408
}
409409

410-
/**
411-
* @param array $data
412-
* @return bool|int
413-
*/
414410
private function removeMetricOnLabelDiff(array $data): bool
415411
{
416412
$metrics = match ($data['type']) {
@@ -426,12 +422,9 @@ private function removeMetricOnLabelDiff(array $data): bool
426422
continue;
427423
}
428424

429-
$labelDiff1 = count(array_diff($metric['labelNames'], $data['labelNames']));
430-
$labelDiff2 = count(array_diff($data['labelNames'], $metric['labelNames']));
431-
432425
if (
433-
($labelDiff1 == 0 && $labelDiff2 == 0) ||
434-
($labelDiff1 == count($metric['labelNames']) && $labelDiff2 == count($data['labelNames']))
426+
$this->isMetricLabelNamesIdentical($data['labelNames'], $metric['labelNames']) &&
427+
!$this->issetMetricWithSuchLabelValues($data['labelValues'], $metric['samples'] ?? [])
435428
) {
436429
return false;
437430
}
@@ -442,10 +435,40 @@ private function removeMetricOnLabelDiff(array $data): bool
442435
return false;
443436
}
444437

438+
private function isMetricLabelNamesIdentical(array $labelNames, array $existingMetricLabelNames): bool
439+
{
440+
$labelDiff1 = count(array_diff($existingMetricLabelNames, $labelNames));
441+
$labelDiff2 = count(array_diff($labelNames, $existingMetricLabelNames));
442+
443+
if (
444+
($labelDiff1 == 0 && $labelDiff2 == 0) ||
445+
($labelDiff1 == count($existingMetricLabelNames) && $labelDiff2 == count($labelNames))
446+
) {
447+
return true;
448+
}
449+
450+
return false;
451+
}
452+
453+
/**
454+
* Существует ли метрика с таким же labelValues, но с разными ключами
455+
*/
456+
private function issetMetricWithSuchLabelValues(array $labelValues, array $existingMetricSamples): bool
457+
{
458+
foreach ($existingMetricSamples as $sample) {
459+
if (
460+
count(array_diff($labelValues, $sample['labelValues'])) == 0 &&
461+
count(array_diff($sample['labelValues'], $labelValues)) == 0 &&
462+
(count(array_diff_key($labelValues, $sample['labelValues'])) > 0 || count(array_diff_key($sample['labelValues'], $labelValues)) > 0)
463+
) {
464+
return true;
465+
}
466+
}
467+
468+
return false;
469+
}
470+
445471
/**
446-
* @param string $type
447-
* @param array $data
448-
* @return bool|int
449472
* @throws \RedisException
450473
*/
451474
public function removeMetric(string $type, array $data): bool

0 commit comments

Comments
 (0)