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

io.opentelemetry.context.propagation.MultiTextMapPropagator Maven / Gradle / Ivy

There is a newer version: 1.44.1
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.context.propagation;

import io.opentelemetry.context.Context;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;

final class MultiTextMapPropagator implements TextMapPropagator {
  private final TextMapPropagator[] textMapPropagators;
  private final Collection allFields;

  MultiTextMapPropagator(TextMapPropagator... textMapPropagators) {
    this(Arrays.asList(textMapPropagators));
  }

  MultiTextMapPropagator(List textMapPropagators) {
    this.textMapPropagators = new TextMapPropagator[textMapPropagators.size()];
    textMapPropagators.toArray(this.textMapPropagators);
    this.allFields = Collections.unmodifiableList(getAllFields(this.textMapPropagators));
  }

  @Override
  public Collection fields() {
    return allFields;
  }

  private static List getAllFields(TextMapPropagator[] textPropagators) {
    Set fields = new LinkedHashSet<>();
    for (TextMapPropagator textPropagator : textPropagators) {
      fields.addAll(textPropagator.fields());
    }

    return new ArrayList<>(fields);
  }

  @Override
  public  void inject(Context context, @Nullable C carrier, TextMapSetter setter) {
    if (context == null || setter == null) {
      return;
    }
    for (TextMapPropagator textPropagator : textMapPropagators) {
      textPropagator.inject(context, carrier, setter);
    }
  }

  @Override
  public  Context extract(Context context, @Nullable C carrier, TextMapGetter getter) {
    if (context == null) {
      return Context.root();
    }
    if (getter == null) {
      return context;
    }
    for (TextMapPropagator textPropagator : textMapPropagators) {
      context = textPropagator.extract(context, carrier, getter);
    }
    return context;
  }

  @Override
  public String toString() {
    return "MultiTextMapPropagator{textMapPropagators=" + Arrays.toString(textMapPropagators) + '}';
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy