Skip to content

Commit 332d71c

Browse files
committed
Allow context implementation to skip interim copy when adding two entries at the same time (such as adding span plus baggage)
1 parent e7d0ba0 commit 332d71c

File tree

2 files changed

+24
-2
lines changed
  • components/context/src/main/java/datadog/context
  • internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api

2 files changed

+24
-2
lines changed

components/context/src/main/java/datadog/context/Context.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static Context detachFrom(Object carrier) {
114114
/**
115115
* Creates a copy of this context with the given key-value set.
116116
*
117-
* <p>Existing value with the given key will be replaced, and mapping to a {@code null} value will
117+
* <p>Existing value with the given key will be replaced. Mapping to a {@code null} value will
118118
* remove the key-value from the context copy.
119119
*
120120
* @param <T> the type of the value.
@@ -124,6 +124,28 @@ static Context detachFrom(Object carrier) {
124124
*/
125125
<T> Context with(ContextKey<T> key, @Nullable T value);
126126

127+
/**
128+
* Creates a copy of this context with the given pair of key-values.
129+
*
130+
* <p>Existing values with the given keys will be replaced. Mapping to a {@code null} value will
131+
* remove the key-value from the context copy.
132+
*
133+
* @param <T> the type of the first value.
134+
* @param <U> the type of the second value.
135+
* @param firstKey the first key to store the first value.
136+
* @param firstValue the first value to store.
137+
* @param secondKey the second key to store the second value.
138+
* @param secondValue the second value to store.
139+
* @return a new context with the pair of key-values set.
140+
*/
141+
default <T, U> Context with(
142+
ContextKey<T> firstKey,
143+
@Nullable T firstValue,
144+
ContextKey<U> secondKey,
145+
@Nullable U secondValue) {
146+
return with(firstKey, firstValue).with(secondKey, secondValue);
147+
}
148+
127149
/**
128150
* Creates a copy of this context with the implicit key is mapped to the value.
129151
*

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/AgentSpan.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,6 @@ default <T> T get(ContextKey<T> key) {
167167

168168
@Override
169169
default <T> Context with(ContextKey<T> key, @Nullable T value) {
170-
return Context.root().with(SPAN_KEY, this).with(key, value);
170+
return Context.root().with(SPAN_KEY, this, key, value);
171171
}
172172
}

0 commit comments

Comments
 (0)