-
Notifications
You must be signed in to change notification settings - Fork 1k
[Spring starter] Make sdk bean overridable and cache env calls #15132
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
jaydeluca
wants to merge
5
commits into
open-telemetry:main
Choose a base branch
from
jaydeluca:spring-cache-config
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.
+238
−14
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9d975b9
add bean override and cache for spring environment calls
jaydeluca 39757e1
use helper methods
jaydeluca 5fa3e39
implement cached property resolver
jaydeluca 77e4485
rename environment var
jaydeluca 4dd742c
revert ConditionalOnMissingBean
jaydeluca 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
97 changes: 97 additions & 0 deletions
97
...etry/instrumentation/spring/autoconfigure/internal/properties/CachedPropertyResolver.java
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 |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import javax.annotation.Nullable; | ||
| import org.springframework.core.env.Environment; | ||
|
|
||
| /** | ||
| * A caching wrapper around Spring's {@link Environment} that caches property lookups to avoid | ||
| * repeated expensive operations and property source traversal. | ||
| * | ||
| * <p>Thread-safe for concurrent access. Cached values persist indefinitely and are assumed to be | ||
| * immutable after the first access. If properties can change at runtime, use {@link #clear()} to | ||
| * invalidate the cache. | ||
| * | ||
| * <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
| * at any time. | ||
| */ | ||
| final class CachedPropertyResolver { | ||
| private final Environment environment; | ||
| private final ConcurrentHashMap<CacheKey, Optional<?>> cache = new ConcurrentHashMap<>(); | ||
|
|
||
| CachedPropertyResolver(Environment environment) { | ||
| this.environment = Objects.requireNonNull(environment, "environment"); | ||
| } | ||
|
|
||
| /** | ||
| * Gets a property value from the environment, using a cache to avoid repeated lookups. | ||
| * | ||
| * @param name the property name | ||
| * @param targetType the target type to convert to | ||
| * @return the property value, or null if not found | ||
| */ | ||
| @Nullable | ||
| <T> T getProperty(String name, Class<T> targetType) { | ||
| CacheKey key = new CacheKey(name, targetType); | ||
| // CacheKey includes targetType, ensuring type match | ||
| @SuppressWarnings("unchecked") | ||
| Optional<T> result = | ||
| (Optional<T>) | ||
| cache.computeIfAbsent( | ||
| key, k -> Optional.ofNullable(environment.getProperty(name, targetType))); | ||
| return result.orElse(null); | ||
| } | ||
|
|
||
| /** | ||
| * Gets a string property value from the environment. | ||
| * | ||
| * @param name the property name | ||
| * @return the property value, or null if not found | ||
| */ | ||
| @Nullable | ||
| String getProperty(String name) { | ||
| return getProperty(name, String.class); | ||
| } | ||
|
|
||
| /** Clears all cached property values, forcing fresh lookups on subsequent calls. */ | ||
| void clear() { | ||
| cache.clear(); | ||
| } | ||
|
|
||
| /** Cache key combining property name and target type. */ | ||
| private static final class CacheKey { | ||
| private final String name; | ||
| private final Class<?> targetType; | ||
| private final int cachedHashCode; | ||
|
|
||
| CacheKey(String name, Class<?> targetType) { | ||
| this.name = Objects.requireNonNull(name, "name"); | ||
| this.targetType = Objects.requireNonNull(targetType, "targetType"); | ||
| this.cachedHashCode = Objects.hash(name, targetType); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) { | ||
| return true; | ||
| } | ||
| if (!(obj instanceof CacheKey)) { | ||
| return false; | ||
| } | ||
| CacheKey other = (CacheKey) obj; | ||
| return name.equals(other.name) && targetType.equals(other.targetType); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return cachedHashCode; | ||
| } | ||
| } | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative approach that could be considered would be to wrap the
environmentinto a cachingPropertyResolver. I presume that the sdk deliberately rereads these properties so they could be updated with opamp in the future. Idk whether this is something we should be concerned with.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, it was not a deliberate decision as far as I know.
When I worked on the project, I assumed that properties would only be read at startup time.
I think it's hard to anticipate how OpAMP will be integrated, but we may simply flush the cache (from the caching
PropertyResolver.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
implemented a caching property resolver, and included a way to flush the cache