From 34962054604f42f377478c798914fdf26e831154 Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Thu, 14 Nov 2024 15:12:05 +0400 Subject: [PATCH 01/23] DDEV-1965 octane swoole tables storage --- composer.json | 4 +- config/octane.php | 51 ++++ src/MetricsBag.php | 3 + src/Storage/OctaneCache.php | 562 ++++++++++++++++++++++++++++++++++++ 4 files changed, 619 insertions(+), 1 deletion(-) create mode 100644 config/octane.php create mode 100644 src/Storage/OctaneCache.php diff --git a/composer.json b/composer.json index 1874aa4..cd3635a 100644 --- a/composer.json +++ b/composer.json @@ -7,17 +7,19 @@ "php": "^8.1", "ext-redis": "*", "laravel/framework": "^9.0 || ^10.0 || ^11.0", + "laravel/octane": "^2.5", "promphp/prometheus_client_php": "^2.6" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.2", "nunomaduro/collision": "^6.0 || ^7.0 || ^8.1", + "orchestra/testbench": "^7.0 || ^8.0 || ^9.0", "pestphp/pest": "^1.22 || ^2.0", "pestphp/pest-plugin-laravel": "^1.1 || ^2.0", "phpstan/extension-installer": "^1.3", "phpstan/phpstan": "^1.11", "spaze/phpstan-disallowed-calls": "^2.15", - "orchestra/testbench": "^7.0 || ^8.0 || ^9.0" + "swoole/ide-helper": "~5.0.0" }, "autoload": { "psr-4": { diff --git a/config/octane.php b/config/octane.php new file mode 100644 index 0000000..eff5793 --- /dev/null +++ b/config/octane.php @@ -0,0 +1,51 @@ + [ + 'gauges:1000' => [ + 'meta' => 'string:10000', + 'valueKeys' => 'string:10000', + ], + 'gauge_values:100000' => [ + 'value' => 'float', + ], + + 'сounters:1000' => [ + 'meta' => 'string:10000', + 'valueKeys' => 'string:10000', + ], + 'сounter_values:100000' => [ + 'value' => 'float', + ], + + 'summaries:1000' => [ + 'meta' => 'string:10000', + 'valueKeys' => 'string:10000', + ], + 'summary_values:100000' => [ + 'labelValues' => 'string:10000', + 'sampleTimes' => 'string:10000', + 'sampleValues' => 'string:10000', + ], + + 'histograms:1000' => [ + 'meta' => 'string:10000', + 'valueKeys' => 'string:10000', + ], + 'histogram_values:100000' => [ + 'value' => 'float', + ], + ], +]; diff --git a/src/MetricsBag.php b/src/MetricsBag.php index cb34e9d..6ae79ff 100644 --- a/src/MetricsBag.php +++ b/src/MetricsBag.php @@ -9,6 +9,7 @@ use Ensi\LaravelPrometheus\Metrics\Summary; use Ensi\LaravelPrometheus\OnDemandMetrics\OnDemandMetric; use Ensi\LaravelPrometheus\Storage\NullStorage; +use Ensi\LaravelPrometheus\Storage\OctaneCache; use Ensi\LaravelPrometheus\Storage\Redis; use Illuminate\Http\Request; use Illuminate\Support\Facades\Redis as RedisManager; @@ -179,6 +180,8 @@ private function getStorage(): Adapter return new APCng($this->config['apcu-ng']['prefix']); case array_key_exists('memory', $this->config): return new InMemory(); + case array_key_exists('octane-cache', $this->config): + return new OctaneCache($this->config['octane-cache']['prefix']); case array_key_exists('null-storage', $this->config): return new NullStorage(); } diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php new file mode 100644 index 0000000..e6c552f --- /dev/null +++ b/src/Storage/OctaneCache.php @@ -0,0 +1,562 @@ +gauges = Octane::table('gauges'); + $this->gaugeValues = Octane::table('gauge_values'); + + $this->сounters = Octane::table('сounters'); + $this->сounterValues = Octane::table('сounter_values'); + + $this->summaries = Octane::table('summaries'); + $this->summaryValues = Octane::table('summary_values'); + + $this->histograms = Octane::table('histograms'); + $this->histogramValues = Octane::table('histogram_values'); + } + + + /** + * @return MetricFamilySamples[] + * @throws StorageException + */ + public function collect(): array + { + $metrics = $this->collectHistograms(); + $metrics = array_merge($metrics, $this->collectGauges()); + $metrics = array_merge($metrics, $this->collectCounters()); + $metrics = array_merge($metrics, $this->collectSummaries()); + + return array_map( + function (array $metric): MetricFamilySamples { + return new MetricFamilySamples($metric); + }, + $metrics + ); + } + + + /** + * @param mixed[] $data + * @throws StorageException + */ + public function updateHistogram(array $data): void + { + // Initialize the sum + $metaKey = $this->metaKey($data); + $metaKeyValue = $this->histograms->get($metaKey); + + if(!$metaKeyValue) { + $metaKeyValue = [ + 'meta' => $this->metaData($data), + 'valueKeys' => '', + ]; + } + + $sumKey = $this->histogramBucketValueKey($data, 'sum'); + $sumValue = $this->histogramValues->get($sumKey) ?? 0; + if (!$sumValue) { + $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $sumKey); + $histogramValue = 0; + } + + $histogramValue += $data['value']; + + + $bucketToIncrease = '+Inf'; + foreach ($data['buckets'] as $bucket) { + if ($data['value'] <= $bucket) { + $bucketToIncrease = $bucket; + break; + } + } + + $bucketKey = $this->histogramBucketValueKey($data, $bucketToIncrease); + $bucketValue = $this->histogramValues->get($bucketKey) ?? 0; + if (!$bucketValue) { + $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $bucketKey); + $bucketValue = 0; + } + $bucketValue += 1; + + $this->summaries->set($metaKey, $metaKeyValue); + } + + /** + * @param mixed[] $data + * @throws StorageException + */ + public function updateSummary(array $data): void + { + $metaKey = $this->metaKey($data); + $valueKey = $this->valueKey($data); + + $metaKeyValue = $this->gauges->get($metaKey); + if(!$metaKeyValue) { + $metaKeyValue = [ + 'meta' => $this->metaData($data), + 'valueKeys' => '', + ]; + } + + $summaryValue = $this->summaryValues->get($valueKey); + if (!$summaryValue) { + $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); + $summaryValue = [ + 'sampleKeys' => '', + ]; + } + + $this->summaryValues->set($valueKey, [ + 'labelValues' => $this->encodeLabelValues($data['labelValues']), + 'sampleTimes' => $this->implodeKeysString($summaryValue['sampleTimes'], (string) time()), + 'sampleValues' => $this->implodeKeysString($summaryValue['sampleValues'], (string) $data['value']), + ]); + + $this->summaries->set($metaKey, $metaKeyValue); + } + + /** + * @param mixed[] $data + * @throws StorageException + */ + public function updateGauge(array $data): void + { + $metaKey = $this->metaKey($data); + $valueKey = $this->valueKey($data); + + $metaKeyValue = $this->gauges->get($metaKey); + if (!$metaKeyValue) { + $metaKeyValue = [ + 'meta' => $this->metaData($data), + 'valueKeys' => '', + ]; + } + if (!$this->gaugeValues->get($valueKey)) { + $value = 0; + } + if( !$this->gaugeValues->get($valueKey)) { + $value = $this->gaugeValues->get($valueKey) ?? 0; + $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); + } + if ($data['command'] === Adapter::COMMAND_SET) { + $this->gaugeValues->set($valueKey, ['value' => $data['value']]); + } else { + $this->gaugeValues->set($valueKey, ['value' => $value + $data['value']]); + } + + $this->gauges->set($metaKey, $metaKeyValue); + } + + /** + * @param mixed[] $data + * @throws StorageException + */ + public function updateCounter(array $data): void + { + $metaKey = $this->metaKey($data); + $valueKey = $this->valueKey($data); + + $metaKeyValue = $this->сounters->get($metaKey); + if (!$metaKeyValue) { + $metaKeyValue = [ + 'meta' => $this->metaData($data), + 'valueKeys' => '', + ]; + } + if (!$this->сounterValues->get($valueKey)) { + $value = 0; + } + if( !$this->сounterValues->get($valueKey)) { + $value = $this->сounterValues->get($valueKey) ?? 0; + $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); + } + if ($data['command'] === Adapter::COMMAND_SET) { + $this->сounterValues->set($valueKey, ['value' => 0]); + } else { + $this->сounterValues->set($valueKey, ['value' => $value + $data['value']]); + } + + $this->сounters->set($metaKey, $metaKeyValue); + } + + /** + * @return mixed[] + */ + private function collectHistograms(): array + { + $histograms = []; + foreach ($this->histograms as $histogram) { + $metaData = json_decode($histogram['meta']); + $data = [ + 'name' => $metaData['name'], + 'help' => $metaData['help'], + 'type' => $metaData['type'], + 'labelNames' => $metaData['labelNames'], + 'buckets' => $metaData['buckets'], + ]; + + // Add the Inf bucket so we can compute it later on + $data['buckets'][] = '+Inf'; + + $histogramBuckets = []; + foreach (explode('::', $histogram['valueKeys']) as $valueKey) { + $parts = explode(':', $valueKey); + + $labelValues = $parts[2]; + $bucket = $parts[3]; + $value = $this->histogramValues->get($valueKey); + // Key by labelValues + $histogramBuckets[$labelValues][$bucket] = $value; + } + + // Compute all buckets + $labels = array_keys($histogramBuckets); + sort($labels); + foreach ($labels as $labelValues) { + $acc = 0; + $decodedLabelValues = $this->decodeLabelValues($labelValues); + foreach ($data['buckets'] as $bucket) { + $bucket = (string)$bucket; + if (!isset($histogramBuckets[$labelValues][$bucket])) { + $data['samples'][] = [ + 'name' => $metaData['name'] . '_bucket', + 'labelNames' => ['le'], + 'labelValues' => array_merge($decodedLabelValues, [$bucket]), + 'value' => $acc, + ]; + } else { + $acc += $histogramBuckets[$labelValues][$bucket]; + $data['samples'][] = [ + 'name' => $metaData['name'] . '_' . 'bucket', + 'labelNames' => ['le'], + 'labelValues' => array_merge($decodedLabelValues, [$bucket]), + 'value' => $acc, + ]; + } + } + + // Add the count + $data['samples'][] = [ + 'name' => $metaData['name'] . '_count', + 'labelNames' => [], + 'labelValues' => $decodedLabelValues, + 'value' => $acc, + ]; + + // Add the sum + $data['samples'][] = [ + 'name' => $metaData['name'] . '_sum', + 'labelNames' => [], + 'labelValues' => $decodedLabelValues, + 'value' => $histogramBuckets[$labelValues]['sum'], + ]; + } + $histograms[] = new MetricFamilySamples($data); + } + return $histograms; + } + + + /** + * @return mixed[] + */ + private function collectSummaries(): array + { + $math = new Math(); + $summaries = []; + foreach ($this->summaries as $metaKey => $summary) { + $metaData = json_decode($summary['meta']); + $data = [ + 'name' => $metaData['name'], + 'help' => $metaData['help'], + 'type' => $metaData['type'], + 'labelNames' => $metaData['labelNames'], + 'maxAgeSeconds' => $metaData['maxAgeSeconds'], + 'quantiles' => $metaData['quantiles'], + 'samples' => [], + ]; + + foreach (explode('::', $summary['valueKeys']) as $valueKey) { + + $parts = explode(':', $valueKey); + $labelValues = $parts[2]; + $decodedLabelValues = $this->decodeLabelValues($labelValues); + + $summaryValue = $this->summaryValues->get($valueKey); + $sampleTimes = explode('::', $summaryValue['sampleTimes']); + $values = Arr::mapWithKeys( + explode('::', $summaryValue['sampleValues']), + fn($sampleValue, $key) => ['value' => (float) $sampleValue, 'time' => (int) $sampleTimes[$key]] + ); + + // Remove old data + $values = array_filter($values, function (array $value) use ($data): bool { + return time() - $value['time'] <= $data['maxAgeSeconds']; + }); + if (count($values) === 0) { + continue; + $this->summaryValues->del($valueKey); + } + + // Compute quantiles + usort($values, function (array $value1, array $value2) { + if ($value1['value'] === $value2['value']) { + return 0; + } + return ($value1['value'] < $value2['value']) ? -1 : 1; + }); + + foreach ($data['quantiles'] as $quantile) { + $data['samples'][] = [ + 'name' => $metaData['name'], + 'labelNames' => ['quantile'], + 'labelValues' => array_merge($decodedLabelValues, [$quantile]), + 'value' => $math->quantile(array_column($values, 'value'), $quantile), + ]; + } + + // Add the count + $data['samples'][] = [ + 'name' => $metaData['name'] . '_count', + 'labelNames' => [], + 'labelValues' => $decodedLabelValues, + 'value' => count($values), + ]; + + // Add the sum + $data['samples'][] = [ + 'name' => $metaData['name'] . '_sum', + 'labelNames' => [], + 'labelValues' => $decodedLabelValues, + 'value' => array_sum(array_column($values, 'value')), + ]; + } + + if (count($data['samples']) > 0) { + $summaries[] = new MetricFamilySamples($data); + } else { + $this->summaries->del($metaKey); + } + } + return $summaries; + } + + /** + * @return mixed[] + */ + private function collectGauges(): array + { + $result = []; + foreach ($this->gauges as $key => $metric) { + $metaData = json_decode($metric['meta']); + $data = [ + 'name' => $metaData['name'], + 'help' => $metaData['help'], + 'type' => $metaData['type'], + 'labelNames' => $metaData['labelNames'], + 'samples' => [], + ]; + foreach (explode('::', $metric['valueKeys']) as $valueKey) { + $value = $this->gaugeValues->get($valueKey, 'value'); + $parts = explode(':', $valueKey); + $labelValues = $parts[2]; + $data['samples'][] = [ + 'name' => $metaData['name'], + 'labelNames' => [], + 'labelValues' => $this->decodeLabelValues($labelValues), + 'value' => $value, + ]; + + $this->gaugeValues->del($valueKey); + } + + $result[] = new MetricFamilySamples($data); + + $this->сounters->del($key); + } + return $result; + } + + /** + * @return mixed[] + */ + private function collectCounters(): array + { + $result = []; + foreach ($this->сounters as $key => $metric) { + $metaData = json_decode($metric['meta']); + $data = [ + 'name' => $metaData['name'], + 'help' => $metaData['help'], + 'type' => $metaData['type'], + 'labelNames' => $metaData['labelNames'], + 'samples' => [], + ]; + foreach (explode('::', $metric['valueKeys']) as $valueKey) { + $value = $this->сounterValues->get($valueKey, 'value'); + $parts = explode(':', $valueKey); + $labelValues = $parts[2]; + $data['samples'][] = [ + 'name' => $metaData['name'], + 'labelNames' => [], + 'labelValues' => $this->decodeLabelValues($labelValues), + 'value' => $value, + ]; + + $this->сounterValues->del($valueKey); + } + + $result[] = new MetricFamilySamples($data); + + $this->сounters->del($key); + } + return $result; + } + + /** + * Removes all previously stored data from apcu + * + * @return void + */ + public function wipeStorage(): void + { + + } + + /** + * @param mixed[] $data + * @return string + */ + private function valueKey(array $data): string + { + return implode(':', [ + $this->prometheusPrefix, + $data['type'], + $data['name'], + $this->encodeLabelValues($data['labelValues']), + 'value', + ]); + } + + /** + * @param mixed[] $data + * @return string + */ + private function implodeKeysString(string $keys, string $key): string + { + return implode('::', [ + $keys, + $key + ]); + } + + /** + * @param mixed[] $data + * + * @return string + */ + protected function metaKey(array $data): string + { + return implode(':', [ + $this->prometheusPrefix, + $data['type'], + $data['name'], + 'meta' + ]); + } + + /** + * @param mixed[] $data + * @return mixed[] + */ + private function metaData(array $data): string + { + $metricsMetaData = $data; + unset($metricsMetaData['value'], $metricsMetaData['command'], $metricsMetaData['labelValues']); + return json_encode($metricsMetaData); + } + + /** + * @param mixed[] $values + * @return string + * @throws RuntimeException + */ + private function encodeLabelValues(array $values): string + { + $json = json_encode($values); + if (false === $json) { + throw new RuntimeException(json_last_error_msg()); + } + + return base64_encode($json); + } + + /** + * @param string $values + * @return mixed[] + * @throws RuntimeException + */ + private function decodeLabelValues(string $values): array + { + $json = base64_decode($values, true); + if (false === $json) { + throw new RuntimeException('Cannot base64 decode label values'); + } + $decodedValues = json_decode($json, true); + if (false === $decodedValues) { + throw new RuntimeException(json_last_error_msg()); + } + + return $decodedValues; + } + + /** + * @param mixed[] $data + * @param string|int $bucket + * + * @return string + */ + protected function histogramBucketValueKey(array $data, $bucket): string + { + return implode(':', [ + $data['type'], + $data['name'], + $this->encodeLabelValues($data['labelValues']), + $bucket, + ]); + } +} From c4f3c0654d3e1d767b04c97788731ab53ede74e9 Mon Sep 17 00:00:00 2001 From: SiriusV Date: Thu, 14 Nov 2024 11:12:25 +0000 Subject: [PATCH 02/23] Fix styling --- src/Storage/OctaneCache.php | 74 +++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index e6c552f..754086d 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -4,14 +4,14 @@ namespace Ensi\LaravelPrometheus\Storage; +use Illuminate\Support\Arr; +use Laravel\Octane\Facades\Octane; use Prometheus\Exception\StorageException; use Prometheus\Math; use Prometheus\MetricFamilySamples; use Prometheus\Storage\Adapter; use RuntimeException; use Swoole\Table; -use Laravel\Octane\Facades\Octane; -use Illuminate\Support\Arr; class OctaneCache implements Adapter { @@ -48,7 +48,6 @@ public function __construct(private string $prometheusPrefix = self::PROMETHEUS_ $this->histogramValues = Octane::table('histogram_values'); } - /** * @return MetricFamilySamples[] * @throws StorageException @@ -68,24 +67,23 @@ function (array $metric): MetricFamilySamples { ); } - /** * @param mixed[] $data * @throws StorageException */ public function updateHistogram(array $data): void { - // Initialize the sum + // Initialize the sum $metaKey = $this->metaKey($data); $metaKeyValue = $this->histograms->get($metaKey); - - if(!$metaKeyValue) { + + if (!$metaKeyValue) { $metaKeyValue = [ 'meta' => $this->metaData($data), 'valueKeys' => '', ]; } - + $sumKey = $this->histogramBucketValueKey($data, 'sum'); $sumValue = $this->histogramValues->get($sumKey) ?? 0; if (!$sumValue) { @@ -100,6 +98,7 @@ public function updateHistogram(array $data): void foreach ($data['buckets'] as $bucket) { if ($data['value'] <= $bucket) { $bucketToIncrease = $bucket; + break; } } @@ -107,8 +106,8 @@ public function updateHistogram(array $data): void $bucketKey = $this->histogramBucketValueKey($data, $bucketToIncrease); $bucketValue = $this->histogramValues->get($bucketKey) ?? 0; if (!$bucketValue) { - $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $bucketKey); - $bucketValue = 0; + $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $bucketKey); + $bucketValue = 0; } $bucketValue += 1; @@ -125,7 +124,7 @@ public function updateSummary(array $data): void $valueKey = $this->valueKey($data); $metaKeyValue = $this->gauges->get($metaKey); - if(!$metaKeyValue) { + if (!$metaKeyValue) { $metaKeyValue = [ 'meta' => $this->metaData($data), 'valueKeys' => '', @@ -139,7 +138,7 @@ public function updateSummary(array $data): void 'sampleKeys' => '', ]; } - + $this->summaryValues->set($valueKey, [ 'labelValues' => $this->encodeLabelValues($data['labelValues']), 'sampleTimes' => $this->implodeKeysString($summaryValue['sampleTimes'], (string) time()), @@ -154,10 +153,10 @@ public function updateSummary(array $data): void * @throws StorageException */ public function updateGauge(array $data): void - { + { $metaKey = $this->metaKey($data); $valueKey = $this->valueKey($data); - + $metaKeyValue = $this->gauges->get($metaKey); if (!$metaKeyValue) { $metaKeyValue = [ @@ -168,7 +167,7 @@ public function updateGauge(array $data): void if (!$this->gaugeValues->get($valueKey)) { $value = 0; } - if( !$this->gaugeValues->get($valueKey)) { + if (!$this->gaugeValues->get($valueKey)) { $value = $this->gaugeValues->get($valueKey) ?? 0; $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); } @@ -189,7 +188,7 @@ public function updateCounter(array $data): void { $metaKey = $this->metaKey($data); $valueKey = $this->valueKey($data); - + $metaKeyValue = $this->сounters->get($metaKey); if (!$metaKeyValue) { $metaKeyValue = [ @@ -200,7 +199,7 @@ public function updateCounter(array $data): void if (!$this->сounterValues->get($valueKey)) { $value = 0; } - if( !$this->сounterValues->get($valueKey)) { + if (!$this->сounterValues->get($valueKey)) { $value = $this->сounterValues->get($valueKey) ?? 0; $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); } @@ -287,10 +286,10 @@ private function collectHistograms(): array } $histograms[] = new MetricFamilySamples($data); } + return $histograms; } - /** * @return mixed[] */ @@ -319,8 +318,8 @@ private function collectSummaries(): array $summaryValue = $this->summaryValues->get($valueKey); $sampleTimes = explode('::', $summaryValue['sampleTimes']); $values = Arr::mapWithKeys( - explode('::', $summaryValue['sampleValues']), - fn($sampleValue, $key) => ['value' => (float) $sampleValue, 'time' => (int) $sampleTimes[$key]] + explode('::', $summaryValue['sampleValues']), + fn ($sampleValue, $key) => ['value' => (float) $sampleValue, 'time' => (int) $sampleTimes[$key]] ); // Remove old data @@ -337,6 +336,7 @@ private function collectSummaries(): array if ($value1['value'] === $value2['value']) { return 0; } + return ($value1['value'] < $value2['value']) ? -1 : 1; }); @@ -372,6 +372,7 @@ private function collectSummaries(): array $this->summaries->del($metaKey); } } + return $summaries; } @@ -394,7 +395,7 @@ private function collectGauges(): array $value = $this->gaugeValues->get($valueKey, 'value'); $parts = explode(':', $valueKey); $labelValues = $parts[2]; - $data['samples'][] = [ + $data['samples'][] = [ 'name' => $metaData['name'], 'labelNames' => [], 'labelValues' => $this->decodeLabelValues($labelValues), @@ -408,6 +409,7 @@ private function collectGauges(): array $this->сounters->del($key); } + return $result; } @@ -441,9 +443,10 @@ private function collectCounters(): array } $result[] = new MetricFamilySamples($data); - + $this->сounters->del($key); } + return $result; } @@ -480,22 +483,22 @@ private function implodeKeysString(string $keys, string $key): string { return implode('::', [ $keys, - $key + $key, ]); } - /** - * @param mixed[] $data - * - * @return string - */ + /** + * @param mixed[] $data + * + * @return string + */ protected function metaKey(array $data): string { return implode(':', [ $this->prometheusPrefix, $data['type'], $data['name'], - 'meta' + 'meta', ]); } @@ -507,6 +510,7 @@ private function metaData(array $data): string { $metricsMetaData = $data; unset($metricsMetaData['value'], $metricsMetaData['command'], $metricsMetaData['labelValues']); + return json_encode($metricsMetaData); } @@ -525,11 +529,11 @@ private function encodeLabelValues(array $values): string return base64_encode($json); } - /** - * @param string $values - * @return mixed[] - * @throws RuntimeException - */ + /** + * @param string $values + * @return mixed[] + * @throws RuntimeException + */ private function decodeLabelValues(string $values): array { $json = base64_decode($values, true); @@ -544,7 +548,7 @@ private function decodeLabelValues(string $values): array return $decodedValues; } - /** + /** * @param mixed[] $data * @param string|int $bucket * From 98ed51bc72a53f5ac8e8832cbca868f0e50344e0 Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Thu, 14 Nov 2024 15:37:58 +0400 Subject: [PATCH 03/23] DDEV-1965 destroy swoole tables --- src/Storage/OctaneCache.php | 99 +++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index 754086d..f6315f6 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -4,14 +4,14 @@ namespace Ensi\LaravelPrometheus\Storage; -use Illuminate\Support\Arr; -use Laravel\Octane\Facades\Octane; use Prometheus\Exception\StorageException; use Prometheus\Math; use Prometheus\MetricFamilySamples; use Prometheus\Storage\Adapter; use RuntimeException; use Swoole\Table; +use Laravel\Octane\Facades\Octane; +use Illuminate\Support\Arr; class OctaneCache implements Adapter { @@ -48,6 +48,7 @@ public function __construct(private string $prometheusPrefix = self::PROMETHEUS_ $this->histogramValues = Octane::table('histogram_values'); } + /** * @return MetricFamilySamples[] * @throws StorageException @@ -59,31 +60,27 @@ public function collect(): array $metrics = array_merge($metrics, $this->collectCounters()); $metrics = array_merge($metrics, $this->collectSummaries()); - return array_map( - function (array $metric): MetricFamilySamples { - return new MetricFamilySamples($metric); - }, - $metrics - ); + return $metrics; } + /** * @param mixed[] $data * @throws StorageException */ public function updateHistogram(array $data): void { - // Initialize the sum + // Initialize the sum $metaKey = $this->metaKey($data); $metaKeyValue = $this->histograms->get($metaKey); - - if (!$metaKeyValue) { + + if(!$metaKeyValue) { $metaKeyValue = [ 'meta' => $this->metaData($data), 'valueKeys' => '', ]; } - + $sumKey = $this->histogramBucketValueKey($data, 'sum'); $sumValue = $this->histogramValues->get($sumKey) ?? 0; if (!$sumValue) { @@ -98,7 +95,6 @@ public function updateHistogram(array $data): void foreach ($data['buckets'] as $bucket) { if ($data['value'] <= $bucket) { $bucketToIncrease = $bucket; - break; } } @@ -106,8 +102,8 @@ public function updateHistogram(array $data): void $bucketKey = $this->histogramBucketValueKey($data, $bucketToIncrease); $bucketValue = $this->histogramValues->get($bucketKey) ?? 0; if (!$bucketValue) { - $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $bucketKey); - $bucketValue = 0; + $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $bucketKey); + $bucketValue = 0; } $bucketValue += 1; @@ -124,7 +120,7 @@ public function updateSummary(array $data): void $valueKey = $this->valueKey($data); $metaKeyValue = $this->gauges->get($metaKey); - if (!$metaKeyValue) { + if(!$metaKeyValue) { $metaKeyValue = [ 'meta' => $this->metaData($data), 'valueKeys' => '', @@ -138,7 +134,7 @@ public function updateSummary(array $data): void 'sampleKeys' => '', ]; } - + $this->summaryValues->set($valueKey, [ 'labelValues' => $this->encodeLabelValues($data['labelValues']), 'sampleTimes' => $this->implodeKeysString($summaryValue['sampleTimes'], (string) time()), @@ -153,10 +149,10 @@ public function updateSummary(array $data): void * @throws StorageException */ public function updateGauge(array $data): void - { + { $metaKey = $this->metaKey($data); $valueKey = $this->valueKey($data); - + $metaKeyValue = $this->gauges->get($metaKey); if (!$metaKeyValue) { $metaKeyValue = [ @@ -167,7 +163,7 @@ public function updateGauge(array $data): void if (!$this->gaugeValues->get($valueKey)) { $value = 0; } - if (!$this->gaugeValues->get($valueKey)) { + if( !$this->gaugeValues->get($valueKey)) { $value = $this->gaugeValues->get($valueKey) ?? 0; $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); } @@ -188,7 +184,7 @@ public function updateCounter(array $data): void { $metaKey = $this->metaKey($data); $valueKey = $this->valueKey($data); - + $metaKeyValue = $this->сounters->get($metaKey); if (!$metaKeyValue) { $metaKeyValue = [ @@ -199,7 +195,7 @@ public function updateCounter(array $data): void if (!$this->сounterValues->get($valueKey)) { $value = 0; } - if (!$this->сounterValues->get($valueKey)) { + if( !$this->сounterValues->get($valueKey)) { $value = $this->сounterValues->get($valueKey) ?? 0; $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); } @@ -213,7 +209,7 @@ public function updateCounter(array $data): void } /** - * @return mixed[] + * @return MetricFamilySamples[] */ private function collectHistograms(): array { @@ -286,12 +282,12 @@ private function collectHistograms(): array } $histograms[] = new MetricFamilySamples($data); } - return $histograms; } + /** - * @return mixed[] + * @return MetricFamilySamples[] */ private function collectSummaries(): array { @@ -318,8 +314,8 @@ private function collectSummaries(): array $summaryValue = $this->summaryValues->get($valueKey); $sampleTimes = explode('::', $summaryValue['sampleTimes']); $values = Arr::mapWithKeys( - explode('::', $summaryValue['sampleValues']), - fn ($sampleValue, $key) => ['value' => (float) $sampleValue, 'time' => (int) $sampleTimes[$key]] + explode('::', $summaryValue['sampleValues']), + fn($sampleValue, $key) => ['value' => (float) $sampleValue, 'time' => (int) $sampleTimes[$key]] ); // Remove old data @@ -336,7 +332,6 @@ private function collectSummaries(): array if ($value1['value'] === $value2['value']) { return 0; } - return ($value1['value'] < $value2['value']) ? -1 : 1; }); @@ -372,12 +367,11 @@ private function collectSummaries(): array $this->summaries->del($metaKey); } } - return $summaries; } /** - * @return mixed[] + * @return MetricFamilySamples[] */ private function collectGauges(): array { @@ -395,7 +389,7 @@ private function collectGauges(): array $value = $this->gaugeValues->get($valueKey, 'value'); $parts = explode(':', $valueKey); $labelValues = $parts[2]; - $data['samples'][] = [ + $data['samples'][] = [ 'name' => $metaData['name'], 'labelNames' => [], 'labelValues' => $this->decodeLabelValues($labelValues), @@ -409,12 +403,11 @@ private function collectGauges(): array $this->сounters->del($key); } - return $result; } /** - * @return mixed[] + * @return MetricFamilySamples[] */ private function collectCounters(): array { @@ -443,10 +436,9 @@ private function collectCounters(): array } $result[] = new MetricFamilySamples($data); - + $this->сounters->del($key); } - return $result; } @@ -457,7 +449,17 @@ private function collectCounters(): array */ public function wipeStorage(): void { + $this->gauges->destroy(); + $this->gaugeValues->destroy(); + + $this->сounters->destroy(); + $this->сounterValues->destroy(); + + $this->summaries->destroy(); + $this->summaryValues->destroy(); + $this->histograms->destroy(); + $this->histogramValues->destroy(); } /** @@ -483,22 +485,22 @@ private function implodeKeysString(string $keys, string $key): string { return implode('::', [ $keys, - $key, + $key ]); } - /** - * @param mixed[] $data - * - * @return string - */ + /** + * @param mixed[] $data + * + * @return string + */ protected function metaKey(array $data): string { return implode(':', [ $this->prometheusPrefix, $data['type'], $data['name'], - 'meta', + 'meta' ]); } @@ -510,7 +512,6 @@ private function metaData(array $data): string { $metricsMetaData = $data; unset($metricsMetaData['value'], $metricsMetaData['command'], $metricsMetaData['labelValues']); - return json_encode($metricsMetaData); } @@ -529,11 +530,11 @@ private function encodeLabelValues(array $values): string return base64_encode($json); } - /** - * @param string $values - * @return mixed[] - * @throws RuntimeException - */ + /** + * @param string $values + * @return mixed[] + * @throws RuntimeException + */ private function decodeLabelValues(string $values): array { $json = base64_decode($values, true); @@ -548,7 +549,7 @@ private function decodeLabelValues(string $values): array return $decodedValues; } - /** + /** * @param mixed[] $data * @param string|int $bucket * From 5adef6f0c4525f3aa103c1b720ecab0d30c18fca Mon Sep 17 00:00:00 2001 From: SiriusV Date: Thu, 14 Nov 2024 11:38:15 +0000 Subject: [PATCH 04/23] Fix styling --- src/Storage/OctaneCache.php | 74 +++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index f6315f6..caf4cb4 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -4,14 +4,14 @@ namespace Ensi\LaravelPrometheus\Storage; +use Illuminate\Support\Arr; +use Laravel\Octane\Facades\Octane; use Prometheus\Exception\StorageException; use Prometheus\Math; use Prometheus\MetricFamilySamples; use Prometheus\Storage\Adapter; use RuntimeException; use Swoole\Table; -use Laravel\Octane\Facades\Octane; -use Illuminate\Support\Arr; class OctaneCache implements Adapter { @@ -48,7 +48,6 @@ public function __construct(private string $prometheusPrefix = self::PROMETHEUS_ $this->histogramValues = Octane::table('histogram_values'); } - /** * @return MetricFamilySamples[] * @throws StorageException @@ -63,24 +62,23 @@ public function collect(): array return $metrics; } - /** * @param mixed[] $data * @throws StorageException */ public function updateHistogram(array $data): void { - // Initialize the sum + // Initialize the sum $metaKey = $this->metaKey($data); $metaKeyValue = $this->histograms->get($metaKey); - - if(!$metaKeyValue) { + + if (!$metaKeyValue) { $metaKeyValue = [ 'meta' => $this->metaData($data), 'valueKeys' => '', ]; } - + $sumKey = $this->histogramBucketValueKey($data, 'sum'); $sumValue = $this->histogramValues->get($sumKey) ?? 0; if (!$sumValue) { @@ -95,6 +93,7 @@ public function updateHistogram(array $data): void foreach ($data['buckets'] as $bucket) { if ($data['value'] <= $bucket) { $bucketToIncrease = $bucket; + break; } } @@ -102,8 +101,8 @@ public function updateHistogram(array $data): void $bucketKey = $this->histogramBucketValueKey($data, $bucketToIncrease); $bucketValue = $this->histogramValues->get($bucketKey) ?? 0; if (!$bucketValue) { - $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $bucketKey); - $bucketValue = 0; + $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $bucketKey); + $bucketValue = 0; } $bucketValue += 1; @@ -120,7 +119,7 @@ public function updateSummary(array $data): void $valueKey = $this->valueKey($data); $metaKeyValue = $this->gauges->get($metaKey); - if(!$metaKeyValue) { + if (!$metaKeyValue) { $metaKeyValue = [ 'meta' => $this->metaData($data), 'valueKeys' => '', @@ -134,7 +133,7 @@ public function updateSummary(array $data): void 'sampleKeys' => '', ]; } - + $this->summaryValues->set($valueKey, [ 'labelValues' => $this->encodeLabelValues($data['labelValues']), 'sampleTimes' => $this->implodeKeysString($summaryValue['sampleTimes'], (string) time()), @@ -149,10 +148,10 @@ public function updateSummary(array $data): void * @throws StorageException */ public function updateGauge(array $data): void - { + { $metaKey = $this->metaKey($data); $valueKey = $this->valueKey($data); - + $metaKeyValue = $this->gauges->get($metaKey); if (!$metaKeyValue) { $metaKeyValue = [ @@ -163,7 +162,7 @@ public function updateGauge(array $data): void if (!$this->gaugeValues->get($valueKey)) { $value = 0; } - if( !$this->gaugeValues->get($valueKey)) { + if (!$this->gaugeValues->get($valueKey)) { $value = $this->gaugeValues->get($valueKey) ?? 0; $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); } @@ -184,7 +183,7 @@ public function updateCounter(array $data): void { $metaKey = $this->metaKey($data); $valueKey = $this->valueKey($data); - + $metaKeyValue = $this->сounters->get($metaKey); if (!$metaKeyValue) { $metaKeyValue = [ @@ -195,7 +194,7 @@ public function updateCounter(array $data): void if (!$this->сounterValues->get($valueKey)) { $value = 0; } - if( !$this->сounterValues->get($valueKey)) { + if (!$this->сounterValues->get($valueKey)) { $value = $this->сounterValues->get($valueKey) ?? 0; $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); } @@ -282,10 +281,10 @@ private function collectHistograms(): array } $histograms[] = new MetricFamilySamples($data); } + return $histograms; } - /** * @return MetricFamilySamples[] */ @@ -314,8 +313,8 @@ private function collectSummaries(): array $summaryValue = $this->summaryValues->get($valueKey); $sampleTimes = explode('::', $summaryValue['sampleTimes']); $values = Arr::mapWithKeys( - explode('::', $summaryValue['sampleValues']), - fn($sampleValue, $key) => ['value' => (float) $sampleValue, 'time' => (int) $sampleTimes[$key]] + explode('::', $summaryValue['sampleValues']), + fn ($sampleValue, $key) => ['value' => (float) $sampleValue, 'time' => (int) $sampleTimes[$key]] ); // Remove old data @@ -332,6 +331,7 @@ private function collectSummaries(): array if ($value1['value'] === $value2['value']) { return 0; } + return ($value1['value'] < $value2['value']) ? -1 : 1; }); @@ -367,6 +367,7 @@ private function collectSummaries(): array $this->summaries->del($metaKey); } } + return $summaries; } @@ -389,7 +390,7 @@ private function collectGauges(): array $value = $this->gaugeValues->get($valueKey, 'value'); $parts = explode(':', $valueKey); $labelValues = $parts[2]; - $data['samples'][] = [ + $data['samples'][] = [ 'name' => $metaData['name'], 'labelNames' => [], 'labelValues' => $this->decodeLabelValues($labelValues), @@ -403,6 +404,7 @@ private function collectGauges(): array $this->сounters->del($key); } + return $result; } @@ -436,9 +438,10 @@ private function collectCounters(): array } $result[] = new MetricFamilySamples($data); - + $this->сounters->del($key); } + return $result; } @@ -485,22 +488,22 @@ private function implodeKeysString(string $keys, string $key): string { return implode('::', [ $keys, - $key + $key, ]); } - /** - * @param mixed[] $data - * - * @return string - */ + /** + * @param mixed[] $data + * + * @return string + */ protected function metaKey(array $data): string { return implode(':', [ $this->prometheusPrefix, $data['type'], $data['name'], - 'meta' + 'meta', ]); } @@ -512,6 +515,7 @@ private function metaData(array $data): string { $metricsMetaData = $data; unset($metricsMetaData['value'], $metricsMetaData['command'], $metricsMetaData['labelValues']); + return json_encode($metricsMetaData); } @@ -530,11 +534,11 @@ private function encodeLabelValues(array $values): string return base64_encode($json); } - /** - * @param string $values - * @return mixed[] - * @throws RuntimeException - */ + /** + * @param string $values + * @return mixed[] + * @throws RuntimeException + */ private function decodeLabelValues(string $values): array { $json = base64_decode($values, true); @@ -549,7 +553,7 @@ private function decodeLabelValues(string $values): array return $decodedValues; } - /** + /** * @param mixed[] $data * @param string|int $bucket * From 2cc78b892b084f97dfaaf2be32fea13b395162b0 Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Fri, 15 Nov 2024 14:06:45 +0400 Subject: [PATCH 05/23] DDEV-1965 change wipeStorage to del in while loops --- src/Storage/OctaneCache.php | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index caf4cb4..aa5a3d8 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -452,17 +452,31 @@ private function collectCounters(): array */ public function wipeStorage(): void { - $this->gauges->destroy(); - $this->gaugeValues->destroy(); + $this->clearTable($this->gauges); + $this->clearTable($this->gaugeValues); - $this->сounters->destroy(); - $this->сounterValues->destroy(); + $this->clearTable($this->сounters); + $this->clearTable($this->сounterValues); - $this->summaries->destroy(); - $this->summaryValues->destroy(); + $this->clearTable($this->summaries); + $this->clearTable($this->summaryValues); - $this->histograms->destroy(); - $this->histogramValues->destroy(); + $this->clearTable($this->histograms); + $this->clearTable($this->histogramValues); + } + + /** + * @param Table $table + * @return void + */ + private function clearTable(Table $table): void + { + $table->rewind(); + while ($table->valid()) + { + $table->del($table->key()); + $table->next(); + } } /** From 76f83e265c894f0d329336386de0f8f88be3b166 Mon Sep 17 00:00:00 2001 From: SiriusV Date: Fri, 15 Nov 2024 10:07:07 +0000 Subject: [PATCH 06/23] Fix styling --- src/Storage/OctaneCache.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index aa5a3d8..1ba4b5e 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -472,8 +472,7 @@ public function wipeStorage(): void private function clearTable(Table $table): void { $table->rewind(); - while ($table->valid()) - { + while ($table->valid()) { $table->del($table->key()); $table->next(); } From 508c52d27ef2d6a99256cd223b520c28f993b485 Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Mon, 18 Nov 2024 12:46:38 +0400 Subject: [PATCH 07/23] =?UTF-8?q?DDEV-1965=20fix=20get=20=D1=81ounterValue?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Storage/OctaneCache.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index aa5a3d8..0e28648 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -191,11 +191,8 @@ public function updateCounter(array $data): void 'valueKeys' => '', ]; } - if (!$this->сounterValues->get($valueKey)) { - $value = 0; - } - if (!$this->сounterValues->get($valueKey)) { - $value = $this->сounterValues->get($valueKey) ?? 0; + $value = $this->сounterValues->get($valueKey) ?? 0; + if (!$value) { $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); } if ($data['command'] === Adapter::COMMAND_SET) { From aabd94c2113ebe71132fcf36eb8ca9f7ffed4dc2 Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Mon, 18 Nov 2024 13:16:04 +0400 Subject: [PATCH 08/23] DDEV-1965 fix updateGauge and updateCounter --- src/Storage/OctaneCache.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index ee4b694..1159edd 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -159,17 +159,16 @@ public function updateGauge(array $data): void 'valueKeys' => '', ]; } - if (!$this->gaugeValues->get($valueKey)) { - $value = 0; - } - if (!$this->gaugeValues->get($valueKey)) { - $value = $this->gaugeValues->get($valueKey) ?? 0; + + $value = $this->сounterValues->get($valueKey); + if (!$value) { + $value = ['value' => 0]; $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); } if ($data['command'] === Adapter::COMMAND_SET) { $this->gaugeValues->set($valueKey, ['value' => $data['value']]); } else { - $this->gaugeValues->set($valueKey, ['value' => $value + $data['value']]); + $this->gaugeValues->set($valueKey, ['value' => $value['value'] + $data['value']]); } $this->gauges->set($metaKey, $metaKeyValue); @@ -191,14 +190,15 @@ public function updateCounter(array $data): void 'valueKeys' => '', ]; } - $value = $this->сounterValues->get($valueKey) ?? 0; + $value = $this->сounterValues->get($valueKey); if (!$value) { + $value = ['value' => 0]; $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); } if ($data['command'] === Adapter::COMMAND_SET) { $this->сounterValues->set($valueKey, ['value' => 0]); } else { - $this->сounterValues->set($valueKey, ['value' => $value + $data['value']]); + $this->сounterValues->set($valueKey, ['value' => $value['value'] + $data['value']]); } $this->сounters->set($metaKey, $metaKeyValue); From 141f0b9572744edb79c4ac8e29525d687fd430be Mon Sep 17 00:00:00 2001 From: SiriusV Date: Mon, 18 Nov 2024 09:16:53 +0000 Subject: [PATCH 09/23] Fix styling --- src/Storage/OctaneCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index 1159edd..38c23ab 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -160,7 +160,7 @@ public function updateGauge(array $data): void ]; } - $value = $this->сounterValues->get($valueKey); + $value = $this->сounterValues->get($valueKey); if (!$value) { $value = ['value' => 0]; $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKey); From c26e3c54b1e8dc144b78cc5569bc9eef33a6b27f Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Mon, 18 Nov 2024 13:30:20 +0400 Subject: [PATCH 10/23] DDEV-1965 md5 hashing key --- src/Storage/OctaneCache.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index 1159edd..c9472b3 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -481,13 +481,13 @@ private function clearTable(Table $table): void */ private function valueKey(array $data): string { - return implode(':', [ + return hash('md5', implode(':', [ $this->prometheusPrefix, $data['type'], $data['name'], $this->encodeLabelValues($data['labelValues']), 'value', - ]); + ])); } /** @@ -509,12 +509,12 @@ private function implodeKeysString(string $keys, string $key): string */ protected function metaKey(array $data): string { - return implode(':', [ + return hash('md5', implode(':', [ $this->prometheusPrefix, $data['type'], $data['name'], 'meta', - ]); + ])); } /** From eeb0b582a1e4a1562336900ec5a7fe00a2ce9fa7 Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Tue, 19 Nov 2024 13:28:02 +0400 Subject: [PATCH 11/23] DDEV-1965 fix labels after hashing key --- composer.json | 2 +- config/octane.php | 5 +- config/prometheus.php | 4 + src/PrometheusServiceProvider.php | 3 + src/Storage/OctaneCache.php | 130 ++++++++++++++++++------------ 5 files changed, 89 insertions(+), 55 deletions(-) diff --git a/composer.json b/composer.json index cd3635a..c1b87ce 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/config/octane.php b/config/octane.php index eff5793..3ab439f 100644 --- a/config/octane.php +++ b/config/octane.php @@ -20,6 +20,7 @@ ], 'gauge_values:100000' => [ 'value' => 'float', + 'key' => 'string:100000', ], 'сounters:1000' => [ @@ -28,6 +29,7 @@ ], 'сounter_values:100000' => [ 'value' => 'float', + 'key' => 'string:100000', ], 'summaries:1000' => [ @@ -35,7 +37,7 @@ 'valueKeys' => 'string:10000', ], 'summary_values:100000' => [ - 'labelValues' => 'string:10000', + 'key' => 'string:100000', 'sampleTimes' => 'string:10000', 'sampleValues' => 'string:10000', ], @@ -46,6 +48,7 @@ ], 'histogram_values:100000' => [ 'value' => 'float', + 'key' => 'string:100000', ], ], ]; diff --git a/config/prometheus.php b/config/prometheus.php index 4de1722..3cad332 100644 --- a/config/prometheus.php +++ b/config/prometheus.php @@ -24,6 +24,10 @@ // or // 'apcu' => [ // 'prefix' => 'metrics' +// ], +// or +// 'octane-cache' => [ +// 'prefix' => 'metrics' // ], 'label_middlewares' => [ // \Ensi\LaravelPrometheus\LabelMiddlewares\AppNameLabelMiddleware::class, diff --git a/src/PrometheusServiceProvider.php b/src/PrometheusServiceProvider.php index d5967bc..9a603f8 100644 --- a/src/PrometheusServiceProvider.php +++ b/src/PrometheusServiceProvider.php @@ -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'); diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index 26d1359..18142dd 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -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 = [ @@ -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'; @@ -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); } /** @@ -116,9 +127,11 @@ 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), @@ -126,21 +139,21 @@ public function updateSummary(array $data): void ]; } - $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); } /** @@ -150,9 +163,11 @@ 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), @@ -160,18 +175,22 @@ public function updateGauge(array $data): void ]; } - $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); } /** @@ -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); } /** @@ -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'], @@ -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; } @@ -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'], @@ -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']), @@ -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'], @@ -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'], @@ -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'], @@ -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'], @@ -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', - ])); + ]); } /** @@ -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', - ])); + ]); } /** From 6b77a013dc2e87ea0e967de63b208ff4b1e29b86 Mon Sep 17 00:00:00 2001 From: SiriusV Date: Tue, 19 Nov 2024 09:28:22 +0000 Subject: [PATCH 12/23] Fix styling --- src/Storage/OctaneCache.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index 18142dd..79f182c 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -87,9 +87,9 @@ public function updateHistogram(array $data): void $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $sumKeyHash); $sumValue = [ 'value' => 0, - 'key' => $sumKey + 'key' => $sumKey, ]; - } + } $sumValue['value'] += $data['value']; $this->histogramValues->set($sumKeyHash, $sumValue); @@ -111,7 +111,7 @@ public function updateHistogram(array $data): void $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $bucketKeyHash); $bucketValue = [ 'value' => 0, - 'key' => $bucketKey + 'key' => $bucketKey, ]; } $bucketValue['value'] += 1; @@ -180,7 +180,7 @@ public function updateGauge(array $data): void $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKeyHash); $gaugeValue = [ 'value' => 0, - 'key' => $valueKey + 'key' => $valueKey, ]; } if ($data['command'] === Adapter::COMMAND_SET) { @@ -188,7 +188,7 @@ public function updateGauge(array $data): void } else { $gaugeValue['value'] += $data['value']; } - + $this->gaugeValues->set($valueKeyHash, $gaugeValue); $this->gauges->set($metaKeyHash, $metaKeyValue); } @@ -216,7 +216,7 @@ public function updateCounter(array $data): void $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKeyHash); $сounterValue = [ 'value' => 0, - 'key' => $valueKey + 'key' => $valueKey, ]; } if ($data['command'] === Adapter::COMMAND_SET) { From eabdcc122e42ad3541b94c7a563b6e48ce646d2d Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Tue, 19 Nov 2024 13:32:50 +0400 Subject: [PATCH 13/23] DDEV-1965 fix labels after hashing key --- src/Storage/OctaneCache.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index 79f182c..6355f20 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -408,14 +408,14 @@ private function collectGauges(): array 'samples' => [], ]; foreach (explode('::', $metric['valueKeys']) as $valueKey) { - $value = $this->gaugeValues->get($valueKey, 'value'); + $value = $this->gaugeValues->get($valueKey); $parts = explode(':', $value['key']); $labelValues = $parts[2]; $data['samples'][] = [ 'name' => $metaData['name'], 'labelNames' => [], 'labelValues' => $this->decodeLabelValues($labelValues), - 'value' => $value, + 'value' => $value['value'], ]; $this->gaugeValues->del($valueKey); @@ -445,14 +445,14 @@ private function collectCounters(): array 'samples' => [], ]; foreach (explode('::', $metric['valueKeys']) as $valueKey) { - $value = $this->сounterValues->get($valueKey, 'value'); + $value = $this->сounterValues->get($valueKey); $parts = explode(':', $value['key']); $labelValues = $parts[2]; $data['samples'][] = [ 'name' => $metaData['name'], 'labelNames' => [], 'labelValues' => $this->decodeLabelValues($labelValues), - 'value' => $value, + 'value' => $value['value'], ]; $this->сounterValues->del($valueKey); From ec133826e33738dc2a79dc3bc9d8b2e4c75c7241 Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Tue, 19 Nov 2024 13:39:51 +0400 Subject: [PATCH 14/23] DDEV-1965 fix after hashing key --- src/Storage/OctaneCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index 6355f20..080c8bf 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -175,7 +175,7 @@ public function updateGauge(array $data): void ]; } - $gaugeValue = $this->gaugeValues->get($valueKey); + $gaugeValue = $this->gaugeValues->get($valueKeyHash); if (!$gaugeValue) { $metaKeyValue['valueKeys'] = $this->implodeKeysString($metaKeyValue['valueKeys'], $valueKeyHash); $gaugeValue = [ From d9aafddda197dd3c4e199e6c72f4fc6bf4a82f9f Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Tue, 19 Nov 2024 13:58:34 +0400 Subject: [PATCH 15/23] DDEV-1965 fix after hashing key --- src/Storage/OctaneCache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index 080c8bf..28266a0 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -520,10 +520,10 @@ private function valueKey(array $data): string */ private function implodeKeysString(string $keys, string $key): string { - return implode('::', [ + return $keys ? implode('::', [ $keys, $key, - ]); + ]) : $key; } /** From 4499d3f1bd61dddf6058a157918a43207cdbe93f Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Wed, 20 Nov 2024 10:10:08 +0400 Subject: [PATCH 16/23] DDEV-1965 delete prefix --- config/prometheus.php | 1 - src/MetricsBag.php | 2 +- src/Storage/OctaneCache.php | 6 +----- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/config/prometheus.php b/config/prometheus.php index 3cad332..71922df 100644 --- a/config/prometheus.php +++ b/config/prometheus.php @@ -27,7 +27,6 @@ // ], // or // 'octane-cache' => [ -// 'prefix' => 'metrics' // ], 'label_middlewares' => [ // \Ensi\LaravelPrometheus\LabelMiddlewares\AppNameLabelMiddleware::class, diff --git a/src/MetricsBag.php b/src/MetricsBag.php index 6ae79ff..29a637a 100644 --- a/src/MetricsBag.php +++ b/src/MetricsBag.php @@ -181,7 +181,7 @@ private function getStorage(): Adapter case array_key_exists('memory', $this->config): return new InMemory(); case array_key_exists('octane-cache', $this->config): - return new OctaneCache($this->config['octane-cache']['prefix']); + return new OctaneCache(); case array_key_exists('null-storage', $this->config): return new NullStorage(); } diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index 28266a0..4ab087a 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -15,8 +15,6 @@ class OctaneCache implements Adapter { - public const PROMETHEUS_PREFIX = 'PROMETHEUS_'; - private Table $gauges; private Table $gaugeValues; @@ -33,7 +31,7 @@ class OctaneCache implements Adapter * Redis constructor. * @param mixed[] $options */ - public function __construct(private string $prometheusPrefix = self::PROMETHEUS_PREFIX) + public function __construct() { $this->gauges = Octane::table('gauges'); $this->gaugeValues = Octane::table('gauge_values'); @@ -506,7 +504,6 @@ private function clearTable(Table $table): void private function valueKey(array $data): string { return implode(':', [ - $this->prometheusPrefix, $data['type'], $data['name'], $this->encodeLabelValues($data['labelValues']), @@ -534,7 +531,6 @@ private function implodeKeysString(string $keys, string $key): string protected function metaKey(array $data): string { return implode(':', [ - $this->prometheusPrefix, $data['type'], $data['name'], 'meta', From 6815c880b774251c6b705c51868cd05879642224 Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Wed, 20 Nov 2024 10:57:30 +0400 Subject: [PATCH 17/23] DDEV-1965 fix table name --- src/Storage/OctaneCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index 4ab087a..cc4d08f 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -421,7 +421,7 @@ private function collectGauges(): array $result[] = new MetricFamilySamples($data); - $this->сounters->del($key); + $this->gauges->del($key); } return $result; From e92eedf13e4659497a138e358458b39bbb36024d Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Mon, 25 Nov 2024 09:46:19 +0400 Subject: [PATCH 18/23] DDEV-1965 fix config --- src/PrometheusServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PrometheusServiceProvider.php b/src/PrometheusServiceProvider.php index 9a603f8..f7fff2f 100644 --- a/src/PrometheusServiceProvider.php +++ b/src/PrometheusServiceProvider.php @@ -11,7 +11,7 @@ class PrometheusServiceProvider extends ServiceProvider public function register(): void { $this->mergeConfigFrom(__DIR__ . '/../config/prometheus.php', 'prometheus'); - if (array_key_exists('octane-cache', config('prometheus.bags'))) { + if (array_key_exists('octane-cache', config('prometheus.bags.'.config('prometheus.default_bag')))) { $this->mergeConfigFrom(__DIR__ . '/../config/octane.php', 'octane'); } From e687b1b45d4462ab6077630711170350b507d140 Mon Sep 17 00:00:00 2001 From: SiriusV Date: Mon, 25 Nov 2024 05:46:53 +0000 Subject: [PATCH 19/23] Fix styling --- src/PrometheusServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PrometheusServiceProvider.php b/src/PrometheusServiceProvider.php index f7fff2f..11b9825 100644 --- a/src/PrometheusServiceProvider.php +++ b/src/PrometheusServiceProvider.php @@ -11,7 +11,7 @@ class PrometheusServiceProvider extends ServiceProvider public function register(): void { $this->mergeConfigFrom(__DIR__ . '/../config/prometheus.php', 'prometheus'); - if (array_key_exists('octane-cache', config('prometheus.bags.'.config('prometheus.default_bag')))) { + if (array_key_exists('octane-cache', config('prometheus.bags.' . config('prometheus.default_bag')))) { $this->mergeConfigFrom(__DIR__ . '/../config/octane.php', 'octane'); } From 57fa0b3b199e5a210cd057b55e6aac5ad20ab413 Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Mon, 16 Dec 2024 11:58:00 +0400 Subject: [PATCH 20/23] =?UTF-8?q?DDEV-1965=20=D1=83=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D1=8C=D1=88=D0=B8=D0=BB=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80?= =?UTF-8?q?=20=D1=82=D0=B0=D0=B1=D0=BB=D0=B8=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/octane.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/config/octane.php b/config/octane.php index 3ab439f..97c88d8 100644 --- a/config/octane.php +++ b/config/octane.php @@ -18,26 +18,26 @@ 'meta' => 'string:10000', 'valueKeys' => 'string:10000', ], - 'gauge_values:100000' => [ + 'gauge_values:10000' => [ 'value' => 'float', - 'key' => 'string:100000', + 'key' => 'string:10000', ], 'сounters:1000' => [ 'meta' => 'string:10000', 'valueKeys' => 'string:10000', ], - 'сounter_values:100000' => [ + 'сounter_values:10000' => [ 'value' => 'float', - 'key' => 'string:100000', + 'key' => 'string:10000', ], 'summaries:1000' => [ 'meta' => 'string:10000', 'valueKeys' => 'string:10000', ], - 'summary_values:100000' => [ - 'key' => 'string:100000', + 'summary_values:10000' => [ + 'key' => 'string:10000', 'sampleTimes' => 'string:10000', 'sampleValues' => 'string:10000', ], @@ -46,9 +46,9 @@ 'meta' => 'string:10000', 'valueKeys' => 'string:10000', ], - 'histogram_values:100000' => [ + 'histogram_values:10000' => [ 'value' => 'float', - 'key' => 'string:100000', + 'key' => 'string:10000', ], ], ]; From 745d14d4ba88a6d3c374f33a3f739eb0b1bc6f6d Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Tue, 17 Dec 2024 10:37:13 +0400 Subject: [PATCH 21/23] =?UTF-8?q?DDEV-1965=20=D1=84=D0=B8=D0=BA=D1=81=20?= =?UTF-8?q?=D0=B8=D0=BC=D0=B5=D0=BD=D0=B8=20=D1=82=D0=B0=D0=B1=D0=BB=D0=B8?= =?UTF-8?q?=D1=86=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Storage/OctaneCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index cc4d08f..f2fc605 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -129,7 +129,7 @@ public function updateSummary(array $data): void $valueKey = $this->valueKey($data); $valueKeyHash = hash('md5', $valueKey); - $metaKeyValue = $this->gauges->get($metaKeyHash); + $metaKeyValue = $this->summaries->get($metaKeyHash); if (!$metaKeyValue) { $metaKeyValue = [ 'meta' => $this->metaData($data), From b055f31bf3d599adc1256c0fb9dcdceda00c310c Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Tue, 17 Dec 2024 11:02:59 +0400 Subject: [PATCH 22/23] =?UTF-8?q?DDEV-1965=20=D1=84=D0=B8=D0=BA=D1=81=20?= =?UTF-8?q?=D0=B8=D0=BC=D0=B5=D0=BD=D0=B8=20=D1=82=D0=B0=D0=B1=D0=BB=D0=B8?= =?UTF-8?q?=D1=86=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Storage/OctaneCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index f2fc605..1f4e3d5 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -115,7 +115,7 @@ public function updateHistogram(array $data): void $bucketValue['value'] += 1; $this->histogramValues->set($bucketKeyHash, $bucketValue); - $this->summaries->set($metaKeyHash, $metaKeyValue); + $this->histograms->set($metaKeyHash, $metaKeyValue); } /** From fadf417dbd606c0e96eea1d96fff7ff483bfd0ae Mon Sep 17 00:00:00 2001 From: Vadim Davydenko Date: Tue, 17 Dec 2024 11:16:15 +0400 Subject: [PATCH 23/23] =?UTF-8?q?DDEV-1965=20=D1=84=D0=B8=D0=BA=D1=81=20hi?= =?UTF-8?q?stogram=20=20value?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Storage/OctaneCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/OctaneCache.php b/src/Storage/OctaneCache.php index 1f4e3d5..f49fe9a 100644 --- a/src/Storage/OctaneCache.php +++ b/src/Storage/OctaneCache.php @@ -253,7 +253,7 @@ private function collectHistograms(): array $labelValues = $parts[2]; $bucket = $parts[3]; // Key by labelValues - $histogramBuckets[$labelValues][$bucket] = $value; + $histogramBuckets[$labelValues][$bucket] = $value['value']; } // Compute all buckets