Fix edge cases in custom metrics#3861
Merged
tillprochaska merged 1 commit intorelease/4.0.0from Sep 6, 2024
Merged
Conversation
This bug occurred due to a combination of two edge cases:
1. The Python Prometheus client does allow `None` as a label value, except when implementing a custom collector[1] which we are doing in this case. If you try to use `None` as a label value when implementing a custom collector, everything will work fine just until the metrics are actually exposed (for example when Prometheus sends a request to the `/metrics` endpoint) which will raise an unhandled exception.
2. In Aleph, every collection can be assigned zero or more countries and languages. These are stored as array columns in Postgres. For example, if a collection contains data about Germany and Luxembourg, that would be stored as `{de,lu}`. However, in real-life, we also have a few collections with `{NULL}` (instead of just an empty array `{}`). The same is the case for languages. That means we were trying to expose a Prometheus metrics such as `aleph_collection_countries{country=None}` which fails due to 1). I’m not entirely sure why this happens, but this seems to affect only collections that have been created quite some time ago, so it might be due to a bug in a previous version of Aleph.
I’ve adjusted the test cases to ensure that we try to expose the collected metrics in the Prometheus plaintext format (in the same way it would be done in production when requesting the `/metrics` endpoint) and fixed the metrics collector to ignore `NULL` values in the `countries` and `languages` columns.
[1]: prometheus/client_python#270
stchris
approved these changes
Sep 5, 2024
Contributor
stchris
left a comment
There was a problem hiding this comment.
What a surprising bug. Thanks for the explanation and the fix! 🙏
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This bug occurred due to a combination of two edge cases:
The Python Prometheus client does allow
Noneas a label value, except when implementing a custom collector1 which we are doing in this case. If you try to useNoneas a label value when implementing a custom collector, everything will work fine just until the metrics are actually exposed (for example when Prometheus sends a request to the/metricsendpoint) which will raise an unhandled exception.In Aleph, every collection can be assigned zero or more countries and languages. These are stored as array columns in Postgres. For example, if a collection contains data about Germany and Luxembourg, that would be stored as
{de,lu}. However, in real-life, we also have a few collections with{NULL}(instead of just an empty array{}). The same is the case for languages.That means we were trying to expose Prometheus metrics such as
aleph_collection_countries{country=None}which fails due to 1). I’m not entirely sure why we have{NULL}values, but this seems to affect only collections that have been created quite some time ago, so it might be due to a bug in a previous version of Aleph.I’ve adjusted the test cases to ensure that we try to expose the collected metrics in the Prometheus plaintext format (in the same way it would be done in production when requesting the
/metricsendpoint) and fixed the metrics collector to ignoreNULLvalues in thecountriesandlanguagescolumns.