Allow spaces and apostrophes in dimensions

Change-Id: I0f6a00496e65eeadb946849e68107321310a1a11
This commit is contained in:
Ryan Brandt 2015-08-05 12:43:43 -06:00
parent de87b63c9b
commit 41457907ab
3 changed files with 44 additions and 5 deletions

View File

@ -82,8 +82,26 @@ class AlarmSubExpressionListener extends AlarmExpressionBaseListener {
@Override
public void enterDimension(AlarmExpressionParser.DimensionContext ctx) {
String dimensionName = ctx.getChild(0).getText();
if (dimensions.put(dimensionName, ctx.getChild(2).getText()) != null)
StringBuilder dimensionName = new StringBuilder();
dimensionName.append(ctx.getChild(0).getText());
int i = 1;
while (!ctx.getChild(i).getText().equals("=")) {
dimensionName.append(' ');
dimensionName.append(ctx.getChild(i).getText());
i++;
}
// move past the '=' token
i++;
StringBuilder dimensionValue = new StringBuilder();
dimensionValue.append(ctx.getChild(i).getText());
i++;
while (i < ctx.getChildCount()) {
dimensionValue.append(' ');
dimensionValue.append(ctx.getChild(i).getText());
i++;
}
if (dimensions.put(dimensionName.toString(), dimensionValue.toString()) != null)
throw new IllegalArgumentException("More than one value was given for dimension "
+ dimensionName);
}

View File

@ -97,7 +97,7 @@ dimensionList
;
dimension
: txt '=' txt
: (txt)+ '=' (txt)+
;
keyword
@ -210,7 +210,7 @@ DECIMAL
;
TXT
: ~('\'' | '}' | '{' | '&' | '|' | '>' | '<' | '=' | ',' | ')' | '(' | ' ' | '"' )+
: ~('}' | '{' | '&' | '|' | '>' | '<' | '=' | ',' | ')' | '(' | ' ' | '"' )+
;
STRING

View File

@ -24,11 +24,12 @@ import java.util.List;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import monasca.common.model.metric.MetricDefinition;
@Test
public class AlarmExpressionTest {
private final String restrictedChars = "(){}&|<>='\",";
private final String restrictedChars = "(){}&|<>=\",";
public void shouldParseExpression() {
AlarmExpression expr = new AlarmExpression(
@ -251,6 +252,26 @@ public class AlarmExpressionTest {
assertEquals(alarm1.getMetricDefinition(), expected1);
}
public void shouldParseDimensionsWithSpaces() {
AlarmExpression[] expr_list = {
new AlarmExpression("test_metric{this_is_a_test=this is a test} > 10"),
new AlarmExpression("test_metric{this is also a test = this_is_also_a_test} > 10")
};
MetricDefinition[] expected_list = {
new MetricDefinition("test_metric", ImmutableMap.<String,String>builder()
.put("this_is_a_test", "this is a test")
.build()),
new MetricDefinition("test_metric", ImmutableMap.<String,String>builder()
.put("this is also a test", "this_is_also_a_test")
.build())
};
for(int i = 0; i < expr_list.length; i++) {
AlarmSubExpression expr = expr_list[i].getSubExpressions().get(0);
assertEquals(expr.getMetricDefinition(), expected_list[i]);
}
}
public void shouldFailWithRestrictedChars() {
String[] expressions = {"%cmetric{foo=bar,metric_name=mem} > 4",
"me%ctric{foo=bar,metric_name=mem} > 4",