-
Notifications
You must be signed in to change notification settings - Fork 304
Implement APIGW Inferred Proxy Spans #8336
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
Changes from 23 commits
934f400
d0aa92b
25ec7b8
6e6f67b
a67d0da
7170583
bac390a
4e5d8c7
8216eb1
0724360
9938175
ecee7d7
6141896
3c74099
c63ef4c
ddbc584
9abec59
b004c19
9016603
77119c7
c9882da
1458e52
bb26be8
27cfb94
ca4d546
d71753e
dfcc717
d9f55c5
9c45d87
9ed4910
d73cee5
ce06b40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might want to adjust the capacity There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's your recommendation on standardizing capacity? I see an example like this (link) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you know about how many elements that you are going to put into the Map, then initialize the capacity to that. HashMap is automatically going to pick the next power of 2 up from the capacity specified. |
||
} | ||
|
||
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,78 @@ | ||
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) { | ||
if (carrier == null) { | ||
return; | ||
} | ||
setter.set(carrier, INFERRED_PROXY_KEY, context.toString()); | ||
jordan-wong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
return extractedContext.storeInto(context); | ||
} | ||
|
||
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<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's happening here? |
||
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) { | ||
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); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.