fix: metric loss when a previously expired metric is re-added#11
Merged
fix: metric loss when a previously expired metric is re-added#11
Conversation
Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
… the index Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
Author
|
@coderabbitai pls review |
|
@shreemaan-abhishek why do we need this PR? can you tell me more about why we do this? seems relate to apache/apisix#11113 |
Author
|
@membphis yes, you are right. |
membphis
approved these changes
Feb 27, 2026
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.
Description
A worker-local cache (self.lookup and self.index) was used to optimize metric lookups. However, because ngx.shared.dict automatically evicts keys upon TTL expiration but the local Lua memory cache is not automatically synced to reflect this eviction, a drift occurs:
A metric key expires and is removed from the global ngx.shared.dict.
A worker still holds a reference to this metric in its local self.index cache.
When the worker tries to increment/update this metric, KeyIndex:add() sees the metric in self.index and skips adding it to the shared dictionary.
As a result, the metric is completely lost from the KeyIndex:list() scrape cycle, causing missing metrics in Prometheus.
Solution:
When a worker looks up a metric to increment/update it, if it has an expiration time set (self.exptime), we ensure the key index relationship is updated via self._key_index:add to put it back into the list.
regularly sweep the local index and clear out references to keys that have TTL-expired from the global dictionary. This prevents memory leaks in the Lua process and ensures that KeyIndex:list() doesn't iterate over thousands of dead keys.