Skip to content

Conversation

@Princemachiavelli
Copy link
Contributor

The peak memory usage of the APCng storage adapter while collecting the metrics for display is currently always the worst-case for the cardinality of the metric label values.

This change replaces the buildPermutationTree implementation with a (cartesian product) generator which avoids keeping every metric label value permutation in memory at once. This can reduce peak memory usage by orders of magnitude for metrics with high cardinality but sparse actual usage. This helps avoid exceeding the PHP memory_limit.

Besides the reduced memory usage, I haven't seen much of a difference in how long buildPermutationTree takes to run. (To improve that, only the actually used metric label pairs could be to tracked separately to avoid building the permutations completely.)

if (isset($metaData['buckets'])) {
$metaData['buckets'][] = 'sum';
$labels[] = $metaData['buckets'];
$metaData['labelNames'][] = '__histogram_buckets';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I don't use the number of label names anymore, I removed that parameter from buildPermutationTree and removed this line as it seemed unused. I don't see any change in the output for histograms but if this actually used then I can re-add it.

Signed-off-by: Josh Hoffer <jhoffer@datava.com>
@mlebrun
Copy link

mlebrun commented Jul 27, 2023

This would be great to land, we are hitting the PHP limit as well.

@xocasdashdash
Copy link

I've tested a copy of this PR and it works well to reduce memory usage with histograms

Princemachiavelli and others added 3 commits September 14, 2024 19:37
Signed-off-by: Lukas Kämmerling <lukas.kaemmerling@hetzner-cloud.de>
Copy link
Member

@LKaemmerling LKaemmerling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you a lot! Great change! Sorry for the really long review time. I just fixed a few phpstan issues and will merge it when the tests are green :)!

@LKaemmerling LKaemmerling merged commit 3e4811f into PromPHP:main Mar 5, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants