-
Notifications
You must be signed in to change notification settings - Fork 867
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
Make it possible to use InstrumentationContext (now VirtualField) fro… #4218
Merged
mateuszrzeszutek
merged 6 commits into
open-telemetry:main
from
mateuszrzeszutek:virtual-fields
Oct 1, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 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
61 changes: 61 additions & 0 deletions
61
...umentation-api/src/main/java/io/opentelemetry/instrumentation/api/field/VirtualField.java
This file contains 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,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.field; | ||
|
||
import io.opentelemetry.instrumentation.api.internal.RuntimeVirtualFieldSupplier; | ||
import java.util.function.Supplier; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
/** | ||
* Represents a "virtual" field of type {@code F} that is added to type {@code T} in the runtime. | ||
* | ||
* <p>A virtual field has similar semantics to a weak-keys strong-values map: the value will be | ||
* garbage collected when their owner instance is collected. It is discouraged to use a virtual | ||
* field for keeping values that might reference their key, as it may cause memory leaks. | ||
* | ||
* @param <T> The type that will contain the new virtual field. | ||
* @param <F> The field type that'll be added to {@code T}. | ||
*/ | ||
// we're using an abstract class here so that we can call static find() in pre-jdk8 advice classes | ||
public abstract class VirtualField<T, F> { | ||
|
||
/** | ||
* Finds a {@link VirtualField} instance for given {@code type} and {@code fieldType}. | ||
* | ||
* <p>Conceptually this can be thought of as a map lookup to fetch a second level map given {@code | ||
* type}. | ||
* | ||
* <p>In runtime, when using the javaagent, the <em>calls</em> to this method are rewritten to | ||
* something more performant while injecting advice into a method. | ||
* | ||
* <p>When using this method outside of Advice method, the {@link VirtualField} should be looked | ||
* up once and stored in a field to avoid repeatedly calling this method. | ||
* | ||
* @param type The type that will contain the new virtual field. | ||
* @param fieldType The field type that'll be added to {@code type}. | ||
*/ | ||
public static <U extends T, T, F> VirtualField<U, F> find(Class<T> type, Class<F> fieldType) { | ||
return RuntimeVirtualFieldSupplier.get().find(type, fieldType); | ||
} | ||
|
||
/** Gets the value of this virtual field. */ | ||
@Nullable | ||
public abstract F get(T object); | ||
|
||
/** Sets the new value of this virtual field. */ | ||
public abstract void set(T object, @Nullable F fieldValue); | ||
|
||
/** Sets the new value of this virtual field if the current value is {@code null}. */ | ||
public abstract void setIfNull(T object, F fieldValue); | ||
|
||
/** | ||
* Sets the new value of this virtual field if the current value is {@code null}. | ||
* | ||
* @return The old field value if it was present, or the result of evaluating passed {@code | ||
* fieldValueSupplier}. | ||
*/ | ||
public abstract F computeIfNull(T object, Supplier<F> fieldValueSupplier); | ||
} |
87 changes: 87 additions & 0 deletions
87
.../main/java/io/opentelemetry/instrumentation/api/internal/RuntimeVirtualFieldSupplier.java
This file contains 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,87 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.internal; | ||
|
||
import io.opentelemetry.instrumentation.api.caching.Cache; | ||
import io.opentelemetry.instrumentation.api.field.VirtualField; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Supplier; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public final class RuntimeVirtualFieldSupplier { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(RuntimeVirtualFieldSupplier.class); | ||
|
||
public interface VirtualFieldSupplier { | ||
<U extends T, T, F> VirtualField<U, F> find(Class<T> type, Class<F> fieldType); | ||
} | ||
|
||
private static final VirtualFieldSupplier DEFAULT = new CacheBasedVirtualFieldSupplier(); | ||
|
||
private static volatile VirtualFieldSupplier instance = DEFAULT; | ||
|
||
public static void set(VirtualFieldSupplier virtualFieldSupplier) { | ||
// only overwrite the default, cache-based supplier | ||
if (instance != DEFAULT) { | ||
logger.warn( | ||
"Runtime VirtualField supplier has already been set up, further set() calls are ignored"); | ||
return; | ||
} | ||
instance = virtualFieldSupplier; | ||
} | ||
|
||
public static VirtualFieldSupplier get() { | ||
return instance; | ||
} | ||
|
||
private static final class CacheBasedVirtualFieldSupplier | ||
extends ClassValue<Map<Class<?>, VirtualField<?, ?>>> implements VirtualFieldSupplier { | ||
|
||
@Override | ||
public <U extends T, T, F> VirtualField<U, F> find(Class<T> type, Class<F> fieldType) { | ||
return (VirtualField<U, F>) | ||
get(type).computeIfAbsent(fieldType, k -> new CacheBasedVirtualField<>()); | ||
} | ||
|
||
@Override | ||
protected Map<Class<?>, VirtualField<?, ?>> computeValue(Class<?> type) { | ||
return new ConcurrentHashMap<>(); | ||
} | ||
} | ||
|
||
private static final class CacheBasedVirtualField<T, F> extends VirtualField<T, F> { | ||
private final Cache<T, F> cache = Cache.newBuilder().setWeakKeys().build(); | ||
|
||
@Override | ||
public @Nullable F get(T object) { | ||
return cache.get(object); | ||
} | ||
|
||
@Override | ||
public void set(T object, @Nullable F fieldValue) { | ||
if (fieldValue == null) { | ||
cache.remove(object); | ||
} else { | ||
cache.put(object, fieldValue); | ||
} | ||
} | ||
|
||
@Override | ||
public void setIfNull(T object, F fieldValue) { | ||
cache.computeIfAbsent(object, k -> fieldValue); | ||
} | ||
|
||
@Override | ||
public F computeIfNull(T object, Supplier<F> fieldValueSupplier) { | ||
return cache.computeIfAbsent(object, k -> fieldValueSupplier.get()); | ||
} | ||
} | ||
|
||
private RuntimeVirtualFieldSupplier() {} | ||
} |
This file contains 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 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 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 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 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 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.
I ❤️ reducing
ContextStore
andInstrumentationContext
down to just one conceptVirtualField
(and that doesn't have the word Context in it)