Make alarm expression parsing less restrictive

Allow more than alphanumeric characters in the metric and dimensions names

Change-Id: If8903e3937358050baf1d98e295efd4021e90098
This commit is contained in:
Ryan Brandt 2015-04-09 09:35:18 -06:00
parent 92b5934545
commit 986b1caf31
3 changed files with 46 additions and 22 deletions

View File

@ -21,11 +21,11 @@ start
: expression EOF
;
expression
: compoundIdentifier relational_operator literal # relationalExprFwd
| function relational_operator literal ('times' repeat)? # relationalExprFuncFwd
| literal relational_operator compoundIdentifier # relationalExprBwd
expression
: function relational_operator literal ('times' repeat)? # relationalExprFuncFwd
| literal relational_operator function ('times' repeat)? # relationalExprFuncBwd
| compoundIdentifier relational_operator literal # relationalExprFwd
| literal relational_operator compoundIdentifier # relationalExprBwd
| expression and expression # andExpr
| expression or expression # orExpr
| '(' expression ')' # parenExpr
@ -132,7 +132,7 @@ repeat
txt
: TXT
| keyword
| INTEGER
| INTEGER
| STRING
;
LT
@ -208,11 +208,11 @@ INTEGER
;
DECIMAL
: '-'?[0-9]+('.'[0-9]+)?
: '-'?DIGIT+('.'DIGIT+)?
;
TXT
: [//a-zA-Z_$/\\0-9]~('\''|';' | '}' | '{' | '=' | ','| '&' | ')' | '(' |' '| '"' )+
: ~('}' | '{' | '=' | ',' | ')' | '(' | ' ')*
;
STRING

View File

@ -174,10 +174,10 @@ public class AlarmExpressionTest {
.build()));
assertFalse(expr.evaluate(ImmutableMap.<AlarmSubExpression, Boolean>builder()
.put(alarm1, false)
.put(alarm2, true)
.put(alarm3, false)
.build()));
.put(alarm1, false)
.put(alarm2, true)
.put(alarm3, false)
.build()));
}
public void shouldDefaultPeriodAndPeriods() {
@ -193,13 +193,17 @@ public class AlarmExpressionTest {
AlarmExpression expr = new AlarmExpression(
"avg(hpcs.compute{instance_id=5,metric_name=cpu,device=2}, 1) > 5 times 3 and avg(hpcs.compute{flavor_id=3,metric_name=mem}, 2) < 4 times 3");
expr.evaluate(ImmutableMap.<AlarmSubExpression, Boolean>builder()
.put(
new AlarmSubExpression(AggregateFunction.AVG, new MetricDefinition("hpcs.compute",
ImmutableMap.<String, String>builder()
.put("flavor_id", "3")
.put("metric_name", "mem")
.build()), AlarmOperator.LT, 4, 2, 3), true)
.build());
.put(
new AlarmSubExpression(AggregateFunction.AVG,
new MetricDefinition("hpcs.compute",
ImmutableMap
.<String, String>builder()
.put("flavor_id", "3")
.put("metric_name",
"mem")
.build()),
AlarmOperator.LT, 4, 2, 3), true)
.build());
}
public void shouldGetAlarmExpressionTree() {
@ -230,4 +234,24 @@ public class AlarmExpressionTest {
"avg(hpcs.compute{instance_id=5,metric_name=cpu,device=a}, 1) lt 5 times 444 and avg(hpcs.compute{flavor_id=3,metric_name=mem}, 2) < 4 times 3");
assertNotEquals(expr1, expr3);
}
public void shouldParseDimensionsWithUnicode() {
AlarmExpression expr1 = new AlarmExpression(
"公{此=该,metric_name=mem} > 4"
);
AlarmSubExpression alarm1 = expr1.getSubExpressions().get(0);
MetricDefinition expected1 = new MetricDefinition("",
ImmutableMap.<String, String>builder()
.put("", "")
.put("metric_name", "mem")
.build());
assertEquals(alarm1.getMetricDefinition(), expected1);
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void shouldFailWithRestrictedChars() {
AlarmExpression expr1 = new AlarmExpression(
"metric{\u00A0\u007Dthing=\u00AEstuff,metric_name=mem} > 4"
);
}
}

View File

@ -118,8 +118,8 @@ public class AlarmSubExpressionTest {
}
public void shouldParseExpressionKeywordNamespace() {
AlarmSubExpression expr = AlarmSubExpression.of("avg(avg{metric_name=cpu, instance_id=5}, 1) > 5 times 3");
assertEquals(expr, new AlarmSubExpression(AggregateFunction.AVG, new MetricDefinition("avg",
AlarmSubExpression expr = AlarmSubExpression.of("avg(count{metric_name=cpu, instance_id=5}, 1) > 5 times 3");
assertEquals(expr, new AlarmSubExpression(AggregateFunction.AVG, new MetricDefinition("count",
ImmutableMap.<String, String>builder()
.put("instance_id", "5")
.put("metric_name", "cpu")
@ -127,12 +127,12 @@ public class AlarmSubExpressionTest {
}
public void shouldParseExpressionKeywordMetricType() {
AlarmSubExpression expr = AlarmSubExpression.of("avg(hpcs.compute{metric_name=avg, instance_id=5}, 1) > 5 times 3");
AlarmSubExpression expr = AlarmSubExpression.of("avg(hpcs.compute{metric_name=count, instance_id=5}, 1) > 5 times 3");
assertEquals(expr,
new AlarmSubExpression(AggregateFunction.AVG, new MetricDefinition("hpcs.compute",
ImmutableMap.<String, String>builder()
.put("instance_id", "5")
.put("metric_name", "avg")
.put("metric_name", "count")
.build()), AlarmOperator.GT, 5, 1, 3));
}