Check for backslash in metric names

Also, update documentation with a warning about using control characters in metrics

Change-Id: If796348cde90fdd5d50bc51ab66be2951b9db1ab
This commit is contained in:
Ryan Brandt 2015-05-06 11:21:59 -06:00
parent f93ec0db47
commit 866590cad0
4 changed files with 7 additions and 7 deletions

View File

@ -331,10 +331,10 @@ Before using the API, you must first get a valid auth token from Keystone. All A
A metric is uniquely identified by a name and set of dimensions.
### Name
Defines the name of a metric. A name is of type string(255). The name may include any characters except the following: `> < = { } ( ) , ' " ; &`. If one of the restricted characters is needed, this can be achieved by double quoting the name.
Defines the name of a metric. A name is of type string(255). The name may include any characters except the following: `> < = { } ( ) , ' " \ ; &`. A metric name surrounded by double quotes may include the following: `> < = { } ( ) , ; &`, but not `" \`. Note that JSON does allow control characters (such as `\n`), however these should not be used in metric names.
### Dimensions
A dictionary of (key, value) pairs. The key and value are of type string(255). Dimension keys may not begin with '_' (underscore). The dimensions string may include any characters except the following: `> < = { } ( ) , ' " ; &`. If one of the restricted characters is needed, this can be achieved by double quoting the dimension key or value containing the character.
A dictionary of (key, value) pairs. The key and value are of type string(255). Dimension keys may not begin with '_' (underscore). The dimension key and value strings may include any characters except the following: `> < = { } ( ) , ' " \ ; &`. A dimension key or value surrounded by double quotes may include the following: `> < = { } ( ) , ; &`, but not `" \`. Note that JSON does allow control characters (such as `\n`), however these should not be used in dimension keys or values.
### Text Representation
In this document, metrics will be represented in the form `name{name=value,name=value}` where name is the metric name and the name=value pairs in the curly braces are the dimensions. For example, `cpu.idle_perc{service=monitoring,hostname=mini-mon}` represents a metric with the name "cpu.idle_perc" and the dimensions "service=monitoring" and "hostname=mini-mon".

View File

@ -34,8 +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("[^><={}(), '\";&]+$");
private static final String INVALID_CHAR_STRING = "> < = { } ( ) ' \" , ; &";
private static final Pattern VALID_DIMENSION_NAME = Pattern.compile("[^><={}(), '\"\\\\;&]+$");
private static final String INVALID_CHAR_STRING = "> < = { } ( ) ' \" \\ , ; &";
private DimensionValidation() {}

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("[^><={}(), '\";&]+$");
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 not contain: > < = { } ( ) ' \" , ; &",
throw Exceptions.unprocessableEntity("Metric name %s may not contain: > < = { } ( ) ' \" \\ , ; &",
metricName);
// Service specific validations

View File

@ -165,7 +165,7 @@ public class MetricResourceTest extends AbstractMonApiResourceTest {
valueMeta));
ErrorMessages.assertThat(response.getEntity(String.class)).matches("unprocessable_entity", 422,
"Metric name hpcs{.compute% may not contain: > < = { } ( ) ' \" , ; &");
"Metric name hpcs{.compute% may not contain: > < = { } ( ) ' \" \\ , ; &");
}
public void shouldErrorOnCreateWithTooLongName() {