Skip to content

Commit c63ef4c

Browse files
committed
working version happy path using context
1 parent 3c74099 commit c63ef4c

File tree

5 files changed

+72
-32
lines changed

5 files changed

+72
-32
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/decorator/HttpServerDecorator.java

+43-19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import static datadog.trace.bootstrap.instrumentation.decorator.http.HttpResourceDecorator.HTTP_RESOURCE_DECORATOR;
99

1010
import datadog.appsec.api.blocking.BlockingException;
11+
import datadog.context.Context;
12+
import datadog.context.InferredProxyContext;
13+
import datadog.context.propagation.Propagators;
1114
import datadog.trace.api.Config;
1215
import datadog.trace.api.DDTags;
1316
import datadog.trace.api.DDTraceId;
@@ -409,16 +412,6 @@ public void setRequestBlockingAction(Flow.Action.RequestBlockingAction rba) {
409412
public Flow.Action.RequestBlockingAction getRequestBlockingAction() {
410413
return serverSpan.getRequestBlockingAction();
411414
}
412-
413-
@Override
414-
public boolean isRequiresPostProcessing() {
415-
return serverSpan.isRequiresPostProcessing();
416-
}
417-
418-
@Override
419-
public void setRequiresPostProcessing(boolean requiresPostProcessing) {
420-
serverSpan.setRequiresPostProcessing(requiresPostProcessing);
421-
}
422415
}
423416

424417
private static final Logger log = LoggerFactory.getLogger(HttpServerDecorator.class);
@@ -501,6 +494,8 @@ public AgentSpanContext.Extracted extract(REQUEST_CARRIER carrier) {
501494
if (null == carrier || null == getter) {
502495
return null;
503496
}
497+
Context c = Propagators.defaultPropagator().extract(Context.root(), carrier, getter);
498+
c.attach();
504499
return extractContextAndGetSpanContext(carrier, getter);
505500
}
506501

@@ -514,36 +509,63 @@ public AgentSpan startSpan(
514509
String instrumentationName, REQUEST_CARRIER carrier, AgentSpanContext.Extracted context) {
515510
AgentSpan apiGtwSpan = null;
516511
if (Config.get().isInferredProxyPropagationEnabled()) {
517-
System.out.println("inferred proxy to be crearted");
512+
System.out.println("inferred proxy to be created");
518513
// create the apigtw span
519514
apiGtwSpan =
520515
tracer().startSpan("inferred_proxy", "aws.apigateway", callIGCallbackStart(context));
516+
InferredProxyContext inferredProxy = InferredProxyContext.fromContext(Context.current());
517+
System.out.println("inferredProxy matt way: " + inferredProxy);
518+
System.out.println("inferredProxy matt way map: " + inferredProxy.getInferredProxyContext());
521519

522520
// WILL NEED CONTEXT TRACKING API TO GET TAGS FROM CONTEXT
523521
// set tags from Context
524522
// System.out.println("here");
525523
// InferredProxyContext inferredProxyContext =
526-
// Context.root().get(InferredProxyContext.CONTEXT_KEY);
524+
// Context.root().get(InferredProxyContext.CONTEXT_KEY);
527525
// System.out.println("hello inferred context obj: " + inferredProxyContext);
528526
// Map<String, String> contextMap = inferredProxyContext.getInferredProxyContext();
529527
// System.out.println("hello inferred map: " + contextMap);
530528
// //get(InferredProxyPropagator.INFERRED_PROXY_KEY);
531529
// if (contextMap != null) {
532530
// apiGtwSpan.setAllTags(inferredProxyContext.getInferredProxyContext());
533531
// }
534-
532+
System.out.println("gateway span after context: " + apiGtwSpan);
533+
/*
534+
inferredProxy matt way map:
535+
{x-dd-proxy-request-time-ms=123,
536+
x-dd-proxy-path=/api/hello,
537+
x-dd-proxy-httpmethod=GET,
538+
x-dd-proxy-domain-name=example.com,
539+
x-dd-proxy=true,
540+
x-dd-proxy-stage=dev}
541+
*/
542+
Map<String, String> inferredProxyTagInfo = inferredProxy.getInferredProxyContext();
535543
// mocking tags
536544
apiGtwSpan.setTag(Tags.COMPONENT, "aws.apigateway");
537-
apiGtwSpan.setTag(DDTags.RESOURCE_NAME, "GET /api/hello");
538-
apiGtwSpan.setTag(DDTags.TRACE_START_TIME, "123");
539-
apiGtwSpan.setTag(DDTags.SERVICE_NAME, "example.com");
545+
// "GET /api/hello"
546+
apiGtwSpan.setTag(
547+
DDTags.RESOURCE_NAME,
548+
inferredProxyTagInfo.get("x-dd-proxy-httpmethod")
549+
+ " "
550+
+ inferredProxyTagInfo.get("x-dd-proxy-path"));
551+
// 123
552+
apiGtwSpan.setTag(
553+
DDTags.TRACE_START_TIME, inferredProxyTagInfo.get("x-dd-proxy-request-time-ms"));
554+
// example.com
555+
apiGtwSpan.setTag(DDTags.SERVICE_NAME, inferredProxyTagInfo.get("x-dd-proxy-domain-name"));
540556
apiGtwSpan.setTag(DDTags.SPAN_TYPE, "web");
541-
apiGtwSpan.setTag(Tags.HTTP_METHOD, "GET");
542-
apiGtwSpan.setTag(Tags.HTTP_URL, "example.com/api/hello");
543-
apiGtwSpan.setHttpStatusCode(200);
557+
// GET
558+
apiGtwSpan.setTag(Tags.HTTP_METHOD, inferredProxyTagInfo.get("x-dd-proxy-httpmethod"));
559+
// "example.com/api/hello"
560+
apiGtwSpan.setTag(
561+
Tags.HTTP_URL,
562+
inferredProxyTagInfo.get("x-dd-proxy-domain-name")
563+
+ inferredProxyTagInfo.get("x-dd-proxy-path"));
564+
// apiGtwSpan.setHttpStatusCode(200);
544565
apiGtwSpan.setTag("stage", "dev");
545566
apiGtwSpan.setTag("_dd.inferred_span", "1");
546567
}
568+
547569
AgentSpan span =
548570
tracer()
549571
.startSpan(
@@ -560,6 +582,8 @@ public AgentSpan startSpan(
560582
tracer().getDataStreamsMonitoring().setCheckpoint(span, fromTags(SERVER_PATHWAY_EDGE_TAGS));
561583
}
562584
System.out.println("starting http server span");
585+
System.out.println(apiGtwSpan);
586+
System.out.println(span);
563587
return new InferredProxySpanGroup(apiGtwSpan, span);
564588
}
565589

dd-trace-api/src/main/java/datadog/trace/api/ConfigDefaults.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package datadog.trace.api;
22

3-
import static datadog.trace.api.TracePropagationStyle.BAGGAGE;
4-
import static datadog.trace.api.TracePropagationStyle.DATADOG;
5-
import static datadog.trace.api.TracePropagationStyle.TRACECONTEXT;
3+
import static datadog.trace.api.TracePropagationStyle.*;
64
import static java.util.Arrays.asList;
75

86
import java.util.Arrays;
@@ -79,7 +77,7 @@ public final class ConfigDefaults {
7977
static final int DEFAULT_PARTIAL_FLUSH_MIN_SPANS = 1000;
8078
static final boolean DEFAULT_PROPAGATION_EXTRACT_LOG_HEADER_NAMES_ENABLED = false;
8179
static final Set<TracePropagationStyle> DEFAULT_TRACE_PROPAGATION_STYLE =
82-
new LinkedHashSet<>(asList(DATADOG, TRACECONTEXT, BAGGAGE));
80+
new LinkedHashSet<>(asList(DATADOG, TRACECONTEXT, BAGGAGE, INFERREDPROXY));
8381
static final Set<PropagationStyle> DEFAULT_PROPAGATION_STYLE =
8482
new LinkedHashSet<>(asList(PropagationStyle.DATADOG));
8583
static final int DEFAULT_TRACE_BAGGAGE_MAX_ITEMS = 64;

dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
import static datadog.trace.api.DDTags.DJM_ENABLED;
55
import static datadog.trace.api.DDTags.DSM_ENABLED;
66
import static datadog.trace.api.DDTags.PROFILING_CONTEXT_ENGINE;
7-
import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.BAGGAGE_CONCERN;
8-
import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.DSM_CONCERN;
9-
import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.TRACING_CONCERN;
10-
import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.XRAY_TRACING_CONCERN;
7+
import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.*;
118
import static datadog.trace.common.metrics.MetricsAggregatorFactory.createMetricsAggregator;
129
import static datadog.trace.util.AgentThreadFactory.AGENT_THREAD_GROUP;
1310
import static datadog.trace.util.CollectionUtils.tryMakeImmutableMap;
@@ -21,6 +18,7 @@
2118
import datadog.communication.ddagent.SharedCommunicationObjects;
2219
import datadog.communication.monitor.Monitoring;
2320
import datadog.communication.monitor.Recording;
21+
import datadog.context.propagation.InferredProxyPropagator;
2422
import datadog.context.propagation.Propagators;
2523
import datadog.trace.api.ClassloaderConfigurationOverrides;
2624
import datadog.trace.api.Config;
@@ -726,6 +724,9 @@ private CoreTracer(
726724
if (config.isBaggagePropagationEnabled()) {
727725
Propagators.register(BAGGAGE_CONCERN, new BaggagePropagator(config));
728726
}
727+
if (config.isInferredProxyPropagationEnabled()) {
728+
Propagators.register(INFERRED_PROXY_CONCERN, new InferredProxyPropagator());
729+
}
729730

730731
this.tagInterceptor =
731732
null == tagInterceptor ? new TagInterceptor(new RuleFlags(config)) : tagInterceptor;

internal-api/src/main/java/datadog/trace/api/Config.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public static String getHostName() {
194194
private final boolean tracePropagationExtractFirst;
195195
private final int traceBaggageMaxItems;
196196
private final int traceBaggageMaxBytes;
197+
private final boolean traceInferredProxyEnabled;
197198
private final int clockSyncPeriod;
198199
private final boolean logsInjectionEnabled;
199200

@@ -1065,7 +1066,8 @@ private Config(final ConfigProvider configProvider, final InstrumenterConfig ins
10651066
tracePropagationExtractFirst =
10661067
configProvider.getBoolean(
10671068
TRACE_PROPAGATION_EXTRACT_FIRST, DEFAULT_TRACE_PROPAGATION_EXTRACT_FIRST);
1068-
inferredProxyEnabled = configProvider.getBoolean(TRACE_INFERRED_PROXY_SERVICES_ENABLED, false);
1069+
traceInferredProxyEnabled =
1070+
configProvider.getBoolean(TRACE_INFERRED_PROXY_SERVICES_ENABLED, false);
10691071

10701072
clockSyncPeriod = configProvider.getInteger(CLOCK_SYNC_PERIOD, DEFAULT_CLOCK_SYNC_PERIOD);
10711073

@@ -2357,6 +2359,22 @@ public boolean isTracePropagationExtractFirst() {
23572359
return tracePropagationExtractFirst;
23582360
}
23592361

2362+
public boolean isInferredProxyToExtract() {
2363+
return tracePropagationStylesToExtract.contains(TracePropagationStyle.INFERREDPROXY);
2364+
}
2365+
2366+
public boolean isInferredProxyToInject() {
2367+
return tracePropagationStylesToInject.contains(TracePropagationStyle.INFERREDPROXY);
2368+
}
2369+
2370+
public boolean isInferredProxyEnabledByEnv() {
2371+
return traceInferredProxyEnabled;
2372+
}
2373+
2374+
public boolean isInferredProxyPropagationEnabled() {
2375+
return isInferredProxyToExtract() || isInferredProxyToInject() || isInferredProxyEnabledByEnv();
2376+
}
2377+
23602378
public boolean isBaggageExtract() {
23612379
return tracePropagationStylesToExtract.contains(TracePropagationStyle.BAGGAGE);
23622380
}
@@ -4736,10 +4754,8 @@ public String toString() {
47364754
+ debuggerThirdPartyIncludes
47374755
+ ", thirdPartyExcludes="
47384756
+ debuggerThirdPartyExcludes
4739-
+ ", debuggerExceptionEnabled="
4740-
+ debuggerExceptionEnabled
4741-
+ ", debuggerCodeOriginEnabled="
4742-
+ debuggerCodeOriginEnabled
4757+
+ ", thirdPartyShadingIdentifiers="
4758+
+ debuggerShadingIdentifiers
47434759
+ ", awsPropagationEnabled="
47444760
+ awsPropagationEnabled
47454761
+ ", sqsPropagationEnabled="

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/AgentPropagation.java

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public final class AgentPropagation {
1717
// TODO DSM propagator should run after the other propagators as it stores the pathway context
1818
// TODO into the span context for now. Remove priority after the migration is complete.
1919
public static final Concern DSM_CONCERN = withPriority("data-stream-monitoring", 110);
20+
public static final Concern INFERRED_PROXY_CONCERN = named("inferred-proxy");
2021

2122
private AgentPropagation() {}
2223

0 commit comments

Comments
 (0)