
metrics_influxdb.serialization.line.Inliner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of metrics-influxdb Show documentation
Show all versions of metrics-influxdb Show documentation
A reporter for metrics which announces measurements to an InfluxDB server.
The newest version!
package metrics_influxdb.serialization.line;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import metrics_influxdb.measurements.Measure;
import metrics_influxdb.misc.Miscellaneous;
public class Inliner {
private static char[] ESCAPE_CHARS = {' ', ',', '='};
private TimeUnit precision;
public Inliner(TimeUnit precision) {
this.precision = precision;
}
public String inline(Measure m) {
String key = buildMeasureKey(m.getName(), m.getTags());
String values = buildMeasureFields(m.getValues());
String timestamp = "" + precision.convert(m.getTimestamp(), TimeUnit.MILLISECONDS);
return key + " " + values + " " + timestamp;
}
public String inline(Iterable measures) {
StringBuilder sb = new StringBuilder();
String join = "";
String cr = "\n";
for (Measure m : measures) {
sb.append(join).append(inline(m));
join = cr;
}
return sb.toString();
}
private String buildMeasureFields(Map values) {
Map sortedValues = new InfluxDBSortedMap();
sortedValues.putAll(values);
StringBuilder fields = new StringBuilder();
String join = "";
for (Map.Entry v: sortedValues.entrySet()) {
fields.append(join).append(Miscellaneous.escape(v.getKey(), ESCAPE_CHARS)).append("=").append(v.getValue()); // values are already escaped
join = ",";
}
return fields.toString();
}
private String buildMeasureKey(String name, Map tags) {
StringBuilder key = new StringBuilder(Miscellaneous.escape(name, ESCAPE_CHARS));
Map sortedTags = new InfluxDBSortedMap();
sortedTags.putAll(tags);
for (Map.Entry e: sortedTags.entrySet()) {
key.append(',').append(Miscellaneous.escape(e.getKey(), ESCAPE_CHARS)).append("=").append(Miscellaneous.escape(e.getValue(), ESCAPE_CHARS));
}
return key.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy