Skip to content

Commit 0228f71

Browse files
feat: add product field to snapshots
1 parent eacde60 commit 0228f71

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/agent/JsonSnapshotSerializer.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
public class JsonSnapshotSerializer implements DebuggerContext.ValueSerializer {
1515
private static final String DD_TRACE_ID = "dd.trace_id";
1616
private static final String DD_SPAN_ID = "dd.span_id";
17+
public static final String TEST_OPT_PRODUCT = "test_optimization";
1718
private static final JsonAdapter<IntakeRequest> ADAPTER =
1819
MoshiHelper.createMoshiSnapshot().adapter(IntakeRequest.class);
1920
private static final JsonAdapter<CapturedContext.CapturedValue> VALUE_ADAPTER =
2021
new MoshiSnapshotHelper.CapturedValueAdapter();
2122

22-
public String serializeSnapshot(String serviceName, Snapshot snapshot) {
23-
IntakeRequest request = new IntakeRequest(serviceName, new DebuggerIntakeRequestData(snapshot));
23+
public String serializeSnapshot(String serviceName, Snapshot snapshot, Config config) {
24+
IntakeRequest request =
25+
new IntakeRequest(serviceName, new DebuggerIntakeRequestData(snapshot), config);
2426
handleCorrelationFields(snapshot, request);
2527
handleDuration(snapshot, request);
2628
handlerLogger(snapshot, request);
@@ -53,6 +55,7 @@ public static class IntakeRequest {
5355
private final String service;
5456
private final DebuggerIntakeRequestData debugger;
5557
private final String ddsource = "dd_debugger";
58+
private final String product;
5659
private final String message;
5760

5861
private final String ddtags;
@@ -85,14 +88,15 @@ public static class IntakeRequest {
8588
@Json(name = "logger.thread_name")
8689
private String loggerThreadName;
8790

88-
public IntakeRequest(String service, DebuggerIntakeRequestData debugger) {
91+
public IntakeRequest(String service, DebuggerIntakeRequestData debugger, Config config) {
8992
this.service = service;
9093
this.debugger = debugger;
9194
this.message = debugger.snapshot.getMessage();
9295
this.ddtags = debugger.snapshot.getProbe().getStrTags();
9396
this.timestamp = debugger.snapshot.getTimestamp();
9497
final CharSequence pt = ProcessTags.getTagsForSerialization();
9598
this.processTags = pt != null ? pt.toString() : null;
99+
this.product = config.isCiVisibilityFailedTestReplayActive() ? TEST_OPT_PRODUCT : null;
96100
}
97101

98102
public String getService() {
@@ -107,6 +111,10 @@ public String getDdsource() {
107111
return ddsource;
108112
}
109113

114+
public String getProduct() {
115+
return product;
116+
}
117+
110118
public String getMessage() {
111119
return message;
112120
}

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/sink/SnapshotSink.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ public class SnapshotSink {
4545
private final AtomicBoolean started = new AtomicBoolean();
4646
private volatile AgentTaskScheduler.Scheduled<SnapshotSink> highRateScheduled;
4747
private volatile long currentHighRateFlushInterval = HIGH_RATE_MAX_FLUSH_INTERVAL_MS;
48+
private final Config config;
4849

4950
public SnapshotSink(Config config, String tags, BatchUploader snapshotUploader) {
5051
this.serviceName = TagsHelper.sanitize(config.getServiceName());
5152
this.batchSize = config.getDynamicInstrumentationUploadBatchSize();
53+
this.config = config;
5254
this.tags = tags;
5355
this.snapshotUploader = snapshotUploader;
5456
}
@@ -183,7 +185,8 @@ private List<String> getSerializedSnapshots(BlockingQueue<Snapshot> queue, int l
183185

184186
private String serializeSnapshot(String serviceName, Snapshot snapshot) {
185187
snapshot.getId(); // Ensure id is generated
186-
String str = DebuggerAgent.getSnapshotSerializer().serializeSnapshot(serviceName, snapshot);
188+
String str =
189+
DebuggerAgent.getSnapshotSerializer().serializeSnapshot(serviceName, snapshot, config);
187190
String prunedStr = SnapshotPruner.prune(str, MAX_SNAPSHOT_SIZE, 4);
188191
if (prunedStr.length() != str.length()) {
189192
LOGGER.debug(

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/sink/DebuggerSinkTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,27 @@ public void addSnapshot(boolean processTagsEnabled) throws IOException {
124124
}
125125
}
126126

127+
@ParameterizedTest(name = "Add Failed Test Replay product ''{0}''")
128+
@ValueSource(booleans = {true, false})
129+
public void addProductTag(boolean failedTestReplayActive) throws IOException {
130+
when(config.isCiVisibilityFailedTestReplayActive()).thenReturn(failedTestReplayActive);
131+
ProcessTags.reset(config);
132+
DebuggerSink sink = createDefaultDebuggerSink();
133+
DebuggerAgentHelper.injectSerializer(new JsonSnapshotSerializer());
134+
Snapshot snapshot = createSnapshot();
135+
sink.addSnapshot(snapshot);
136+
sink.lowRateFlush(sink);
137+
verify(batchUploader).upload(payloadCaptor.capture(), matches(EXPECTED_SNAPSHOT_TAGS));
138+
String strPayload = new String(payloadCaptor.getValue(), StandardCharsets.UTF_8);
139+
System.out.println(strPayload);
140+
JsonSnapshotSerializer.IntakeRequest intakeRequest = assertOneIntakeRequest(strPayload);
141+
if (failedTestReplayActive) {
142+
assertEquals(JsonSnapshotSerializer.TEST_OPT_PRODUCT, intakeRequest.getProduct());
143+
} else {
144+
assertNull(intakeRequest.getProduct());
145+
}
146+
}
147+
127148
@Test
128149
public void addMultipleSnapshots() throws IOException {
129150
when(config.getDynamicInstrumentationUploadBatchSize()).thenReturn(2);

0 commit comments

Comments
 (0)