Skip to content

Commit f1bdfdf

Browse files
authored
Fix npe in context bridging (#18444)
1 parent 752cd34 commit f1bdfdf

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

instrumentation/opentelemetry-api/opentelemetry-api-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/opentelemetryapi/context/AgentContextStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public static Context getAgentContext(
137137

138138
public static application.io.opentelemetry.context.Context toApplicationContext(
139139
Context agentContext) {
140-
return new AgentContextWrapper(agentContext);
140+
return AgentContextWrapper.getApplicationContext(agentContext);
141141
}
142142

143143
public static application.io.opentelemetry.context.Context newContextWrapper(

instrumentation/opentelemetry-api/opentelemetry-api-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/opentelemetryapi/context/AgentContextWrapper.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ final class AgentContextWrapper implements application.io.opentelemetry.context.
4646
final Context agentContext;
4747
final application.io.opentelemetry.context.Context applicationContext;
4848

49-
AgentContextWrapper(Context agentContext) {
50-
this(agentContext, agentContext.get(AgentContextStorage.APPLICATION_CONTEXT));
51-
}
52-
5349
AgentContextWrapper(
5450
Context agentContext, application.io.opentelemetry.context.Context applicationContext) {
5551
if (applicationContext instanceof AgentContextWrapper) {
@@ -59,17 +55,33 @@ final class AgentContextWrapper implements application.io.opentelemetry.context.
5955
this.applicationContext = applicationContext;
6056
}
6157

58+
public static application.io.opentelemetry.context.Context getApplicationContext(
59+
Context agentContext) {
60+
application.io.opentelemetry.context.Context rootApplicationContext =
61+
application.io.opentelemetry.context.Context.root();
62+
if (agentContext == Context.root()) {
63+
return rootApplicationContext;
64+
}
65+
66+
application.io.opentelemetry.context.Context applicationContext =
67+
agentContext.get(AgentContextStorage.APPLICATION_CONTEXT);
68+
// application context can be null when the agent context is converted to application context
69+
// from instrumented code e.g. instrumentation uses
70+
// AgentContextStorage.toApplicationContext(agentContext) to get application context but agent
71+
// context isn't really associated with application context
72+
if (applicationContext == null) {
73+
applicationContext = ((AgentContextWrapper) rootApplicationContext).applicationContext;
74+
}
75+
return new AgentContextWrapper(agentContext, applicationContext);
76+
}
77+
6278
Context toAgentContext() {
6379
if (agentContext.get(AgentContextStorage.APPLICATION_CONTEXT) == applicationContext) {
6480
return agentContext;
6581
}
6682
return agentContext.with(AgentContextStorage.APPLICATION_CONTEXT, applicationContext);
6783
}
6884

69-
public Context getAgentContext() {
70-
return agentContext;
71-
}
72-
7385
@Override
7486
public <V> V get(application.io.opentelemetry.context.ContextKey<V> key) {
7587
for (ContextKeyBridge<?, ?> bridge : CONTEXT_KEY_BRIDGES) {

instrumentation/opentelemetry-extension-kotlin-1.0/javaagent/src/test/kotlin/io/opentelemetry/javaagent/instrumentation/extensionkotlin/v1_0/ContextExtensionInstrumentationTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.opentelemetry.context.Context
99
import io.opentelemetry.context.ContextKey
1010
import io.opentelemetry.extension.kotlin.asContextElement
1111
import io.opentelemetry.extension.kotlin.getOpenTelemetryContext
12+
import kotlinx.coroutines.runBlocking
1213
import org.assertj.core.api.Assertions.assertThat
1314
import org.junit.jupiter.api.Test
1415

@@ -28,4 +29,12 @@ class ContextExtensionInstrumentationTest {
2829
// agent context
2930
assertThat(context1).isNotSameAs(context2)
3031
}
32+
33+
@Test
34+
fun `no context`() {
35+
runBlocking {
36+
assertThat(coroutineContext.getOpenTelemetryContext()).isEqualTo(Context.root())
37+
assertThat(coroutineContext.getOpenTelemetryContext().get(animalKey)).isNull()
38+
}
39+
}
3140
}

0 commit comments

Comments
 (0)