Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -39,26 +39,29 @@ public Boolean evaluate(ValueReferenceResolver valueRefResolver) {
// always return FALSE for empty collection
return Boolean.FALSE;
}
int len = collection.count();
try {
int len = collection.count();
for (int i = 0; i < len; i++) {
valueRefResolver.addExtension(ValueReferences.ITERATOR_EXTENSION_NAME, collection.get(i));
if (filterPredicateExpression.evaluate(valueRefResolver)) {
return Boolean.TRUE;
}
}
return Boolean.FALSE;

} catch (IllegalArgumentException | UnsupportedOperationException ex) {
throw new EvaluationException(ex.getMessage(), print(this));
} finally {
valueRefResolver.removeExtension(ValueReferences.ITERATOR_EXTENSION_NAME);
}
}
if (value instanceof MapValue) {
MapValue map = (MapValue) value;
checkSupportedMap(map, this);
if (map.isEmpty()) {
return Boolean.FALSE;
}
try {
if (map.isEmpty()) {
return Boolean.FALSE;
}
for (Value<?> key : map.getKeys()) {
Value<?> val = key.isUndefined() ? Value.undefinedValue() : map.get(key);
valueRefResolver.addExtension(ValueReferences.KEY_EXTENSION_NAME, key);
Expand All @@ -70,6 +73,8 @@ public Boolean evaluate(ValueReferenceResolver valueRefResolver) {
}
}
return Boolean.FALSE;
} catch (IllegalArgumentException | UnsupportedOperationException ex) {
throw new EvaluationException(ex.getMessage(), print(this));
} finally {
valueRefResolver.removeExtension(ValueReferences.ITERATOR_EXTENSION_NAME);
valueRefResolver.removeExtension(ValueReferences.KEY_EXTENSION_NAME);
Expand All @@ -79,17 +84,19 @@ public Boolean evaluate(ValueReferenceResolver valueRefResolver) {
if (value instanceof SetValue) {
SetValue set = (SetValue) value;
Set<?> setHolder = checkSupportedSet(set, this);
if (set.isEmpty()) {
return Boolean.FALSE;
}
try {
if (set.isEmpty()) {
return Boolean.FALSE;
}
for (Object val : setHolder) {
valueRefResolver.addExtension(ValueReferences.ITERATOR_EXTENSION_NAME, Value.of(val));
if (filterPredicateExpression.evaluate(valueRefResolver)) {
return Boolean.TRUE;
}
}
return Boolean.FALSE;
} catch (IllegalArgumentException | UnsupportedOperationException ex) {
throw new EvaluationException(ex.getMessage(), print(this));
} finally {
valueRefResolver.removeExtension(ValueReferences.ITERATOR_EXTENSION_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.datadog.debugger.el.expressions;

import static com.datadog.debugger.el.expressions.CollectionExpressionHelper.checkSupportedList;
import static com.datadog.debugger.el.expressions.CollectionExpressionHelper.checkSupportedMap;

import com.datadog.debugger.el.EvaluationException;
import com.datadog.debugger.el.PrettyPrintVisitor;
import com.datadog.debugger.el.Value;
Expand Down Expand Up @@ -41,17 +44,21 @@ public Value<?> evaluate(ValueReferenceResolver valueRefResolver) {
if (objKey instanceof String && Redaction.isRedactedKeyword((String) objKey)) {
ExpressionHelper.throwRedactedException(this);
} else {
result = ((MapValue) targetValue).get(objKey);
MapValue mapValue = (MapValue) targetValue;
checkSupportedMap(mapValue, this);
result = mapValue.get(objKey);
}
} else if (targetValue instanceof ListValue) {
result = ((ListValue) targetValue).get(keyValue.getValue());
ListValue listValue = (ListValue) targetValue;
checkSupportedList(listValue, this);
result = listValue.get(keyValue.getValue());
} else {
throw new EvaluationException(
"Cannot evaluate the expression for unsupported type: "
+ targetValue.getClass().getTypeName(),
PrettyPrintVisitor.print(this));
}
} catch (IllegalArgumentException ex) {
} catch (IllegalArgumentException | UnsupportedOperationException ex) {
throw new EvaluationException(ex.getMessage(), PrettyPrintVisitor.print(this), ex);
}
Object obj = result.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public int count() {
if (WellKnownClasses.isSafe((Collection<?>) listHolder)) {
return ((Collection<?>) listHolder).size();
} else {
throw new RuntimeException(
throw new UnsupportedOperationException(
"Unsupported Collection class: " + listHolder.getClass().getTypeName());
}
} else if (listHolder == Value.nullValue()) {
Expand Down Expand Up @@ -136,14 +136,14 @@ public boolean contains(Value<?> val) {
if (WellKnownClasses.isSafe((Collection<?>) listHolder)) {
return ((Collection<?>) listHolder).contains(val.isNull() ? null : val.getValue());
}
throw new RuntimeException(
throw new UnsupportedOperationException(
"Unsupported Collection class: " + listHolder.getClass().getTypeName());
}
if (arrayHolder != null) {
int count = Array.getLength(arrayHolder);
if (arrayType.isPrimitive()) {
if (val.getValue() == null || val.isNull()) {
throw new RuntimeException("Cannot compare null with primitive array");
throw new IllegalArgumentException("Cannot compare null with primitive array");
}
if (arrayType == byte.class) {
byte byteValue = (Byte) val.getValue();
Expand Down Expand Up @@ -224,7 +224,8 @@ public boolean contains(Value<?> val) {
}
}
}
throw new RuntimeException("Unsupported value class: " + objValue.getClass().getTypeName());
throw new UnsupportedOperationException(
"Unsupported value class: " + objValue.getClass().getTypeName());
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public boolean isEmpty() {
if (WellKnownClasses.isSafe((Map<?, ?>) mapHolder)) {
return ((Map<?, ?>) mapHolder).isEmpty();
}
throw new RuntimeException("Unsupported Map class: " + mapHolder.getClass().getTypeName());
throw new UnsupportedOperationException(
"Unsupported Map class: " + mapHolder.getClass().getTypeName());
} else if (mapHolder instanceof Value) {
Value<?> val = (Value<?>) mapHolder;
return val.isNull() || val.isUndefined();
Expand All @@ -60,7 +61,8 @@ public int count() {
if (WellKnownClasses.isSafe((Map<?, ?>) mapHolder)) {
return ((Map<?, ?>) mapHolder).size();
}
throw new RuntimeException("Unsupported Map class: " + mapHolder.getClass().getTypeName());
throw new UnsupportedOperationException(
"Unsupported Map class: " + mapHolder.getClass().getTypeName());
} else if (mapHolder == Value.nullValue()) {
return 0;
}
Expand All @@ -73,7 +75,8 @@ public Set<Value<?>> getKeys() {
Map<?, ?> map = (Map<?, ?>) mapHolder;
return map.keySet().stream().map(Value::of).collect(Collectors.toSet());
}
throw new RuntimeException("Unsupported Map class: " + mapHolder.getClass().getTypeName());
throw new UnsupportedOperationException(
"Unsupported Map class: " + mapHolder.getClass().getTypeName());
}
log.warn("{} is not a map", mapHolder);
return Collections.singleton(Value.undefinedValue());
Expand All @@ -94,7 +97,8 @@ public Value<?> get(Object key) {
Object value = map.get(key);
return value != null ? Value.of(value) : Value.nullValue();
}
throw new RuntimeException("Unsupported Map class: " + mapHolder.getClass().getTypeName());
throw new UnsupportedOperationException(
"Unsupported Map class: " + mapHolder.getClass().getTypeName());
}
// the result will be either Value.nullValue() or Value.undefinedValue() depending on the holder
// value
Expand All @@ -112,10 +116,11 @@ public boolean contains(Value<?> val) {
if (WellKnownClasses.isEqualsSafe(val.getValue().getClass())) {
return map.containsKey(val.getValue());
}
throw new RuntimeException(
throw new UnsupportedOperationException(
"Unsupported key class: " + val.getValue().getClass().getTypeName());
}
throw new RuntimeException("Unsupported Map class: " + mapHolder.getClass().getTypeName());
throw new UnsupportedOperationException(
"Unsupported Map class: " + mapHolder.getClass().getTypeName());
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public boolean isEmpty() {
if (WellKnownClasses.isSafe((Collection<?>) setHolder)) {
return ((Set<?>) setHolder).isEmpty();
}
throw new RuntimeException("Unsupported Set class: " + setHolder.getClass().getTypeName());
throw new UnsupportedOperationException(
"Unsupported Set class: " + setHolder.getClass().getTypeName());
} else if (setHolder instanceof Value) {
Value<?> val = (Value<?>) setHolder;
return val.isNull() || val.isUndefined();
Expand All @@ -53,7 +54,8 @@ public int count() {
if (WellKnownClasses.isSafe((Collection<?>) setHolder)) {
return ((Set<?>) setHolder).size();
}
throw new RuntimeException("Unsupported Set class: " + setHolder.getClass().getTypeName());
throw new UnsupportedOperationException(
"Unsupported Set class: " + setHolder.getClass().getTypeName());
} else if (setHolder == Value.nullValue()) {
return 0;
}
Expand All @@ -75,7 +77,8 @@ public Value<?> get(Object key) {
key = key instanceof Value ? ((Value<?>) key).getValue() : key;
return Value.of(set.contains(key));
}
throw new RuntimeException("Unsupported Set class: " + setHolder.getClass().getTypeName());
throw new UnsupportedOperationException(
"Unsupported Set class: " + setHolder.getClass().getTypeName());
}
// the result will be either Value.nullValue() or Value.undefinedValue() depending on the holder
// value
Expand All @@ -92,10 +95,11 @@ public boolean contains(Value<?> val) {
if (WellKnownClasses.isEqualsSafe(val.getValue().getClass())) {
return ((Set<?>) setHolder).contains(val.getValue());
}
throw new RuntimeException(
throw new UnsupportedOperationException(
"Unsupported value class: " + val.getValue().getClass().getTypeName());
}
throw new RuntimeException("Unsupported Set class: " + setHolder.getClass().getTypeName());
throw new UnsupportedOperationException(
"Unsupported Set class: " + setHolder.getClass().getTypeName());
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.datadog.debugger.el.RedactedException;
import com.datadog.debugger.el.RefResolverHelper;
import com.datadog.debugger.el.Value;
import com.datadog.debugger.el.values.ListValue;
import com.datadog.debugger.el.values.MapValue;
import com.datadog.debugger.el.values.NumericValue;
import com.datadog.debugger.el.values.SetValue;
import com.datadog.debugger.el.values.StringValue;
Expand Down Expand Up @@ -77,7 +79,7 @@ void testMap() {
}

@Test
void testUnsupported() {
void testUnsupportedSet() {
IndexExpression expr =
new IndexExpression(new SetValue(new HashSet<>()), new StringValue("foo"));
EvaluationException evaluationException =
Expand All @@ -88,6 +90,40 @@ void testUnsupported() {
evaluationException.getMessage());
}

@Test
void testUnsupportedList() {
IndexExpression expr =
new IndexExpression(new ListValue(new ArrayList<String>() {}), new NumericValue(0));
EvaluationException evaluationException =
assertThrows(
EvaluationException.class, () -> expr.evaluate(RefResolverHelper.createResolver(this)));
assertEquals(
"Unsupported List class: com.datadog.debugger.el.expressions.IndexExpressionTest$1",
evaluationException.getMessage());
}

@Test
void testOutOfBoundsList() {
IndexExpression expr =
new IndexExpression(new ListValue(new ArrayList<String>()), new NumericValue(42));
EvaluationException evaluationException =
assertThrows(
EvaluationException.class, () -> expr.evaluate(RefResolverHelper.createResolver(this)));
assertEquals("index[42] out of bounds: [0-0]", evaluationException.getMessage());
}

@Test
void testUnsupportedMap() {
IndexExpression expr =
new IndexExpression(new MapValue(new HashMap<String, String>() {}), new StringValue("foo"));
EvaluationException evaluationException =
assertThrows(
EvaluationException.class, () -> expr.evaluate(RefResolverHelper.createResolver(this)));
assertEquals(
"Unsupported Map class: com.datadog.debugger.el.expressions.IndexExpressionTest$2",
evaluationException.getMessage());
}

@Test
void undefined() {
IndexExpression expr = new IndexExpression(ValueExpression.UNDEFINED, new NumericValue(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public String evaluate(CapturedContext context, LogProbe.LogStatus status) {
sb, segment.getParsedExpr().getDsl(), result.getValue(), status, limits);
}
} catch (EvaluationException ex) {
status.addError(new EvaluationError(ex.getExpr(), ex.getMessage()));
String msg =
ex instanceof RedactedException ? Redaction.REDACTED_VALUE : ex.getMessage();
sb.append('{').append(msg).append('}');
handleException(status, ex, ex.getExpr(), sb);
} catch (Exception ex) {
// catch all for unexpected exceptions
handleException(status, ex, segment.getExpr(), sb);
}
if (!status.getErrors().isEmpty()) {
status.setLogTemplateErrors(true);
Expand All @@ -67,4 +67,11 @@ public String evaluate(CapturedContext context, LogProbe.LogStatus status) {
}
return sb.toString();
}

private static void handleException(
LogProbe.LogStatus status, Exception ex, String expr, StringBuilder sb) {
status.addError(new EvaluationError(expr, ex.getMessage()));
String msg = ex instanceof RedactedException ? Redaction.REDACTED_VALUE : ex.getMessage();
sb.append('{').append(msg).append('}');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,11 @@ private boolean evaluateCondition(CapturedContext capture, LogStatus status) {
status.addError(new EvaluationError(ex.getExpr(), ex.getMessage()));
status.setConditionErrors(true);
return false;
} catch (Exception ex) {
// catch all for unexpected exceptions
status.addError(new EvaluationError(probeCondition.getDslExpression(), ex.getMessage()));
status.setConditionErrors(true);
return false;
}
return true;
}
Expand Down Expand Up @@ -685,6 +690,11 @@ private void processCaptureExpressions(CapturedContext context, LogStatus logSta
} catch (EvaluationException ex) {
logStatus.addError(new EvaluationError(ex.getExpr(), ex.getMessage()));
logStatus.setLogTemplateErrors(true);
} catch (Exception ex) {
// catch all for unexpected exceptions
logStatus.addError(
new EvaluationError(captureExpression.getExpr().getDsl(), ex.getMessage()));
logStatus.setLogTemplateErrors(true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ public void evaluate(
} catch (EvaluationException ex) {
status.addError(new EvaluationError(ex.getExpr(), ex.getMessage()));
continue;
} catch (Exception ex) {
// catch all for unexpected exceptions
status.addError(new EvaluationError(decoration.when.getDslExpression(), ex.getMessage()));
continue;
}
}
if (!(status instanceof SpanDecorationStatus)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.datadog.debugger.agent.DebuggerAgent;
import com.datadog.debugger.agent.Generated;
import com.datadog.debugger.el.EvaluationException;
import com.datadog.debugger.el.ProbeCondition;
import com.datadog.debugger.instrumentation.CapturedContextInstrumenter;
import com.datadog.debugger.instrumentation.DiagnosticMessage;
Expand Down Expand Up @@ -122,7 +121,7 @@ private boolean evaluateCondition(CapturedContext capture) {
long start = System.nanoTime();
try {
return probeCondition.execute(capture);
} catch (EvaluationException ex) {
} catch (Exception ex) {
DebuggerAgent.getSink().getProbeStatusSink().addError(probeId, ex);
return false;
} finally {
Expand Down
Loading