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

io.opentelemetry.opencensusshim.OpenTelemetryTextFormatImpl Maven / Gradle / Ivy

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

package io.opentelemetry.opencensusshim;

import static io.opentelemetry.opencensusshim.SpanConverter.mapSpanContext;

import io.opencensus.trace.SpanContext;
import io.opencensus.trace.propagation.TextFormat;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapPropagator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;

final class OpenTelemetryTextFormatImpl extends TextFormat {

  private final TextMapPropagator propagator;

  OpenTelemetryTextFormatImpl(TextMapPropagator propagator) {
    this.propagator = propagator;
  }

  @Override
  public List fields() {
    return new ArrayList<>(propagator.fields());
  }

  @Override
  public  void inject(SpanContext spanContext, C carrier, Setter setter) {
    io.opentelemetry.api.trace.SpanContext otelSpanContext = mapSpanContext(spanContext);
    Context otelContext = Context.current().with(Span.wrap(otelSpanContext));
    // Use explicit lambda instead of method reference for nullness check.
    propagator.inject(otelContext, carrier, (c, key, value) -> setter.put(c, key, value));
  }

  @Override
  public  SpanContext extract(C carrier, Getter getter) {
    Context context =
        propagator.extract(
            Context.current(),
            carrier,
            new TextMapGetter() {
              // OC Getter cannot return keys for an object, but users should not need it either.
              @Override
              public Iterable keys(C carrier) {
                return Collections.emptyList();
              }

              @Nullable
              @Override
              public String get(@Nullable C carrier, String key) {
                return getter.get(carrier, key);
              }
            });
    io.opentelemetry.api.trace.SpanContext spanContext = Span.fromContext(context).getSpanContext();
    return mapSpanContext(spanContext);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy