Skip to content

Commit 6a24044

Browse files
authored
Simplification: Context.root() is always empty (#8580)
We haven't needed a non-empty root context, and collapsing these concepts together simplifies the code.
1 parent fd04663 commit 6a24044

5 files changed

Lines changed: 3 additions & 41 deletions

File tree

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,13 @@
3636
* @see ContextKey
3737
*/
3838
public interface Context {
39-
/**
40-
* Returns the empty context.
41-
*
42-
* @return the context containing no values at all.
43-
*/
44-
static Context empty() {
45-
return EmptyContext.INSTANCE;
46-
}
47-
4839
/**
4940
* Returns the root context.
5041
*
5142
* @return the initial local context that all contexts extend.
5243
*/
5344
static Context root() {
54-
return manager().root();
45+
return EmptyContext.INSTANCE;
5546
}
5647

5748
/**

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,10 @@
22

33
/** Manages context across execution units. */
44
public interface ContextManager {
5-
/**
6-
* Returns the root context.
7-
*
8-
* @return the initial local context that all contexts extend.
9-
*/
10-
Context root();
11-
125
/**
136
* Returns the context attached to the current execution unit.
147
*
15-
* @return the attached context; {@link #root()} if there is none.
8+
* @return the attached context; {@link Context#root()} if there is none.
169
*/
1710
Context current();
1811

@@ -28,7 +21,7 @@ public interface ContextManager {
2821
* Swaps the given context with the one attached to current execution unit.
2922
*
3023
* @param context the context to swap.
31-
* @return the previously attached context; {@link #root()} if there was none.
24+
* @return the previously attached context; {@link Context#root()} if there was none.
3225
*/
3326
Context swap(Context context);
3427

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ final class ThreadLocalContextManager implements ContextManager {
55
private static final ThreadLocal<Context[]> CURRENT_HOLDER =
66
ThreadLocal.withInitial(() -> new Context[] {EmptyContext.INSTANCE});
77

8-
@Override
9-
public Context root() {
10-
return EmptyContext.INSTANCE;
11-
}
12-
138
@Override
149
public Context current() {
1510
return CURRENT_HOLDER.get()[0];

components/context/src/test/java/datadog/context/ContextProviderForkedTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ void testCustomManager() {
4242
// register a NOOP context manager
4343
ContextManager.register(
4444
new ContextManager() {
45-
@Override
46-
public Context root() {
47-
return EmptyContext.INSTANCE;
48-
}
49-
5045
@Override
5146
public Context current() {
5247
return root();

components/context/src/test/java/datadog/context/ContextTest.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package datadog.context;
22

3-
import static datadog.context.Context.empty;
43
import static datadog.context.Context.root;
54
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
65
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -22,17 +21,6 @@ class ContextTest {
2221
static final ContextKey<Float> FLOAT_KEY = ContextKey.named("float-key");
2322
static final ContextKey<Long> LONG_KEY = ContextKey.named("long-key");
2423

25-
@Test
26-
void testEmpty() {
27-
// Test empty is always the same
28-
Context empty = empty();
29-
assertEquals(empty, empty(), "Empty context should be consistent");
30-
// Test empty is not mutated
31-
String stringValue = "value";
32-
empty.with(STRING_KEY, stringValue);
33-
assertEquals(empty, empty(), "Empty context should be immutable");
34-
}
35-
3624
@Test
3725
void testRoot() {
3826
// Test root is always the same

0 commit comments

Comments
 (0)