Skip to content
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

Remove explicit capture of fields #7282

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class CapturedContext implements ValueReferenceResolver {
private Map<String, CapturedValue> locals;
private CapturedThrowable throwable;
private Map<String, CapturedValue> staticFields;
private Map<String, CapturedValue> fields;
private Limits limits = Limits.DEFAULT;
private String thisClassName;
private String traceId;
Expand All @@ -43,20 +42,17 @@ public CapturedContext(
CapturedValue[] arguments,
CapturedValue[] locals,
CapturedValue returnValue,
CapturedThrowable throwable,
CapturedValue[] fields) {
CapturedThrowable throwable) {
addArguments(arguments);
addLocals(locals);
addReturn(returnValue);
this.throwable = throwable;
addFields(fields);
}

private CapturedContext(CapturedContext other, Map<String, Object> extensions) {
this.arguments = other.arguments;
this.locals = other.getLocals();
this.throwable = other.throwable;
this.fields = other.fields;
this.extensions.putAll(other.extensions);
this.extensions.putAll(extensions);
}
Expand Down Expand Up @@ -168,15 +164,16 @@ private Object tryRetrieve(String name) {
if (result != null) {
return result;
}
if (fields != null && !fields.isEmpty()) {
result = fields.get(name);
}
if (result != null) {
return result;
}
if (staticFields != null && !staticFields.isEmpty()) {
result = staticFields.get(name);
}
CapturedValue thisValue;
if (arguments != null && (thisValue = arguments.get("this")) != null) {
result = getMember(thisValue.getValue(), name);
if (result != Values.UNDEFINED_OBJECT) {
return result;
}
}
return result != null ? result : Values.UNDEFINED_OBJECT;
}

Expand Down Expand Up @@ -236,25 +233,15 @@ public void addStaticFields(CapturedValue[] values) {
}
}

public void addFields(CapturedValue[] values) {
if (values == null) {
return;
}
for (CapturedValue value : values) {
putInFields(value.name, value);
}
traceId = extractSpecialId("dd.trace_id");
spanId = extractSpecialId("dd.span_id");
public void addTraceId(CapturedValue capturedValue) {
traceId = extractStringId(capturedValue);
}

private String extractSpecialId(String idName) {
if (fields == null) {
return null;
}
CapturedValue capturedValue = fields.get(idName);
if (capturedValue == null) {
return null;
}
public void addSpanId(CapturedValue capturedValue) {
spanId = extractStringId(capturedValue);
}

private String extractStringId(CapturedValue capturedValue) {
Object value = capturedValue.getValue();
return value instanceof String ? (String) value : null;
}
Expand All @@ -280,10 +267,6 @@ public Map<String, CapturedValue> getStaticFields() {
return staticFields;
}

public Map<String, CapturedValue> getFields() {
return fields;
}

public Limits getLimits() {
return limits;
}
Expand Down Expand Up @@ -314,9 +297,6 @@ public void freeze(TimeoutChecker timeoutChecker) {
if (staticFields != null) {
staticFields.values().forEach(capturedValue -> capturedValue.freeze(timeoutChecker));
}
if (fields != null) {
fields.values().forEach(capturedValue -> capturedValue.freeze(timeoutChecker));
}
}

public Status evaluate(
Expand Down Expand Up @@ -359,13 +339,13 @@ public boolean equals(Object o) {
CapturedContext context = (CapturedContext) o;
return Objects.equals(arguments, context.arguments)
&& Objects.equals(locals, context.locals)
&& Objects.equals(throwable, context.throwable)
&& Objects.equals(fields, context.fields);
&& Objects.equals(staticFields, context.staticFields)
&& Objects.equals(throwable, context.throwable);
}

@Override
public int hashCode() {
return Objects.hash(arguments, locals, throwable, fields);
return Objects.hash(arguments, locals, throwable, staticFields);
}

@Override
Expand All @@ -377,8 +357,8 @@ public String toString() {
+ locals
+ ", throwable="
+ throwable
+ ", fields="
+ fields
+ ", staticFields="
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

+ staticFields
+ '}';
}

Expand All @@ -396,13 +376,6 @@ private void putInArguments(String name, CapturedValue value) {
arguments.put(name, value);
}

private void putInFields(String name, CapturedValue value) {
if (fields == null) {
fields = new HashMap<>();
}
fields.put(name, value);
}

private void putInStaticFields(String name, CapturedValue value) {
if (staticFields == null) {
staticFields = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,23 @@ public class ProbeConditionTest {
@Test
void testExecuteCondition() throws Exception {
ProbeCondition probeCondition = load("/test_conditional_01.json");

Collection<String> tags = Arrays.asList("hello", "world", "ko");
Map<String, Object> fields = new HashMap<>();
fields.put("tags", tags);
fields.put("field", 10);
class Obj {
class Obj1 {
Collection<String> tags = Arrays.asList("hello", "world", "ko");
private int field = 10;
List<String> field2 = new ArrayList<>();
}
ValueReferenceResolver ctx =
RefResolverHelper.createResolver(singletonMap("this", new Obj()), null, fields);
RefResolverHelper.createResolver(singletonMap("this", new Obj1()), null);

assertTrue(probeCondition.execute(ctx));

Collection<String> tags2 = Arrays.asList("hey", "world", "ko");
fields = new HashMap<>();
fields.put("tags", tags2);
fields.put("field", 10);
class Obj2 {
Collection<String> tags = Arrays.asList("hey", "world", "ko");
private int field = 10;
List<String> field2 = new ArrayList<>();
}
ValueReferenceResolver ctx2 =
RefResolverHelper.createResolver(singletonMap("this", new Obj()), null, fields);
RefResolverHelper.createResolver(singletonMap("this", new Obj2()), null);
assertFalse(probeCondition.execute(ctx2));
}

Expand All @@ -63,19 +61,15 @@ class Obj {
}
ValueReferenceResolver ctx =
RefResolverHelper.createResolver(
singletonMap("this", new Obj()),
singletonMap("container", new Container("world")),
null);
singletonMap("this", new Obj()), singletonMap("container", new Container("world")));

assertTrue(probeCondition.execute(ctx));
class Obj2 {
Container obj = new Container("hello");
}
ValueReferenceResolver ctx2 =
RefResolverHelper.createResolver(
singletonMap("this", new Obj2()),
singletonMap("container", new Container("world")),
null);
singletonMap("this", new Obj2()), singletonMap("container", new Container("world")));
RuntimeException runtimeException =
assertThrows(RuntimeException.class, () -> probeCondition.execute(ctx2));
assertEquals("Cannot dereference to field: container", runtimeException.getMessage());
Expand All @@ -89,11 +83,7 @@ class Obj {
String strField = "foo";
}
Obj obj = new Obj();
Map<String, Object> fields = new HashMap<>();
fields.put("intField1", obj.intField1);
fields.put("strField", obj.strField);
ValueReferenceResolver ctx =
RefResolverHelper.createResolver(singletonMap("this", obj), null, fields);
ValueReferenceResolver ctx = RefResolverHelper.createResolver(singletonMap("this", obj), null);
assertTrue(probeCondition.execute(ctx));
}

Expand All @@ -105,31 +95,39 @@ class Obj {
}
ValueReferenceResolver ctx =
RefResolverHelper.createResolver(
singletonMap("this", new Obj()), singletonMap("nullField", null), null);
singletonMap("this", new Obj()), singletonMap("nullField", null));
assertTrue(probeCondition.execute(ctx));
}

@Test
void testIndex() throws Exception {
ProbeCondition probeCondition = load("/test_conditional_07.json");
Map<String, Object> fields = new HashMap<>();
fields.put("intArray", new int[] {1, 1, 1});
fields.put("strArray", new String[] {"foo", "bar"});
Map<String, String> strMap = new HashMap<>();
strMap.put("foo", "bar");
strMap.put("bar", "foobar");
fields.put("strMap", strMap);
fields.put("idx", 1);
ValueReferenceResolver ctx = RefResolverHelper.createResolver(null, null, fields);
class Obj {
int[] intArray = new int[] {1, 1, 1};
String[] strArray = new String[] {"foo", "bar"};
Map<String, String> strMap = new HashMap<>();

{
strMap.put("foo", "bar");
strMap.put("bar", "foobar");
}

int idx = 1;
}
ValueReferenceResolver ctx =
RefResolverHelper.createResolver(singletonMap("this", new Obj()), null);
assertTrue(probeCondition.execute(ctx));
}

@Test
void testStringOperation() throws Exception {
ProbeCondition probeCondition = load("/test_conditional_08.json");
Map<String, Object> fields = new HashMap<>();
fields.put("strField", "foobar");
ValueReferenceResolver ctx = RefResolverHelper.createResolver(null, null, fields);
class Obj {
String strField = "foobar";
}
ValueReferenceResolver ctx =
RefResolverHelper.createResolver(singletonMap("this", new Obj()), null);
assertTrue(probeCondition.execute(ctx));
}

Expand All @@ -148,9 +146,11 @@ void testJsonAdapter() throws IOException {
@Test
void testJsonParsing() throws IOException {
ProbeCondition probeCondition = load("/test_conditional_02.json");
Collection<String> vets = Arrays.asList("vet1", "vet2", "vet3");
class Obj {
Collection<String> vets = Arrays.asList("vet1", "vet2", "vet3");
}
ValueReferenceResolver ctx =
RefResolverHelper.createResolver(null, null, singletonMap("vets", vets));
RefResolverHelper.createResolver(singletonMap("this", new Obj()), null);

// the condition checks if length of vets > 2
assertTrue(probeCondition.execute(ctx));
Expand Down Expand Up @@ -192,7 +192,7 @@ void redaction() throws IOException {
ProbeCondition probeCondition = load("/test_conditional_09.json");
Map<String, Object> args = new HashMap<>();
args.put("password", "secret123");
ValueReferenceResolver ctx = RefResolverHelper.createResolver(args, null, null);
ValueReferenceResolver ctx = RefResolverHelper.createResolver(args, null);
EvaluationException evaluationException =
assertThrows(EvaluationException.class, () -> probeCondition.execute(ctx));
assertEquals(
Expand All @@ -207,56 +207,70 @@ void stringPrimitives() throws IOException {
args.put("uuid", UUID.fromString("a3cbe9e7-edd3-4bef-8e5b-59bfcb04cf91"));
args.put("duration", Duration.ofSeconds(42));
args.put("clazz", "foo".getClass());
ValueReferenceResolver ctx = RefResolverHelper.createResolver(args, null, null);
ValueReferenceResolver ctx = RefResolverHelper.createResolver(args, null);
assertTrue(probeCondition.execute(ctx));
}

@Test
void testBooleanOperation() throws Exception {
ProbeCondition probeCondition = load("/test_conditional_11.json");
Map<String, Object> fields = new HashMap<>();
fields.put("strField", "foobar");
fields.put("emptyStr", "");
fields.put("emptyList", new ArrayList<>());
fields.put("emptyMap", new HashMap<>());
fields.put("emptySet", new HashSet<>());
fields.put("emptyArray", new Object[0]);
ValueReferenceResolver ctx = RefResolverHelper.createResolver(null, null, fields);
class Obj {
String strField = "foobar";
String emptyStr = "";
List<String> emptyList = new ArrayList<>();
Map<String, String> emptyMap = new HashMap<>();
Set<String> emptySet = new HashSet<>();
Object[] emptyArray = new Object[0];
}
ValueReferenceResolver ctx =
RefResolverHelper.createResolver(singletonMap("this", new Obj()), null);
assertTrue(probeCondition.execute(ctx));
}

@Test
void testLiterals() throws Exception {
ProbeCondition probeCondition = load("/test_conditional_13.json");
Map<String, Object> fields = new HashMap<>();
fields.put("boolVal", true);
fields.put("intVal", 1);
fields.put("longVal", 1L);
fields.put("doubleVal", 1.0);
fields.put("strVal", "foo");
fields.put("objVal", null);
fields.put("charVal", 'a');
ValueReferenceResolver ctx = RefResolverHelper.createResolver(null, null, fields);
class Obj {
boolean boolVal = true;
int intVal = 1;
long longVal = 1L;
double doubleVal = 1.0;
String strVal = "foo";
Object objVal = null;
char charVal = 'a';
}
ValueReferenceResolver ctx =
RefResolverHelper.createResolver(singletonMap("this", new Obj()), null);
assertTrue(probeCondition.execute(ctx));
}

@Test
void testLenCount() throws Exception {
ProbeCondition probeCondition = load("/test_conditional_14.json");
Map<String, Object> fields = new HashMap<>();
fields.put("intArray", new int[] {1, 1, 1});
fields.put("strArray", new String[] {"foo", "bar"});
Map<String, String> strMap = new HashMap<>();
strMap.put("foo", "bar");
strMap.put("bar", "foobar");
fields.put("strMap", strMap);
Set<String> strSet = new HashSet<>();
strSet.add("foo");
fields.put("strSet", strSet);
List<String> strList = new ArrayList<>();
strList.add("foo");
fields.put("strList", strList);
ValueReferenceResolver ctx = RefResolverHelper.createResolver(null, null, fields);
class Obj {
int[] intArray = new int[] {1, 1, 1};
String[] strArray = new String[] {"foo", "bar"};
Map<String, String> strMap = new HashMap<>();

{
strMap.put("foo", "bar");
strMap.put("bar", "foobar");
}

Set<String> strSet = new HashSet<>();

{
strSet.add("foo");
}

List<String> strList = new ArrayList<>();

{
strList.add("foo");
}
}
ValueReferenceResolver ctx =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use createResolver(new Object)? - as it seems to do the same thing...

public static ValueReferenceResolver createResolver(Object instance) { ... }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • true!

RefResolverHelper.createResolver(singletonMap("this", new Obj()), null);
assertTrue(probeCondition.execute(ctx));
}

Expand Down
Loading
Loading