Skip to content

Commit

Permalink
Allow context implementation to skip interim copy when adding two ent…
Browse files Browse the repository at this point in the history
…ries at the same time (such as adding span plus baggage)
  • Loading branch information
mcculls committed Jan 20, 2025
1 parent 171c10c commit 08762f9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 additions & 1 deletion components/context/src/main/java/datadog/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static Context detachFrom(Object carrier) {
/**
* Creates a copy of this context with the given key-value set.
*
* <p>Existing value with the given key will be replaced, and mapping to a {@code null} value will
* <p>Existing value with the given key will be replaced. Mapping to a {@code null} value will
* remove the key-value from the context copy.
*
* @param <T> the type of the value.
Expand All @@ -124,6 +124,28 @@ static Context detachFrom(Object carrier) {
*/
<T> Context with(ContextKey<T> key, @Nullable T value);

/**
* Creates a copy of this context with the given pair of key-values.
*
* <p>Existing values with the given keys will be replaced. Mapping to a {@code null} value will
* remove the key-value from the context copy.
*
* @param <T> the type of the first value.
* @param <U> the type of the second value.
* @param firstKey the first key to store the first value.
* @param firstValue the first value to store.
* @param secondKey the second key to store the second value.
* @param secondValue the second value to store.
* @return a new context with the pair of key-values set.
*/
default <T, U> Context with(
ContextKey<T> firstKey,
@Nullable T firstValue,
ContextKey<U> secondKey,
@Nullable U secondValue) {
return with(firstKey, firstValue).with(secondKey, secondValue);
}

/**
* Creates a copy of this context with the implicit key is mapped to the value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,6 @@ default <T> T get(ContextKey<T> key) {

@Override
default <T> Context with(ContextKey<T> key, @Nullable T value) {
return Context.root().with(SPAN_KEY, this).with(key, value);
return Context.root().with(SPAN_KEY, this, key, value);
}
}

0 comments on commit 08762f9

Please sign in to comment.