Skip to content

Commit 50f5b7a

Browse files
authored
Merge branch 'master' into mhlidd/migrate_tomcat_okhttp
2 parents 81a9768 + c2faab3 commit 50f5b7a

File tree

10 files changed

+1330
-44
lines changed

10 files changed

+1330
-44
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package datadog.context;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class InferredProxyContext implements ImplicitContextKeyed {
8+
public static final ContextKey<InferredProxyContext> CONTEXT_KEY =
9+
ContextKey.named("inferred-proxy-key");
10+
private final Map<String, String> inferredProxy;
11+
12+
public static InferredProxyContext fromContext(Context context) {
13+
return context.get(CONTEXT_KEY);
14+
}
15+
16+
public InferredProxyContext(Map<String, String> contextInfo) {
17+
this.inferredProxy =
18+
(contextInfo == null || contextInfo.isEmpty())
19+
? new HashMap<>()
20+
: new HashMap<>(contextInfo);
21+
}
22+
23+
public InferredProxyContext() {
24+
this.inferredProxy = new HashMap<>();
25+
}
26+
27+
public Map<String, String> getInferredProxyContext() {
28+
return Collections.unmodifiableMap(inferredProxy);
29+
}
30+
31+
public void putInferredProxyInfo(String key, String value) {
32+
inferredProxy.put(key, value);
33+
}
34+
35+
public void removeInferredProxyInfo(String key) {
36+
inferredProxy.remove(key);
37+
}
38+
39+
/**
40+
* Creates a new context with this value under its chosen key.
41+
*
42+
* @param context the context to copy the original values from.
43+
* @return the new context with the implicitly keyed value.
44+
* @see Context#with(ImplicitContextKeyed)
45+
*/
46+
@Override
47+
public Context storeInto(Context context) {
48+
return context.with(CONTEXT_KEY, this);
49+
}
50+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)