Skip to content

Commit

Permalink
DDEV-1965 fix labels after hashing key
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Davydenko committed Nov 19, 2024
1 parent c21cf8c commit eeb0b58
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 55 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": "^8.1",
"ext-redis": "*",
"laravel/framework": "^9.0 || ^10.0 || ^11.0",
"laravel/octane": "^2.5",
"laravel/octane": "^2.0",
"promphp/prometheus_client_php": "^2.6"
},
"require-dev": {
Expand Down
5 changes: 4 additions & 1 deletion config/octane.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
],
'gauge_values:100000' => [
'value' => 'float',
'key' => 'string:100000',
],

'сounters:1000' => [
Expand All @@ -28,14 +29,15 @@
],
'сounter_values:100000' => [
'value' => 'float',
'key' => 'string:100000',
],

'summaries:1000' => [
'meta' => 'string:10000',
'valueKeys' => 'string:10000',
],
'summary_values:100000' => [
'labelValues' => 'string:10000',
'key' => 'string:100000',
'sampleTimes' => 'string:10000',
'sampleValues' => 'string:10000',
],
Expand All @@ -46,6 +48,7 @@
],
'histogram_values:100000' => [
'value' => 'float',
'key' => 'string:100000',
],
],
];
4 changes: 4 additions & 0 deletions config/prometheus.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
// or
// 'apcu' => [
// 'prefix' => 'metrics'
// ],
// or
// 'octane-cache' => [
// 'prefix' => 'metrics'
// ],
'label_middlewares' => [
// \Ensi\LaravelPrometheus\LabelMiddlewares\AppNameLabelMiddleware::class,
Expand Down
3 changes: 3 additions & 0 deletions src/PrometheusServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class PrometheusServiceProvider extends ServiceProvider
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/../config/prometheus.php', 'prometheus');
if (array_key_exists('octane-cache', config('prometheus.bags'))) {
$this->mergeConfigFrom(__DIR__ . '/../config/octane.php', 'octane');
}

$this->app->singleton(PrometheusManager::class);
$this->app->alias(PrometheusManager::class, 'prometheus');
Expand Down
130 changes: 77 additions & 53 deletions src/Storage/OctaneCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public function updateHistogram(array $data): void
{
// Initialize the sum
$metaKey = $this->metaKey($data);
$metaKeyValue = $this->histograms->get($metaKey);
$metaKeyHash = hash('md5', $metaKey);
$metaKeyValue = $this->histograms->get($metaKeyHash);

if (!$metaKeyValue) {
$metaKeyValue = [
Expand All @@ -80,13 +81,18 @@ public function updateHistogram(array $data): void
}

$sumKey = $this->histogramBucketValueKey($data, 'sum');
$sumValue = $this->histogramValues->get($sumKey) ?? 0;
$sumKeyHash = hash('md5', $sumKey);
$sumValue = $this->histogramValues->get($sumKeyHash);
if (!$sumValue) {
$metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $sumKey);
$histogramValue = 0;
}
$metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $sumKeyHash);
$sumValue = [
'value' => 0,
'key' => $sumKey
];
}
$sumValue['value'] += $data['value'];
$this->histogramValues->set($sumKeyHash, $sumValue);

$histogramValue += $data['value'];


$bucketToIncrease = '+Inf';
Expand All @@ -99,14 +105,19 @@ public function updateHistogram(array $data): void
}

$bucketKey = $this->histogramBucketValueKey($data, $bucketToIncrease);
$bucketValue = $this->histogramValues->get($bucketKey) ?? 0;
$bucketKeyHash = hash('md5', $bucketKey);
$bucketValue = $this->histogramValues->get($bucketKeyHash);
if (!$bucketValue) {
$metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $bucketKey);
$bucketValue = 0;
$metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $bucketKeyHash);
$bucketValue = [
'value' => 0,
'key' => $bucketKey
];
}
$bucketValue += 1;
$bucketValue['value'] += 1;
$this->histogramValues->set($bucketKeyHash, $bucketValue);

$this->summaries->set($metaKey, $metaKeyValue);
$this->summaries->set($metaKeyHash, $metaKeyValue);
}

/**
Expand All @@ -116,31 +127,33 @@ public function updateHistogram(array $data): void
public function updateSummary(array $data): void
{
$metaKey = $this->metaKey($data);
$metaKeyHash = hash('md5', $metaKey);
$valueKey = $this->valueKey($data);
$valueKeyHash = hash('md5', $valueKey);

$metaKeyValue = $this->gauges->get($metaKey);
$metaKeyValue = $this->gauges->get($metaKeyHash);
if (!$metaKeyValue) {
$metaKeyValue = [
'meta' => $this->metaData($data),
'valueKeys' => '',
];
}

$summaryValue = $this->summaryValues->get($valueKey);
$summaryValue = $this->summaryValues->get($valueKeyHash);
if (!$summaryValue) {
$metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey);
$metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKeyHash);
$summaryValue = [
'sampleKeys' => '',
'key' => $valueKey,
'sampleTimes' => '',
'sampleValues' => '',
];
}

$this->summaryValues->set($valueKey, [
'labelValues' => $this->encodeLabelValues($data['labelValues']),
'sampleTimes' => $this->implodeKeysString($summaryValue['sampleTimes'], (string) time()),
'sampleValues' => $this->implodeKeysString($summaryValue['sampleValues'], (string) $data['value']),
]);
$summaryValue['sampleTimes'] = $this->implodeKeysString($summaryValue['sampleTimes'], (string) time());
$summaryValue['sampleValues'] = $this->implodeKeysString($summaryValue['sampleValues'], (string) $data['value']);

$this->summaries->set($metaKey, $metaKeyValue);
$this->summaryValues->set($valueKeyHash, $summaryValue);
$this->summaries->set($metaKeyHash, $metaKeyValue);
}

/**
Expand All @@ -150,28 +163,34 @@ public function updateSummary(array $data): void
public function updateGauge(array $data): void
{
$metaKey = $this->metaKey($data);
$metaKeyHash = hash('md5', $metaKey);
$valueKey = $this->valueKey($data);
$valueKeyHash = hash('md5', $valueKey);

$metaKeyValue = $this->gauges->get($metaKey);
$metaKeyValue = $this->gauges->get($metaKeyHash);
if (!$metaKeyValue) {
$metaKeyValue = [
'meta' => $this->metaData($data),
'valueKeys' => '',
];
}

$value = $this->сounterValues->get($valueKey);
if (!$value) {
$value = ['value' => 0];
$metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey);
$gaugeValue = $this->gaugeValues->get($valueKey);
if (!$gaugeValue) {
$metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKeyHash);
$gaugeValue = [
'value' => 0,
'key' => $valueKey
];
}
if ($data['command'] === Adapter::COMMAND_SET) {
$this->gaugeValues->set($valueKey, ['value' => $data['value']]);
$gaugeValue['value'] = $data['value'];
} else {
$this->gaugeValues->set($valueKey, ['value' => $value['value'] + $data['value']]);
$gaugeValue['value'] += $data['value'];
}

$this->gauges->set($metaKey, $metaKeyValue);

$this->gaugeValues->set($valueKeyHash, $gaugeValue);
$this->gauges->set($metaKeyHash, $metaKeyValue);
}

/**
Expand All @@ -181,27 +200,33 @@ public function updateGauge(array $data): void
public function updateCounter(array $data): void
{
$metaKey = $this->metaKey($data);
$metaKeyHash = hash('md5', $metaKey);
$valueKey = $this->valueKey($data);
$valueKeyHash = hash('md5', $valueKey);

$metaKeyValue = $this->сounters->get($metaKey);
$metaKeyValue = $this->сounters->get($metaKeyHash);
if (!$metaKeyValue) {
$metaKeyValue = [
'meta' => $this->metaData($data),
'valueKeys' => '',
];
}
$value = $this->сounterValues->get($valueKey);
if (!$value) {
$value = ['value' => 0];
$metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey);
$сounterValue = $this->сounterValues->get($valueKeyHash);
if (!$сounterValue) {
$metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKeyHash);
$сounterValue = [
'value' => 0,
'key' => $valueKey
];
}
if ($data['command'] === Adapter::COMMAND_SET) {
$this->сounterValues->set($valueKey, ['value' => 0]);
$сounterValue['value'] = 0;
} else {
$this->сounterValues->set($valueKey, ['value' => $value['value'] + $data['value']]);
$сounterValue['value'] += $data['value'];
}

$this->сounters->set($metaKey, $metaKeyValue);
$this->сounterValues->set($valueKeyHash, $сounterValue);
$this->сounters->set($metaKeyHash, $metaKeyValue);
}

/**
Expand All @@ -211,7 +236,7 @@ private function collectHistograms(): array
{
$histograms = [];
foreach ($this->histograms as $histogram) {
$metaData = json_decode($histogram['meta']);
$metaData = json_decode($histogram['meta'], true);
$data = [
'name' => $metaData['name'],
'help' => $metaData['help'],
Expand All @@ -225,11 +250,10 @@ private function collectHistograms(): array

$histogramBuckets = [];
foreach (explode('::', $histogram['valueKeys']) as $valueKey) {
$parts = explode(':', $valueKey);

$value = $this->histogramValues->get($valueKey);
$parts = explode(':', $value['key']);
$labelValues = $parts[2];
$bucket = $parts[3];
$value = $this->histogramValues->get($valueKey);
// Key by labelValues
$histogramBuckets[$labelValues][$bucket] = $value;
}
Expand Down Expand Up @@ -290,7 +314,7 @@ private function collectSummaries(): array
$math = new Math();
$summaries = [];
foreach ($this->summaries as $metaKey => $summary) {
$metaData = json_decode($summary['meta']);
$metaData = json_decode($summary['meta'], true);
$data = [
'name' => $metaData['name'],
'help' => $metaData['help'],
Expand All @@ -303,11 +327,11 @@ private function collectSummaries(): array

foreach (explode('::', $summary['valueKeys']) as $valueKey) {

$parts = explode(':', $valueKey);
$summaryValue = $this->summaryValues->get($valueKey);
$parts = explode(':', $summaryValue['key']);
$labelValues = $parts[2];
$decodedLabelValues = $this->decodeLabelValues($labelValues);

$summaryValue = $this->summaryValues->get($valueKey);
$sampleTimes = explode('::', $summaryValue['sampleTimes']);
$values = Arr::mapWithKeys(
explode('::', $summaryValue['sampleValues']),
Expand Down Expand Up @@ -375,7 +399,7 @@ private function collectGauges(): array
{
$result = [];
foreach ($this->gauges as $key => $metric) {
$metaData = json_decode($metric['meta']);
$metaData = json_decode($metric['meta'], true);
$data = [
'name' => $metaData['name'],
'help' => $metaData['help'],
Expand All @@ -385,7 +409,7 @@ private function collectGauges(): array
];
foreach (explode('::', $metric['valueKeys']) as $valueKey) {
$value = $this->gaugeValues->get($valueKey, 'value');
$parts = explode(':', $valueKey);
$parts = explode(':', $value['key']);
$labelValues = $parts[2];
$data['samples'][] = [
'name' => $metaData['name'],
Expand All @@ -412,7 +436,7 @@ private function collectCounters(): array
{
$result = [];
foreach ($this->сounters as $key => $metric) {
$metaData = json_decode($metric['meta']);
$metaData = json_decode($metric['meta'], true);
$data = [
'name' => $metaData['name'],
'help' => $metaData['help'],
Expand All @@ -422,7 +446,7 @@ private function collectCounters(): array
];
foreach (explode('::', $metric['valueKeys']) as $valueKey) {
$value = $this->сounterValues->get($valueKey, 'value');
$parts = explode(':', $valueKey);
$parts = explode(':', $value['key']);
$labelValues = $parts[2];
$data['samples'][] = [
'name' => $metaData['name'],
Expand Down Expand Up @@ -481,13 +505,13 @@ private function clearTable(Table $table): void
*/
private function valueKey(array $data): string
{
return hash('md5', implode(':', [
return implode(':', [
$this->prometheusPrefix,
$data['type'],
$data['name'],
$this->encodeLabelValues($data['labelValues']),
'value',
]));
]);
}

/**
Expand All @@ -509,12 +533,12 @@ private function implodeKeysString(string $keys, string $key): string
*/
protected function metaKey(array $data): string
{
return hash('md5', implode(':', [
return implode(':', [
$this->prometheusPrefix,
$data['type'],
$data['name'],
'meta',
]));
]);
}

/**
Expand Down

0 comments on commit eeb0b58

Please sign in to comment.