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

brave.opentracing.TextMapPropagation Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
/*
 * Copyright 2016-2020 The OpenZipkin Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package brave.opentracing;

import brave.Span.Kind;
import brave.propagation.Propagation;
import brave.propagation.Propagation.Getter;
import brave.propagation.Propagation.RemoteSetter;
import brave.propagation.Propagation.Setter;
import brave.propagation.TraceContext.Extractor;
import brave.propagation.TraceContextOrSamplingFlags;
import io.opentracing.propagation.TextMapExtract;
import io.opentracing.propagation.TextMapInject;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

final class TextMapPropagation {
  static final Setter SETTER = new Setter() {
    @Override public void put(TextMapInject request, String key, String value) {
      request.put(key, value);
    }

    @Override public String toString() {
      return "TextMapInject::put";
    }
  };

  enum REMOTE_SETTER implements RemoteSetter {
    CLIENT() {
      @Override public Kind spanKind() {
        return Kind.CLIENT;
      }
    },
    PRODUCER() {
      @Override public Kind spanKind() {
        return Kind.PRODUCER;
      }
    },
    CONSUMER() {
      @Override public Kind spanKind() {
        return Kind.CONSUMER;
      }
    };

    @Override public void put(TextMapInject request, String key, String value) {
      SETTER.put(request, key, value);
    }

    @Override public String toString() {
      return SETTER.toString();
    }
  }

  static final Getter, String> GETTER =
      new Getter, String>() {
        @Override public String get(Map carrier, String key) {
          return carrier.get(key.toLowerCase(Locale.ROOT));
        }

        @Override public String toString() {
          return "Map::getLowerCase";
        }
      };

  /**
   * Even though TextMap is named like Map, it doesn't have a retrieve-by-key method.
   *
   * 

See https://github.com/opentracing/opentracing-java/issues/305 */ static final class TextMapExtractor implements Extractor { final Set allNames; final Extractor> delegate; TextMapExtractor( Propagation propagation, Set allNames, Getter, String> getter) { this.allNames = allNames; this.delegate = propagation.extractor(getter); } /** Performs case-insensitive lookup */ @Override public TraceContextOrSamplingFlags extract(TextMapExtract entries) { Map cache = new LinkedHashMap<>(); for (Iterator> it = entries.iterator(); it.hasNext(); ) { Map.Entry next = it.next(); String inputKey = next.getKey().toLowerCase(Locale.ROOT); if (allNames.contains(inputKey)) { cache.put(inputKey, next.getValue()); } } return delegate.extract(cache); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy