Skip to content

Conversation

@software-artificer
Copy link
Contributor

APCNg is crashing if the label value provided wasn't a string, but something that can be coerced to string (such as int). Other adaptors work properly in this situation.

The problem occurs in two different places:

  • when a metric is emitted, the storeLabelKeys() calls into addItemToKey() which has its second parameter type hinted as a string and throws a type error if anything else is passed. This results in partially stored state;
  • when trying to scrape metrics with partially stored state, the APCng::collect() will try to build all the permutations and expect all the key-value pairs for labels to exist, but numeric label values aren't persisted and so it will cause the Undefined array key error as reported in APCng crashing when rendering with integer labels  #154;

This change ensures that label values are cast to the string type before encoding them and using as APC keys.

An alternative that can be considered is adding a label check / cast at the individual collector layers. Given other adaptors work properly – this solution seemed fitting. Introducing a check into the collectors layer will cause a breaking change, so probably isn't the best option.

Fixes #154

Garry Filakhtov added 2 commits September 3, 2024 18:30
APCNg is crashing if the label value provided wasn't a string, but something that can be coerced to string (such as int).

The problem occurs in two different places:
- when a metric is emitted, the `storeLabelKeys()` calls into `addItemToKey()` which has its second parameter type hinted as a `string` and throws a type error if anything else is passed. This results in partially stored state;
- when trying to scrape metrics with partially stored state, the `APCng::collect()` will try to build all the permutations and expect all the key-value pairs for labels to exist, but numeric label values aren't persisted and so it will cause the `Undefined array key` error as reported in #154;

This change ensures that label values are cast to the string type before encoding them and using as APC keys.

Signed-off-by: Garry Filakhtov <garry.filakhtov@bigcommerce.com>
Signed-off-by: Garry Filakhtov <garry.filakhtov@bigcommerce.com>
@software-artificer
Copy link
Contributor Author

Fixed the PHPStan failures.

@LKaemmerling LKaemmerling merged commit 50b70a6 into PromPHP:main Oct 18, 2024
@LKaemmerling
Copy link
Member

Thank you! Good catch!

@software-artificer software-artificer deleted the force-string-labels branch October 18, 2024 08:12
@software-artificer software-artificer restored the force-string-labels branch October 20, 2024 22:46
@software-artificer software-artificer deleted the force-string-labels branch January 9, 2025 02:33
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.

APCng crashing when rendering with integer labels

2 participants