Allow unicode in metric definitions

Change-Id: I7d3ae6b4010191974d75f2b0a7fb8df144fadf23
This commit is contained in:
Ryan Brandt 2015-04-16 12:41:33 -06:00
parent fd847d4ec1
commit 9d1172c219
5 changed files with 10 additions and 9 deletions

View File

@ -34,7 +34,8 @@ public final class DimensionValidation {
private static final Map<String, DimensionValidator> VALIDATORS;
private static final Pattern UUID_PATTERN = Pattern
.compile("\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}");
private static final Pattern VALID_DIMENSION_NAME = Pattern.compile("^[a-zA-Z0-9_\\.\\-]+$");
private static final Pattern VALID_DIMENSION_NAME = Pattern.compile("[^><={}(), '\";&]+$");
private static final String INVALID_CHAR_STRING = "> < = { } ( ) ' \" , ; &";
private DimensionValidation() {}
@ -135,7 +136,7 @@ public final class DimensionValidation {
}
if (!VALID_DIMENSION_NAME.matcher(name).matches())
throw Exceptions.unprocessableEntity(
"Dimension name %s may only contain: a-z A-Z 0-9 _ - .", name);
"Dimension name %s may not contain: %s", name, INVALID_CHAR_STRING);
// Service specific validations
if (service != null) {
@ -173,7 +174,7 @@ public final class DimensionValidation {
}
if (!VALID_DIMENSION_NAME.matcher(name).matches())
throw Exceptions.unprocessableEntity(
"Dimension name '%s' may only contain: a-z A-Z 0-9 _ - .", name);
"Dimension name '%s' may not contain: %s", name, INVALID_CHAR_STRING);
}
}
}

View File

@ -28,7 +28,7 @@ import com.sun.jersey.spi.container.WebApplication;
* Utilities for validating metric names.
*/
public class MetricNameValidation {
private static final Pattern VALID_METRIC_NAME = Pattern.compile("^[a-zA-Z0-9_\\.\\-]+$");
private static final Pattern VALID_METRIC_NAME = Pattern.compile("[^><={}(), '\";&]+$");
private MetricNameValidation() {}
@ -60,7 +60,7 @@ public class MetricNameValidation {
throw Exceptions.unprocessableEntity("Metric name %s must be %d characters or less",
metricName, CreateMetricCommand.MAX_NAME_LENGTH);
if (!Services.isReserved(metricName) && !VALID_METRIC_NAME.matcher(metricName).matches())
throw Exceptions.unprocessableEntity("Metric name %s may only contain: a-z A-Z 0-9 _ - .",
throw Exceptions.unprocessableEntity("Metric name %s may not contain: > < = { } ( ) ' \" , ; &",
metricName);
// Service specific validations

View File

@ -157,7 +157,7 @@ public class InfluxV9RepoReader {
.debug("Successfully queried influx database {} at {}", this.influxName, this.influxUrl);
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
return entity != null ? EntityUtils.toString(entity, "UTF-8") : null;
} finally {

View File

@ -183,7 +183,7 @@ public class AlarmDefinitionResourceTest extends AbstractMonApiResourceTest {
public void shouldErrorOnCreateWithInvalidOperator() {
String expression =
"avg(hpcs.compute{instance_id=937, az=2, instance_uuid=0ff588fc-d298-482f-bb11-4b52d56801a4, metric_name=disk_read_ops}) & 90";
"avg(hpcs.compute{instance_id=937, az=2, instance_uuid=0ff588fc-d298-482f-bb11-4b52d56801a4, metric_name=disk_read_ops}) ^ 90";
ClientResponse response =
createResponseFor(new CreateAlarmDefinitionCommand("Disk Exceeds 1k Operations", null,
expression, Arrays.asList("service", "instance_id"), "LOW", alarmActions, null, null));

View File

@ -160,11 +160,11 @@ public class MetricResourceTest extends AbstractMonApiResourceTest {
public void shouldErrorOnCreateWithIllegalCharsInName() {
ClientResponse response =
createResponseFor(new CreateMetricCommand("hpcs@.compute%", dimensions, timestamp, 22.0,
createResponseFor(new CreateMetricCommand("hpcs{.compute%", dimensions, timestamp, 22.0,
valueMeta));
ErrorMessages.assertThat(response.getEntity(String.class)).matches("unprocessable_entity", 422,
"Metric name hpcs@.compute% may only contain: a-z A-Z 0-9 _ - .");
"Metric name hpcs{.compute% may not contain: > < = { } ( ) ' \" , ; &");
}
public void shouldErrorOnCreateWithTooLongName() {