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

io.opentelemetry.api.incubator.propagation.ExtendedContextPropagators Maven / Gradle / Ivy

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

package io.opentelemetry.api.incubator.propagation;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapGetter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;

/**
 * Utility class to simplify context propagation.
 *
 * 

The README * explains the use cases in more detail. */ public final class ExtendedContextPropagators { private ExtendedContextPropagators() {} private static final TextMapGetter> TEXT_MAP_GETTER = new TextMapGetter>() { @Override public Set keys(Map carrier) { return carrier.keySet(); } @Override @Nullable public String get(@Nullable Map carrier, String key) { return carrier == null ? null : carrier.get(key); } }; /** * Injects the current context into a string map, which can then be added to HTTP headers or the * metadata of a message. * * @param propagators provide the propagators from {@link OpenTelemetry#getPropagators()} */ public static Map getTextMapPropagationContext(ContextPropagators propagators) { Map carrier = new HashMap<>(); propagators .getTextMapPropagator() .inject( Context.current(), carrier, (map, key, value) -> { if (map != null) { map.put(key, value); } }); return Collections.unmodifiableMap(carrier); } /** * Extract the context from a string map, which you get from HTTP headers of the metadata of a * message you're processing. * * @param carrier the string map * @param propagators provide the propagators from {@link OpenTelemetry#getPropagators()} */ public static Context extractTextMapPropagationContext( Map carrier, ContextPropagators propagators) { Context current = Context.current(); if (carrier == null) { return current; } CaseInsensitiveMap caseInsensitiveMap = new CaseInsensitiveMap(carrier); return propagators.getTextMapPropagator().extract(current, caseInsensitiveMap, TEXT_MAP_GETTER); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy