-
Notifications
You must be signed in to change notification settings - Fork 312
Refactor Severless Gateway Inferred Span #9388
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
Open
PerfectSlayer
wants to merge
2
commits into
master
Choose a base branch
from
bbujon/gateway-inferred-span2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
dd-trace-core/src/main/java/datadog/trace/core/propagation/InferredProxyPropagator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package datadog.trace.core.propagation; | ||
|
||
import static datadog.trace.api.gateway.InferredProxySpan.fromHeaders; | ||
|
||
import datadog.context.Context; | ||
import datadog.context.propagation.CarrierSetter; | ||
import datadog.context.propagation.CarrierVisitor; | ||
import datadog.context.propagation.Propagator; | ||
import datadog.trace.api.gateway.InferredProxySpan; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
/** Inferred proxy propagator. Only extract, not meant for injection. */ | ||
@ParametersAreNonnullByDefault | ||
public class InferredProxyPropagator implements Propagator { | ||
private static final String INFERRED_PROXY_KEY_PREFIX = "x-dd-proxy"; | ||
|
||
@Override | ||
public <C> void inject(Context context, C carrier, CarrierSetter<C> setter) {} | ||
|
||
@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); | ||
InferredProxySpan inferredProxySpan = extractor.inferredProxySpan(); | ||
if (inferredProxySpan != null) { | ||
context = context.with(inferredProxySpan); | ||
} | ||
return context; | ||
} | ||
|
||
/** Extract inferred proxy related headers into a map. */ | ||
private static class InferredProxyContextExtractor implements BiConsumer<String, String> { | ||
private Map<String, String> values; | ||
|
||
@Override | ||
public void accept(String key, String value) { | ||
if (key == null || key.isEmpty() || !key.startsWith(INFERRED_PROXY_KEY_PREFIX)) { | ||
return; | ||
} | ||
if (values == null) { | ||
this.values = new HashMap<>(); | ||
} | ||
this.values.put(key, value); | ||
} | ||
|
||
public InferredProxySpan inferredProxySpan() { | ||
if (this.values == null) { | ||
return null; | ||
} | ||
InferredProxySpan inferredProxySpan = fromHeaders(this.values); | ||
return inferredProxySpan.isValid() ? inferredProxySpan : null; | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
dd-trace-core/src/test/java/datadog/trace/core/propagation/InferredProxyPropagatorTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package datadog.trace.core.propagation; | ||
|
||
import static datadog.context.Context.root; | ||
import static datadog.trace.api.gateway.InferredProxySpan.fromContext; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.params.provider.Arguments.of; | ||
|
||
import datadog.context.Context; | ||
import datadog.context.propagation.CarrierVisitor; | ||
import datadog.trace.api.gateway.InferredProxySpan; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
import java.util.stream.Stream; | ||
import javax.annotation.ParametersAreNonnullByDefault; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
@DisplayName("InferredProxyPropagator Tests") | ||
class InferredProxyPropagatorTests { | ||
private static final String PROXY_SYSTEM_KEY = "x-dd-proxy"; | ||
private static final String PROXY_REQUEST_TIME_MS_KEY = "x-dd-proxy-request-time-ms"; | ||
private static final String PROXY_PATH_KEY = "x-dd-proxy-path"; | ||
private static final String PROXY_HTTP_METHOD_KEY = "x-dd-proxy-httpmethod"; | ||
private static final String PROXY_DOMAIN_NAME_KEY = "x-dd-proxy-domain-name"; | ||
private static final MapVisitor MAP_VISITOR = new MapVisitor(); | ||
|
||
private InferredProxyPropagator propagator; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.propagator = new InferredProxyPropagator(); | ||
} | ||
|
||
@Test | ||
@DisplayName("Should extract InferredProxySpan when valid headers are present") | ||
void testSuccessfulExtraction() { | ||
Map<String, String> headers = new HashMap<>(); | ||
headers.put(PROXY_SYSTEM_KEY, "aws-apigateway"); | ||
headers.put(PROXY_REQUEST_TIME_MS_KEY, "12345"); | ||
headers.put(PROXY_PATH_KEY, "/foo"); | ||
headers.put(PROXY_HTTP_METHOD_KEY, "GET"); | ||
headers.put(PROXY_DOMAIN_NAME_KEY, "api.example.com"); | ||
|
||
Context context = this.propagator.extract(root(), headers, MAP_VISITOR); | ||
InferredProxySpan inferredProxySpan = fromContext(context); | ||
assertNotNull(inferredProxySpan); | ||
assertTrue(inferredProxySpan.isValid()); | ||
} | ||
|
||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("invalidOrMissingHeadersProviderForPropagator") | ||
@DisplayName("Should not create InferredProxySpan if some critical headers are missing") | ||
void testExtractionWithMissingCriticalHeaders(String description, Map<String, String> headers) { | ||
Context rootContext = root(); | ||
Context extractedOuterContext = this.propagator.extract(rootContext, headers, MAP_VISITOR); | ||
InferredProxySpan inferredProxySpan = fromContext(extractedOuterContext); | ||
assertNull(inferredProxySpan, "Invalid inferred proxy span should not be extracted"); | ||
} | ||
|
||
static Stream<Arguments> invalidOrMissingHeadersProviderForPropagator() { // Renamed | ||
Map<String, String> missingSystem = new HashMap<>(); | ||
missingSystem.put(PROXY_REQUEST_TIME_MS_KEY, "12345"); | ||
missingSystem.put(PROXY_PATH_KEY, "/foo"); | ||
|
||
Map<String, String> emptyValue = new HashMap<>(); | ||
emptyValue.put(PROXY_SYSTEM_KEY, ""); | ||
|
||
Map<String, String> nullValue = new HashMap<>(); | ||
nullValue.put(PROXY_SYSTEM_KEY, null); | ||
|
||
Map<String, String> missingTime = new HashMap<>(); | ||
missingTime.put(PROXY_SYSTEM_KEY, "aws-apigw"); | ||
missingTime.put(PROXY_PATH_KEY, "/foo"); | ||
|
||
return Stream.of( | ||
of("PROXY_SYSTEM_KEY missing", missingSystem), | ||
of("PROXY_SYSTEM_KEY empty", emptyValue), | ||
of("PROXY_SYSTEM_KEY null", nullValue), | ||
of("PROXY_REQUEST_TIME_MS_KEY missing", missingTime)); | ||
} | ||
|
||
@ParametersAreNonnullByDefault | ||
private static class MapVisitor implements CarrierVisitor<Map<String, String>> { | ||
@Override | ||
public void forEachKeyValue(Map<String, String> carrier, BiConsumer<String, String> visitor) { | ||
carrier.forEach(visitor); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change related to inferred span?