Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static Context getAgentContext(

public static application.io.opentelemetry.context.Context toApplicationContext(
Context agentContext) {
return new AgentContextWrapper(agentContext);
return AgentContextWrapper.getApplicationContext(agentContext);
}

public static application.io.opentelemetry.context.Context newContextWrapper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ final class AgentContextWrapper implements application.io.opentelemetry.context.
final Context agentContext;
final application.io.opentelemetry.context.Context applicationContext;

AgentContextWrapper(Context agentContext) {
this(agentContext, agentContext.get(AgentContextStorage.APPLICATION_CONTEXT));
}

AgentContextWrapper(
Context agentContext, application.io.opentelemetry.context.Context applicationContext) {
if (applicationContext instanceof AgentContextWrapper) {
Expand All @@ -59,17 +55,33 @@ final class AgentContextWrapper implements application.io.opentelemetry.context.
this.applicationContext = applicationContext;
}

public static application.io.opentelemetry.context.Context getApplicationContext(
Context agentContext) {
application.io.opentelemetry.context.Context rootApplicationContext =
application.io.opentelemetry.context.Context.root();
if (agentContext == Context.root()) {
return rootApplicationContext;
}

application.io.opentelemetry.context.Context applicationContext =
agentContext.get(AgentContextStorage.APPLICATION_CONTEXT);
// application context can be null when the agent context is converted to application context
// from instrumented code e.g. instrumentation uses
// AgentContextStorage.toApplicationContext(agentContext) to get application context but agent
// context isn't really associated with application context
if (applicationContext == null) {
applicationContext = ((AgentContextWrapper) rootApplicationContext).applicationContext;
}
return new AgentContextWrapper(agentContext, applicationContext);
}

Context toAgentContext() {
if (agentContext.get(AgentContextStorage.APPLICATION_CONTEXT) == applicationContext) {
return agentContext;
}
return agentContext.with(AgentContextStorage.APPLICATION_CONTEXT, applicationContext);
}

public Context getAgentContext() {
return agentContext;
}

@Override
public <V> V get(application.io.opentelemetry.context.ContextKey<V> key) {
for (ContextKeyBridge<?, ?> bridge : CONTEXT_KEY_BRIDGES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.opentelemetry.context.Context
import io.opentelemetry.context.ContextKey
import io.opentelemetry.extension.kotlin.asContextElement
import io.opentelemetry.extension.kotlin.getOpenTelemetryContext
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

Expand All @@ -28,4 +29,12 @@ class ContextExtensionInstrumentationTest {
// agent context
assertThat(context1).isNotSameAs(context2)
}

@Test
fun `no context`() {
runBlocking {
assertThat(coroutineContext.getOpenTelemetryContext()).isEqualTo(Context.root())
assertThat(coroutineContext.getOpenTelemetryContext().get(animalKey)).isNull()
}
}
}
Loading