All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.propagation.ApplicationTextMapPropagator Maven / Gradle / Ivy

There is a newer version: 2.8.0-alpha
Show newest version
/*
 * 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);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy