Add measurement valueMeta
Add optional valueMeta to Metric Removed arrays of timestamps and values Metric. It was undocumented and unused Implements: blueprint measurement-meta-data Change-Id: I5ae3cf6b18ba81b2f6ee707f3814655249d5d261
This commit is contained in:
parent
7f5cac26db
commit
cdd502e6a3
@ -15,13 +15,11 @@ package monasca.common.model.metric;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -34,32 +32,28 @@ public class Metric implements Serializable {
|
||||
public Map<String, String> dimensions;
|
||||
public long timestamp;
|
||||
public double value;
|
||||
public double[][] timeValues = null;
|
||||
public Map<String, String> valueMeta;
|
||||
private MetricDefinition definition;
|
||||
|
||||
public Metric() {}
|
||||
|
||||
public Metric(@NotNull MetricDefinition definition, long timestamp, double value) {
|
||||
public Metric(@NotNull MetricDefinition definition, long timestamp, double value,
|
||||
@Nullable Map<String, String> valueMeta) {
|
||||
this.definition = Preconditions.checkNotNull(definition, "definition");
|
||||
this.name = definition.name;
|
||||
setDimensions(definition.dimensions);
|
||||
this.timestamp = timestamp;
|
||||
this.value = Preconditions.checkNotNull(value, "value");
|
||||
this.value = value;
|
||||
this.valueMeta = valueMeta;
|
||||
}
|
||||
|
||||
public Metric(String name, @Nullable Map<String, String> dimensions, long timestamp, double value) {
|
||||
public Metric(String name, @Nullable Map<String, String> dimensions, long timestamp,
|
||||
double value, @Nullable Map<String, String> valueMeta) {
|
||||
this.name = Preconditions.checkNotNull(name, "name");
|
||||
setDimensions(dimensions);
|
||||
this.timestamp = timestamp;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Metric(String name, @Nullable Map<String, String> dimensions, long timestamp,
|
||||
double[][] timeValues) {
|
||||
this.name = Preconditions.checkNotNull(name, "name");
|
||||
setDimensions(dimensions);
|
||||
this.timestamp = Preconditions.checkNotNull(timestamp, "timestamp");
|
||||
this.timeValues = Preconditions.checkNotNull(timeValues, "timeValues");
|
||||
this.valueMeta = valueMeta;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,8 +68,7 @@ public class Metric implements Serializable {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Metric{" + "name='" + name + '\'' + ", dimensions=" + dimensions + ", timeStamp='"
|
||||
+ timestamp + '\'' + ", value=" + value + ", timeValues=" + Arrays.toString(timeValues)
|
||||
+ '}';
|
||||
+ timestamp + '\'' + ", value=" + value + ", valueMeta=" + valueMeta + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -102,7 +95,10 @@ public class Metric implements Serializable {
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
if (!Arrays.deepEquals(timeValues, other.timeValues))
|
||||
if (valueMeta == null) {
|
||||
if (other.valueMeta != null)
|
||||
return false;
|
||||
} else if (!valueMeta.equals(other.valueMeta))
|
||||
return false;
|
||||
if (timestamp != other.timestamp)
|
||||
return false;
|
||||
@ -118,7 +114,7 @@ public class Metric implements Serializable {
|
||||
result = prime * result + ((definition == null) ? 0 : definition.hashCode());
|
||||
result = prime * result + ((dimensions == null) ? 0 : dimensions.hashCode());
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
result = prime * result + Arrays.hashCode(timeValues);
|
||||
result = prime * result + ((valueMeta == null) ? 0 : valueMeta.hashCode());
|
||||
result = prime * result + (int) (timestamp ^ (timestamp >>> 32));
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(value);
|
||||
@ -158,13 +154,11 @@ public class Metric implements Serializable {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@JsonProperty("time_values")
|
||||
public double[][] getTimeValues() {
|
||||
return timeValues;
|
||||
public Map<String, String> getValueMeta() {
|
||||
return valueMeta;
|
||||
}
|
||||
|
||||
@JsonProperty("time_values")
|
||||
public void setTimeValues(double[][] timeValues) {
|
||||
this.timeValues = timeValues;
|
||||
public void setValueMeta(Map<String, String> valueMeta) {
|
||||
this.valueMeta = valueMeta;
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,6 @@
|
||||
package monasca.common.model.metric;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
|
||||
@ -55,23 +54,15 @@ public final class Metrics {
|
||||
jgen.writeStartObject();
|
||||
|
||||
jgen.writeStringField("name", value.name);
|
||||
if (value.dimensions != null && !value.dimensions.isEmpty())
|
||||
if (value.dimensions != null && !value.dimensions.isEmpty()) {
|
||||
jgen.writeObjectField("dimensions", value.dimensions);
|
||||
}
|
||||
jgen.writeNumberField("timestamp", value.timestamp);
|
||||
|
||||
if (value.timeValues == null)
|
||||
jgen.writeNumberField("value", value.value);
|
||||
else {
|
||||
jgen.writeArrayFieldStart("time_values");
|
||||
for (double[] timeValue : value.timeValues) {
|
||||
jgen.writeStartArray();
|
||||
jgen.writeNumber((long) timeValue[0]); // Write timestamp as a long
|
||||
jgen.writeNumber(timeValue[1]);
|
||||
jgen.writeEndArray();
|
||||
}
|
||||
jgen.writeEndArray();
|
||||
jgen.writeNumberField("value", value.value);
|
||||
if (value.valueMeta != null && !value.valueMeta.isEmpty()) {
|
||||
jgen.writeObjectField("value_meta", value.valueMeta);
|
||||
}
|
||||
|
||||
jgen.writeEndObject();
|
||||
}
|
||||
}
|
||||
@ -104,12 +95,4 @@ public final class Metrics {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a metric for the {@code metric} and {@code dimensions}.
|
||||
*/
|
||||
public static Metric of(Metric metric, Map<String, String> dimensions) {
|
||||
return metric.timeValues == null ? new Metric(metric.name, dimensions, metric.timestamp,
|
||||
metric.value) : new Metric(metric.name, dimensions, metric.timestamp, metric.timeValues);
|
||||
}
|
||||
}
|
||||
|
@ -30,26 +30,15 @@ public class MetricsTest {
|
||||
SortedMap<String, String> dimensions = new TreeMap<String, String>();
|
||||
dimensions.put("metric_name", "cpu");
|
||||
dimensions.put("instance_id", "123");
|
||||
Metric metric = new Metric("hpcs.compute", dimensions, 123345, 5);
|
||||
|
||||
SortedMap<String, String> valueMeta = new TreeMap<String, String>();
|
||||
valueMeta.put("result", "foobar");
|
||||
valueMeta.put("deklan", "123");
|
||||
Metric metric = new Metric("hpcs.compute", dimensions, 123345, 5, valueMeta);
|
||||
String json = Metrics.toJson(metric);
|
||||
assertEquals(
|
||||
json,
|
||||
"{\"name\":\"hpcs.compute\",\"dimensions\":{\"instance_id\":\"123\",\"metric_name\":\"cpu\"},\"timestamp\":123345,\"value\":5.0}");
|
||||
}
|
||||
|
||||
public void shouldSerializeTimeValues() {
|
||||
SortedMap<String, String> dimensions = new TreeMap<String, String>();
|
||||
dimensions.put("metric_name", "cpu");
|
||||
dimensions.put("device", "2");
|
||||
dimensions.put("instance_id", "123");
|
||||
Metric metric = new Metric("hpcs.compute", dimensions, 123345, new double[][] { { 123, 5 },
|
||||
{ 456, 6 } });
|
||||
|
||||
String json = Metrics.toJson(metric);
|
||||
assertEquals(
|
||||
json,
|
||||
"{\"name\":\"hpcs.compute\",\"dimensions\":{\"device\":\"2\",\"instance_id\":\"123\",\"metric_name\":\"cpu\"},\"timestamp\":123345,\"time_values\":[[123,5.0],[456,6.0]]}");
|
||||
"{\"name\":\"hpcs.compute\",\"dimensions\":{\"instance_id\":\"123\",\"metric_name\":\"cpu\"}," +
|
||||
"\"timestamp\":123345,\"value\":5.0,\"value_meta\":{\"deklan\":\"123\",\"result\":\"foobar\"}}");
|
||||
}
|
||||
|
||||
public void shouldSerializeAndDeserialize() {
|
||||
@ -57,8 +46,10 @@ public class MetricsTest {
|
||||
dimensions.put("metric_name", "cpu");
|
||||
dimensions.put("device", "2");
|
||||
dimensions.put("instance_id", "123");
|
||||
Metric expected = new Metric("hpcs.compute", dimensions, 123345, new double[][] { { 123, 5 },
|
||||
{ 456, 6 } });
|
||||
SortedMap<String, String> valueMeta = new TreeMap<String, String>();
|
||||
valueMeta.put("result", "foobar");
|
||||
valueMeta.put("deklan", "123");
|
||||
Metric expected = new Metric("hpcs.compute", dimensions, 123345, 55.0, valueMeta);
|
||||
|
||||
Metric metric = Metrics.fromJson(Metrics.toJson(expected).getBytes());
|
||||
assertEquals(metric, expected);
|
||||
@ -68,12 +59,15 @@ public class MetricsTest {
|
||||
SortedMap<String, String> dimensions = new TreeMap<String, String>();
|
||||
dimensions.put("metric_name", "foôbár");
|
||||
dimensions.put("instance_id", "123");
|
||||
Metric metric = new Metric("hpcs.compute", dimensions, 123345, 5);
|
||||
|
||||
SortedMap<String, String> valueMeta = new TreeMap<String, String>();
|
||||
valueMeta.put("result", "boôbár");
|
||||
valueMeta.put("deklan", "123");
|
||||
Metric metric = new Metric("hpcs.compute", dimensions, 123345, 5, valueMeta);
|
||||
String json = Metrics.toJson(metric);
|
||||
assertEquals(
|
||||
json,
|
||||
"{\"name\":\"hpcs.compute\",\"dimensions\":{\"instance_id\":\"123\",\"metric_name\":\"foôbár\"},\"timestamp\":123345,\"value\":5.0}");
|
||||
"{\"name\":\"hpcs.compute\",\"dimensions\":{\"instance_id\":\"123\",\"metric_name\":\"foôbár\"}," +
|
||||
"\"timestamp\":123345,\"value\":5.0,\"value_meta\":{\"deklan\":\"123\",\"result\":\"boôbár\"}}");
|
||||
}
|
||||
|
||||
public void shouldSerializeAndDeserializeUTF8() throws UnsupportedEncodingException {
|
||||
@ -81,8 +75,10 @@ public class MetricsTest {
|
||||
dimensions.put("metric_name", "foôbár");
|
||||
dimensions.put("device", "2");
|
||||
dimensions.put("instance_id", "123");
|
||||
Metric expected = new Metric("hpcs.compute", dimensions, 123345, new double[][] { { 123, 5 },
|
||||
{ 456, 6 } });
|
||||
SortedMap<String, String> valueMeta = new TreeMap<String, String>();
|
||||
valueMeta.put("result", "foôbár");
|
||||
valueMeta.put("deklan", "123");
|
||||
Metric expected = new Metric("hpcs.compute", dimensions, 123345, 42.0, valueMeta);
|
||||
|
||||
Metric metric;
|
||||
metric = Metrics.fromJson(Metrics.toJson(expected).getBytes("UTF-8"));
|
||||
@ -94,8 +90,10 @@ public class MetricsTest {
|
||||
dimensions.put("metric_name", "fo\u00f4b\u00e1r");
|
||||
dimensions.put("device", "2");
|
||||
dimensions.put("instance_id", "123");
|
||||
Metric expected = new Metric("hpcs.compute", dimensions, 123345, new double[][] { { 123, 5 },
|
||||
{ 456, 6 } });
|
||||
SortedMap<String, String> valueMeta = new TreeMap<String, String>();
|
||||
valueMeta.put("result", "fo\u00f4b\u00e1r");
|
||||
valueMeta.put("deklan", "123");
|
||||
Metric expected = new Metric("hpcs.compute", dimensions, 123345, 84.0, valueMeta);
|
||||
|
||||
Metric metric;
|
||||
metric = Metrics.fromJson(Metrics.toJson(expected).getBytes("UTF-8"));
|
||||
@ -111,10 +109,14 @@ public class MetricsTest {
|
||||
dimensions2.put("metric_name", "foôbár");
|
||||
dimensions2.put("device", "2");
|
||||
dimensions2.put("instance_id", "123");
|
||||
Metric expected_escaped = new Metric("hpcs.compute", dimensions, 123345, new double[][] {
|
||||
{ 123, 5 }, { 456, 6 } });
|
||||
Metric expected_nonescaped = new Metric("hpcs.compute", dimensions2, 123345, new double[][] {
|
||||
{ 123, 5 }, { 456, 6 } });
|
||||
SortedMap<String, String> valueMeta = new TreeMap<String, String>();
|
||||
valueMeta.put("result", "fo\u00f4b\u00e1r");
|
||||
valueMeta.put("deklan", "123");
|
||||
SortedMap<String, String> valueMeta2 = new TreeMap<String, String>();
|
||||
valueMeta2.put("result", "foôbár");
|
||||
valueMeta2.put("deklan", "123");
|
||||
Metric expected_escaped = new Metric("hpcs.compute", dimensions, 123345, 42.0, valueMeta);
|
||||
Metric expected_nonescaped = new Metric("hpcs.compute", dimensions2, 123345, 42.0, valueMeta2);
|
||||
|
||||
Metric metric;
|
||||
metric = Metrics.fromJson(Metrics.toJson(expected_escaped).getBytes("UTF-8"));
|
||||
|
Loading…
x
Reference in New Issue
Block a user