io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.propagation.ApplicationTextMapPropagator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentelemetry-javaagent-opentelemetry-api-1.0 Show documentation
Show all versions of opentelemetry-javaagent-opentelemetry-api-1.0 Show documentation
Instrumentation of Java libraries using OpenTelemetry.
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.propagation;
import application.io.opentelemetry.context.Context;
import application.io.opentelemetry.context.propagation.TextMapGetter;
import application.io.opentelemetry.context.propagation.TextMapPropagator;
import application.io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage;
import java.util.Collection;
class ApplicationTextMapPropagator implements TextMapPropagator {
private final io.opentelemetry.context.propagation.TextMapPropagator agentTextMapPropagator;
ApplicationTextMapPropagator(
io.opentelemetry.context.propagation.TextMapPropagator agentTextMapPropagator) {
this.agentTextMapPropagator = agentTextMapPropagator;
}
@Override
public Collection fields() {
return agentTextMapPropagator.fields();
}
@Override
public Context extract(
Context applicationContext, C carrier, TextMapGetter applicationGetter) {
io.opentelemetry.context.Context agentContext =
AgentContextStorage.getAgentContext(applicationContext);
io.opentelemetry.context.Context agentUpdatedContext =
agentTextMapPropagator.extract(agentContext, carrier, new AgentGetter<>(applicationGetter));
if (agentUpdatedContext == agentContext) {
return applicationContext;
}
return AgentContextStorage.newContextWrapper(agentUpdatedContext, applicationContext);
}
@Override
public void inject(
Context applicationContext, C carrier, TextMapSetter applicationSetter) {
io.opentelemetry.context.Context agentContext =
AgentContextStorage.getAgentContext(applicationContext);
agentTextMapPropagator.inject(agentContext, carrier, new AgentSetter<>(applicationSetter));
}
private static class AgentGetter
implements io.opentelemetry.context.propagation.TextMapGetter {
private final TextMapGetter applicationGetter;
AgentGetter(TextMapGetter applicationGetter) {
this.applicationGetter = applicationGetter;
}
@Override
public Iterable keys(C c) {
return applicationGetter.keys(c);
}
@Override
public String get(C carrier, String key) {
return applicationGetter.get(carrier, key);
}
}
private static class AgentSetter
implements io.opentelemetry.context.propagation.TextMapSetter {
private final TextMapSetter applicationSetter;
AgentSetter(TextMapSetter applicationSetter) {
this.applicationSetter = applicationSetter;
}
@Override
public void set(C carrier, String key, String value) {
applicationSetter.set(carrier, key, value);
}
}
}