Skip to content

Commit

Permalink
Merge pull request #5379 from DataDog/jpbempel/fix-duplicate-this-arg
Browse files Browse the repository at this point in the history
Fix `this` duplicate serialization in snapshot
  • Loading branch information
jpbempel authored Jun 14, 2023
2 parents 5eb4d6d + 646b67c commit c28320c
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public static void resetGlobalRate() {
setGlobalSnapshotRate(DEFAULT_GLOBAL_LOG_RATE);
}

public static void resetAll() {
PROBE_SAMPLERS.clear();
resetGlobalRate();
}

private static AdaptiveSampler createSampler(double rate) {
if (rate < 1) {
int intRate = (int) Math.round(rate * 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,6 @@ public void toJson(JsonWriter jsonWriter, CapturedContext capturedContext) throw
jsonWriter.beginObject();
jsonWriter.name(ARGUMENTS);
jsonWriter.beginObject();
if (capturedContext.getFields() != null && !capturedContext.getFields().isEmpty()) {
jsonWriter.name(THIS);
jsonWriter.beginObject();
jsonWriter.name(TYPE);
jsonWriter.value(capturedContext.getThisClassName());
jsonWriter.name(FIELDS);
jsonWriter.beginObject();
SerializationResult result =
toJsonCapturedValues(
jsonWriter,
capturedContext.getFields(),
capturedContext.getLimits(),
timeoutChecker);
jsonWriter.endObject(); // FIELDS
handleSerializationResult(jsonWriter, result);
jsonWriter.endObject(); // THIS
}
SerializationResult resultArgs =
toJsonCapturedValues(
jsonWriter,
Expand Down Expand Up @@ -194,7 +177,7 @@ private void handleSerializationResult(JsonWriter jsonWriter, SerializationResul
for (SerializationResult result : results) {
switch (result) {
case OK:
return;
break;
case FIELD_COUNT:
{
if (!fieldCountReported) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ private void serializeObjectValue(Object value, Limits limits) throws Exception
tokenWriter.objectPrologue(value);
Class<?> currentClass = value.getClass();
int processedFieldCount = 0;
NotCapturedReason reason = null;
classLoop:
do {
Field[] fields = currentClass.getDeclaredFields();
Expand All @@ -221,7 +222,7 @@ private void serializeObjectValue(Object value, Limits limits) throws Exception
onField(field, fieldValue, limits);
processedFieldCount++;
if (processedFieldCount >= limits.maxFieldCount) {
tokenWriter.notCaptured(NotCapturedReason.FIELD_COUNT);
reason = NotCapturedReason.FIELD_COUNT;
break classLoop;
}
} catch (Exception e) {
Expand All @@ -230,6 +231,9 @@ private void serializeObjectValue(Object value, Limits limits) throws Exception
}
} while ((currentClass = currentClass.getSuperclass()) != null);
tokenWriter.objectEpilogue(value);
if (reason != null) {
tokenWriter.notCaptured(reason);
}
}

private void onField(Field field, Object value, Limits limits) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void after() {
if (currentTransformer != null) {
instr.removeTransformer(currentTransformer);
}
ProbeRateLimiter.resetGlobalRate();
ProbeRateLimiter.resetAll();
Assertions.assertFalse(DebuggerContext.isInProbe());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import datadog.trace.bootstrap.debugger.MethodLocation;
import datadog.trace.bootstrap.debugger.ProbeId;
import datadog.trace.bootstrap.debugger.ProbeImplementation;
import datadog.trace.bootstrap.debugger.ProbeRateLimiter;
import java.io.IOException;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
Expand Down Expand Up @@ -45,6 +46,7 @@ public void after() {
if (currentTransformer != null) {
instr.removeTransformer(currentTransformer);
}
ProbeRateLimiter.resetAll();
}

@Test
Expand Down Expand Up @@ -316,7 +318,7 @@ public void lineTemplateThisLog() throws IOException, URISyntaxException {
Assertions.assertEquals(42, result);
Snapshot snapshot = assertOneSnapshot(listener);
assertEquals(
"this is log line for this={STATIC_STR=strStatic, intValue=48, doubleValue=3.14, strValue=done, strList=..., ...}",
"this is log line for this={STATIC_STR=strStatic, intValue=48, doubleValue=3.14, strValue=done, strList=...}, ...",
snapshot.getMessage());
}

Expand Down
Loading

0 comments on commit c28320c

Please sign in to comment.