|
| 1 | +package datadog.context.propagation; |
| 2 | + |
| 3 | +import datadog.context.Context; |
| 4 | +import datadog.context.InferredProxyContext; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.function.BiConsumer; |
| 8 | + |
| 9 | +public class InferredProxyPropagator implements Propagator { |
| 10 | + public static final String INFERRED_PROXY_KEY = "x-dd-proxy"; |
| 11 | + /** |
| 12 | + * METHOD STUB: InferredProxy is currently not meant to be injected to downstream services Injects |
| 13 | + * a context into a downstream service using the given carrier. |
| 14 | + * |
| 15 | + * @param context the context containing the values to be injected. |
| 16 | + * @param carrier the instance that will receive the key/value pairs to propagate. |
| 17 | + * @param setter the callback to set key/value pairs into the carrier. |
| 18 | + */ |
| 19 | + @Override |
| 20 | + public <C> void inject(Context context, C carrier, CarrierSetter<C> setter) {} |
| 21 | + |
| 22 | + /** |
| 23 | + * Extracts a context from un upstream service. |
| 24 | + * |
| 25 | + * @param context the base context to store the extracted values on top, use {@link |
| 26 | + * Context#root()} for a default base context. |
| 27 | + * @param carrier the instance to fetch the propagated key/value pairs from. |
| 28 | + * @param visitor the callback to walk over the carrier and extract its key/value pais. |
| 29 | + * @return A context with the extracted values on top of the given base context. |
| 30 | + */ |
| 31 | + @Override |
| 32 | + public <C> Context extract(Context context, C carrier, CarrierVisitor<C> visitor) { |
| 33 | + if (context == null || carrier == null || visitor == null) { |
| 34 | + return context; |
| 35 | + } |
| 36 | + InferredProxyContextExtractor extractor = new InferredProxyContextExtractor(); |
| 37 | + visitor.forEachKeyValue(carrier, extractor); |
| 38 | + |
| 39 | + InferredProxyContext extractedContext = extractor.extractedContext; |
| 40 | + if (extractedContext == null) { |
| 41 | + return context; |
| 42 | + } |
| 43 | + return extractedContext.storeInto(context); |
| 44 | + } |
| 45 | + |
| 46 | + public static class InferredProxyContextExtractor implements BiConsumer<String, String> { |
| 47 | + private InferredProxyContext extractedContext; |
| 48 | + |
| 49 | + InferredProxyContextExtractor() {} |
| 50 | + |
| 51 | + private Map<String, String> parseInferredProxyHeaders(String input) { |
| 52 | + Map<String, String> parsedHeaders = new HashMap<>(); |
| 53 | + return parsedHeaders; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Performs this operation on the given arguments. |
| 58 | + * |
| 59 | + * @param key the first input argument from an http header |
| 60 | + * @param value the second input argument from an http header |
| 61 | + */ |
| 62 | + @Override |
| 63 | + public void accept(String key, String value) { |
| 64 | + if (key == null || key.isEmpty() || !key.startsWith(INFERRED_PROXY_KEY)) { |
| 65 | + return; |
| 66 | + } |
| 67 | + Map<String, String> inferredProxyMap = parseInferredProxyHeaders(value); |
| 68 | + if (extractedContext == null) { |
| 69 | + extractedContext = new InferredProxyContext(); |
| 70 | + } |
| 71 | + extractedContext.putInferredProxyInfo(key, value); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments