[Backport 7.71.x] [AI-6101] Add support for customizable cache keys for persistent cache #21351
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.
Backport fc7560f from #21316.
What does this PR do?
Original implementation
The implementation outlined here is the original idea when the PR was opened. After review, the implementation has changed quite a bit.
This PR adds support for customizable cache keys to be used with the agent persistent cache. This is implemented in the following way:
CacheKey
abstract class that can be extended to implement any cache invalidation logic needed.AgentCheck
has been modified to rely on aCacheKey
implementation to provide the key for the persistent cache internally. Usingcheck_id
is now the default behavior through theFullConfigCacheKey
implementation but can be overriden when necessary.logs_persistent_cache_key
can be overriden by a developer that wants to modify what is theCacheKey
used to handle the logs cursor.CacheKey
that is to simple, the impact of key clashing is reduced to the same kind of check.CacheKey
interface that work as an example and to provide funcionality:FullConfigCacheKey
is equivalent to the previous behavior. It relies on thecheck_id
that includes a digest of the full configuration of the check. This default behavior makes this change backward compatible with the usage ofsend_log
andget_log_cursor
, both of them using persistent cache.ConfigSetCacheKey
implementation invalidates the cache only when a subset of configuration options have been updated. This provides a similar level of separation between different checks but allowing some configuration options to not invalidate the cache.CacheKeyManager
allows multiple cache keys to coexist in theAgentCheck
for differnet purposes. At the moment we only use theCacheKey
internally to manage the log cursor. However, if we want to include something else we can register a new type ofCacheKeyType
enum and maintain the new one in the manager without having to modify how theAgentCheck
behaves.Note: the manager forces to have each cache key represented by an enum to avoid relying on simple string keys per cache key. While the intention is to allow to personalize the cache invalidation mechanism, we do not want potentially arbitrary cache keys being register which could trigger cache misses if the string key is mistakenly generated.
This PR adds support for customizing the persistent cache prefix used when caching any data in the
AgentCheck
. This is done by allowing the developer to override thepersistent_cache_id
method. This method returns the ID that identifies the specific check instance in the Agent persistent cache.When a given check returns the same persistent cache ID, the cache keys generated will be stable. By default, the persistent cache ID is the digest of the full check configuration. Any update in the configuration will generate a different ID, causing the cache to be invalidated. This is the current behavior before introducing this feature, which ensures backward compatibility.
Motivation
Until now, integrations that wanted to emit logs and be consistent between agent restarts would have logs missing or duplicated if the user changed their configuration. This would invalidate the cache and there was limited control over the last cursor.
The intention of this change is to provide a backward compatible solution that allows developers to modify the cache invalidation behavior in an easy way.
Review checklist (to be filled by reviewers)
qa/skip-qa
label if the PR doesn't need to be tested during QA.backport/<branch-name>
label to the PR and it will automatically open a backport PR once this one is merged