Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add draft inferred proxy spans as http request parent #8336

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package datadog.context;

import java.util.HashMap;
import java.util.Map;

public class InferredProxyContext implements ImplicitContextKeyed {
public static final ContextKey<InferredProxyContext> CONTEXT_KEY =
ContextKey.named("inferred-proxy-key");
private Map<String, String> inferredProxy;

public static InferredProxyContext fromContext(Context context) {
return context.get(CONTEXT_KEY);
}

public InferredProxyContext(Map<String, String> contextInfo) {
this.inferredProxy = contextInfo;
}

public InferredProxyContext() {
this.inferredProxy = new HashMap<>();
}

public Map<String, String> getInferredProxyContext() {
return inferredProxy;
}

public void putInferredProxyInfo(String key, String value) {
inferredProxy.put(key, value);
}

public void removeInferredProxyInfo(String key) {
inferredProxy.remove(key);
}

/**
* Creates a new context with this value under its chosen key.
*
* @param context the context to copy the original values from.
* @return the new context with the implicitly keyed value.
* @see Context#with(ImplicitContextKeyed)
*/
@Override
public Context storeInto(Context context) {
return context.with(CONTEXT_KEY, this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package datadog.context.propagation;

import datadog.context.Context;
import datadog.context.InferredProxyContext;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class InferredProxyPropagator implements Propagator {
public static final String INFERRED_PROXY_KEY = "x-dd-proxy";
/**
* Injects a context into a downstream service using the given carrier.
*
* @param context the context containing the values to be injected.
* @param carrier the instance that will receive the key/value pairs to propagate.
* @param setter the callback to set key/value pairs into the carrier.
*/
@Override
public <C> void inject(Context context, C carrier, CarrierSetter<C> setter) {
// TODO: find out does any inferred proxy info need to be injected to downstream services??
// afaik this shouldnt be used
if (carrier == null) {
return;
}
setter.set(carrier, INFERRED_PROXY_KEY, context.toString());
}

/**
* Extracts a context from un upstream service.
*
* @param context the base context to store the extracted values on top, use {@link
* Context#root()} for a default base context.
* @param carrier the instance to fetch the propagated key/value pairs from.
* @param visitor the callback to walk over the carrier and extract its key/value pais.
* @return A context with the extracted values on top of the given base context.
*/
@Override
public <C> Context extract(Context context, C carrier, CarrierVisitor<C> visitor) {
if (context == null || carrier == null || visitor == null) {
return context;
}
InferredProxyContextExtractor extractor = new InferredProxyContextExtractor();
visitor.forEachKeyValue(carrier, extractor);

InferredProxyContext extractedContext = extractor.extractedContext;
if (extractedContext == null) {
return context;
}
System.out.println("extracted Context:");
System.out.println(extractedContext);
System.out.println("after Extracted");
System.out.println(extractedContext.getInferredProxyContext());
return extractedContext.storeInto(context);
}

// TODO implement HTTP header parser rules
public static class InferredProxyContextExtractor implements BiConsumer<String, String> {
private InferredProxyContext extractedContext;

InferredProxyContextExtractor() {}

private Map<String, String> parseInferredProxyHeaders(String input) {
Map<String, String> parsedHeaders = new HashMap<>();
System.out.println("parsing input: " + input);
return parsedHeaders;
}

/**
* Performs this operation on the given arguments.
*
* @param key the first input argument from an http header
* @param value the second input argument from an http header
*/
@Override
public void accept(String key, String value) {
System.out.println("hello: " + key);
System.out.println(value);
if (key == null || key.isEmpty() || !key.startsWith(INFERRED_PROXY_KEY)) {
return;
}
Map<String, String> inferredProxyMap = parseInferredProxyHeaders(value);
if (extractedContext == null) {
extractedContext = new InferredProxyContext();
}
extractedContext.putInferredProxyInfo(key, value);
}
}
}
Loading