-
Notifications
You must be signed in to change notification settings - Fork 292
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
Introduce context helpers API #8134
Open
PerfectSlayer
wants to merge
1
commit into
master
Choose a base branch
from
bbujon/context-helper
base: master
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.
+407
−3
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
165 changes: 165 additions & 0 deletions
165
components/context/src/main/java/datadog/context/ContextHelpers.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,165 @@ | ||
package datadog.context; | ||
|
||
import static java.lang.Math.max; | ||
import static java.util.Arrays.copyOfRange; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.BinaryOperator; | ||
|
||
/** | ||
* Static helpers to manipulate context collections. | ||
* | ||
* <p>Typical usages include: | ||
* | ||
* <pre>{@code | ||
* // Finding a context value from multiple sources: | ||
* Span span = findFirst(spanKey, message, request, CURRENT) | ||
* // Find all context values from different sources: | ||
* List<Error> errors = findAll(errorKey, message, request, CURRENT) | ||
* // Capture multiple contexts in a single one: | ||
* Context aggregate = combine(message, request, CURRENT) | ||
* // Combine multiple contexts into a single one using custom merge rules: | ||
* Context combined = combine( | ||
* (current, next) -> { | ||
* var metric = current.get(metricKey); | ||
* var nextMetric = next.get(metricKey); | ||
* return current.with(metricKey, metric.add(nextMetric)); | ||
* }, message, request, CURRENT); | ||
* }</pre> | ||
* | ||
* where {@link #CURRENT} denotes a carrier with the current context. | ||
*/ | ||
public final class ContextHelpers { | ||
/** A helper object carrying the {@link Context#current()} context. */ | ||
public static final Object CURRENT = new Object(); | ||
|
||
private ContextHelpers() {} | ||
|
||
/** | ||
* Find the first context value from given context carriers. | ||
* | ||
* @param key The key used to store the value. | ||
* @param carriers The carrier to get context and value from. | ||
* @param <T> The type of the value to look for. | ||
* @return The first context value found, {@code null} if not found. | ||
*/ | ||
public static <T> T findFirst(ContextKey<T> key, Object... carriers) { | ||
requireNonNull(key, "key cannot be null"); | ||
for (Object carrier : carriers) { | ||
requireNonNull(carrier, "carrier cannot be null"); | ||
Context context = carrier == CURRENT ? Context.current() : Context.from(carrier); | ||
T value = context.get(key); | ||
if (value != null) { | ||
return value; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Find all the context values from the given context carriers. | ||
* | ||
* @param key The key used to store the value. | ||
* @param carriers The carriers to get context and value from. | ||
* @param <T> The type of the values to look for. | ||
* @return A list of all values found, in context order. | ||
*/ | ||
public static <T> List<T> findAll(ContextKey<T> key, Object... carriers) { | ||
requireNonNull(key, "key cannot be null"); | ||
List<T> values = new ArrayList<>(carriers.length); | ||
for (Object carrier : carriers) { | ||
requireNonNull(carrier, "carrier cannot be null"); | ||
Context context = carrier == CURRENT ? Context.current() : Context.from(carrier); | ||
T value = context.get(key); | ||
if (value != null) { | ||
values.add(value); | ||
} | ||
} | ||
return values; | ||
} | ||
|
||
/** | ||
* Combine contexts and their values, keeping the first founds. | ||
* | ||
* @param contexts The contexts to combine. | ||
* @return A context containing all the values from all the given context, keeping the first value | ||
* found for a given key. | ||
*/ | ||
public static Context combine(Context... contexts) { | ||
return combine(ContextHelpers::combineKeepingFirst, contexts); | ||
} | ||
|
||
/** | ||
* Combine multiple contexts into a single one. | ||
* | ||
* @param combiner The context combiner, taking already combined context as first parameter, any | ||
* following one as second parameter, and returning the combined context. | ||
* @param contexts The contexts to combine. | ||
* @return The combined context. | ||
*/ | ||
public static Context combine(BinaryOperator<Context> combiner, Context... contexts) { | ||
requireNonNull(combiner, "combiner cannot be null"); | ||
Context result = new IndexedContext(new Object[0]); | ||
for (Context context : contexts) { | ||
requireNonNull(context, "context cannot be null"); | ||
result = combiner.apply(result, context); | ||
} | ||
return result; | ||
} | ||
|
||
private static Context combineKeepingFirst(Context current, Context next) { | ||
if (!(current instanceof IndexedContext)) { | ||
throw new IllegalStateException("Left context is supposed to be an IndexedContext"); | ||
} | ||
IndexedContext currentIndexed = (IndexedContext) current; | ||
if (next instanceof EmptyContext) { | ||
return current; | ||
} else if (next instanceof SingletonContext) { | ||
SingletonContext nextSingleton = (SingletonContext) next; | ||
// Check if the single next value is already define in current so next context can be skipped | ||
if (nextSingleton.index < currentIndexed.store.length | ||
&& currentIndexed.store[nextSingleton.index] != null) { | ||
return current; | ||
} | ||
// Always store next value otherwise | ||
Object[] store = | ||
copyOfRange( | ||
currentIndexed.store, 0, max(currentIndexed.store.length, nextSingleton.index + 1)); | ||
store[nextSingleton.index] = nextSingleton.value; | ||
return new IndexedContext(store); | ||
} else if (next instanceof IndexedContext) { | ||
IndexedContext nextIndexed = (IndexedContext) next; | ||
// Don't prematurely allocate store. Only allocate if: | ||
// * nextIndexed has more values that currentIndexed, | ||
// so the additional values will always be kept | ||
// * nextIndexed has values that currentIndexed do not have | ||
Object[] store = null; | ||
// Allocate store if nextIndexed has more elements than currentIndexed | ||
if (nextIndexed.store.length > currentIndexed.store.length) { | ||
store = copyOfRange(currentIndexed.store, 0, nextIndexed.store.length); | ||
} | ||
// Apply nextIndexed values if not set in currentIndexed | ||
for (int i = 0; i < currentIndexed.store.length; i++) { | ||
Object nextValue = nextIndexed.store[i]; | ||
if (nextValue != null && currentIndexed.store[i] == null) { | ||
if (store == null) { | ||
store = copyOfRange(currentIndexed.store, 0, currentIndexed.store.length); | ||
} | ||
store[i] = nextValue; | ||
} | ||
} | ||
// Apply any additional values from nextIndexed if any | ||
for (int i = currentIndexed.store.length; i < nextIndexed.store.length; i++) { | ||
Object nextValue = nextIndexed.store[i]; | ||
if (nextValue != null) { | ||
store[i] = nextValue; | ||
} | ||
} | ||
// If store was not allocated, no value from nextIndexed was taken | ||
return store == null ? current : new IndexedContext(store); | ||
} | ||
throw new IllegalStateException("Unsupported context type: " + next.getClass().getName()); | ||
} | ||
} |
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.
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.
Minor improvement: we can save an array allocation if we check whether we have a value for the index first: