-
Notifications
You must be signed in to change notification settings - Fork 413
fix: normalize label values once, when a label combination is first stored #798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
milcho0604
wants to merge
1
commit into
prometheus:main
Choose a base branch
from
milcho0604:fix/normalize-labels-in-labelmap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -168,6 +168,72 @@ exports.nowTimestamp = function nowTimestamp() { | |
| * @property labels {object} | ||
| */ | ||
|
|
||
| /** | ||
| * Copy the labels the store is about to take ownership of, coercing | ||
| * non-nullish values to the string Prometheus expects. Coercion uses | ||
| * template interpolation — exactly what rendering would have done later — | ||
| * so the rendered form is unchanged while escaping no longer gets | ||
| * skipped (#791). | ||
| * | ||
| * The copy is unconditional: the entry keeps these labels for its lifetime, | ||
| * so holding on to an object the caller can still mutate would let a later | ||
| * mutation change what a stored series reports. | ||
| * | ||
| * Nullish values are copied as-is: `keyFrom()` treats them as absent, so | ||
| * coercing them to `"null"`/`"undefined"` would make the stored labels | ||
| * compute a different key than the one they are stored under — breaking | ||
| * `remove(entry.labels)` round-trips (Summary's pruning does exactly that) | ||
| * and collapsing `{a: null}` with `{a: 'null'}` after serialization. Their | ||
| * rendered form (`"null"`) contains nothing that needs escaping anyway. | ||
| * | ||
| * Two corner cases are deliberately unsupported (each needs another | ||
| * `for...in`-visible property present — alone, `isEmpty()` keeps original | ||
| * and copy agreed on `''`): | ||
| * - `Object.create(null)` labels missing a declared name that collides with | ||
| * `Object.prototype` (`constructor`, …): the plain copy reads the | ||
| * inherited member where the original read `undefined`. | ||
| * - Non-enumerable label properties: no enumeration sees them, so the copy | ||
| * drops what `keyFrom()` read by property access. | ||
| * Symbols are not label data: `keyFrom()` iterates declared names, | ||
| * exposition uses `Object.entries()`; neither reads them. | ||
| * @param {object} labels | ||
| * @returns {object} a copy owned by the store | ||
| */ | ||
| function normalizeLabels(labels) { | ||
| // Spread first, for the packed object shape V8 gives a clone: building the | ||
| // copy up key by key instead costs about 24 bytes per stored series. | ||
| const copy = { ...labels }; | ||
|
|
||
| // Then walk the source with `for...in`, which picks up inherited enumerable | ||
| // labels too. `keyFrom()` reads labels by name, so it sees those as well, and | ||
| // a copy that dropped them could not reproduce the key its entry is filed | ||
| // under — `remove(entry.labels)`, which Summary's pruning uses, would quietly | ||
| // miss. Non-enumerable labels stay out of reach of any enumeration; they only | ||
| // survived before because the store kept the caller's object. | ||
| for (const name in labels) { | ||
| const value = labels[name]; | ||
| const stored = | ||
| typeof value === 'string' || value === null || value === undefined | ||
| ? value | ||
| : `${value}`; | ||
|
|
||
| if (name === '__proto__') { | ||
| // A legal label name, but assigning it would invoke the prototype | ||
| // setter instead of defining a property, dropping the label. | ||
| Object.defineProperty(copy, name, { | ||
| value: stored, | ||
| writable: true, | ||
| enumerable: true, | ||
| configurable: true, | ||
| }); | ||
| } else { | ||
| copy[name] = stored; | ||
| } | ||
| } | ||
|
|
||
| return copy; | ||
| } | ||
|
|
||
| /** | ||
| * Lookup table for stats by labels. | ||
| */ | ||
|
|
@@ -182,6 +248,22 @@ class LabelMap { | |
| this.#labelNames = new Set(labelNames.slice().sort()); | ||
| } | ||
|
|
||
| /** | ||
| * The single insertion point — every new label combination enters the map | ||
| * here, and takes its own copy of the labels on the way in. Only a | ||
| * combination's first record reaches this method, so the recording fast | ||
| * path for existing combinations copies nothing. | ||
| * @param {string} key precomputed `keyFrom(entry.labels)` | ||
| * @param {StatsEntry} entry | ||
| * @returns {StatsEntry} | ||
| */ | ||
| #insert(key, entry) { | ||
| entry.labels = normalizeLabels(entry.labels); | ||
| this.#map.set(key, entry); | ||
|
|
||
| return entry; | ||
| } | ||
|
|
||
| /** | ||
| * @function setValue | ||
| * @param {object} labels | ||
|
|
@@ -195,7 +277,7 @@ class LabelMap { | |
| if (entry !== undefined) { | ||
| entry.value = value; | ||
| } else { | ||
| this.#map.set(key, { value, labels }); | ||
| this.#insert(key, { value, labels }); | ||
| } | ||
|
|
||
| return this; | ||
|
|
@@ -214,7 +296,7 @@ class LabelMap { | |
| if (entry !== undefined) { | ||
| entry.value += value; | ||
| } else { | ||
| this.#map.set(key, { value, labels }); | ||
| this.#insert(key, { value, labels }); | ||
| } | ||
|
|
||
| return this; | ||
|
|
@@ -244,8 +326,7 @@ class LabelMap { | |
| let entry = this.#map.get(key); | ||
|
|
||
| if (entry === undefined) { | ||
| entry = { value: init(), labels }; | ||
| this.#map.set(key, entry); | ||
| entry = this.#insert(key, { value: init(), labels }); | ||
| } | ||
|
|
||
| return entry.value; | ||
|
|
@@ -273,10 +354,11 @@ class LabelMap { | |
|
|
||
| let entry = this.#map.get(key); | ||
| if (entry !== undefined) { | ||
| Object.assign(entry, values, { labels }); | ||
| // Keep the stored labels: they were copied on first insertion and | ||
| // identify the same combination (same key). | ||
| Object.assign(entry, values, { labels: entry.labels }); | ||
| } else { | ||
| entry = { ...values, labels }; | ||
| this.#map.set(key, entry); | ||
| entry = this.#insert(key, { ...values, labels }); | ||
| } | ||
|
|
||
| return entry; | ||
|
|
@@ -400,6 +482,13 @@ class LabelGrouper { | |
|
|
||
| /** | ||
| * Adds the `value` to the `key`'s array of values. | ||
| * | ||
| * NB: no label normalization here, by design. Aggregation input comes from | ||
| * `registry.getMetricsAsJSON()`, whose store-backed labels were already | ||
| * normalized by LabelMap on first insertion — re-checking every value on | ||
| * this path would tax `aggregate()` for work the stores already did. | ||
| * Labels that never pass through the stores (custom collector results, | ||
| * registry default labels) arrive here as-is, unchanged from before. | ||
|
Comment on lines
+486
to
+491
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| * @param {StatsEntry} value Value to add to `key`'s array. | ||
| * @returns {LabelGrouper} undefined. | ||
| */ | ||
|
|
||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.