@Override public void serialize(SampleDTO value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeStartObject(); jgen.writeStringField("name", value.getName()); jgen.writeNumberField("timestamp", value.getTimestamp()); jgen.writeStringField("type", value.getType().toString()); jgen.writeObjectField("value", value.getValue()); // Since attributes is optional, be compact and omit from JSON output when unused. if (value.getAttributes() != null && !value.getAttributes().isEmpty()) { jgen.writeObjectField("attributes", value.getAttributes()); } // Omit the context field when it is set to the default if (!Context.DEFAULT_CONTEXT.equals(value.getContext())) { jgen.writeStringField("context", value.getContext().getId()); } jgen.writeEndObject(); }
public static ByteBuffer decompose(ValueType<?> value) { ByteBuffer buffer; switch (value.getType()) { case ABSOLUTE: case COUNTER: case DERIVE: buffer = ByteBuffer.allocate(9); buffer.put(0, value.getType().getCode()); buffer.putLong(1, value.longValue()); buffer.rewind(); return buffer; case GAUGE: buffer = ByteBuffer.allocate(9); buffer.put(0, value.getType().getCode()); buffer.putDouble(1, value.doubleValue()); buffer.rewind(); return buffer; default: throw new IllegalArgumentException(String.format("Unknown metric type: %s", value.getType())); } }
public static ValueType<?> compose(ByteBuffer data) { ByteBuffer buffer = data.duplicate(); MetricType type = MetricType.fromCode(buffer.get()); switch (type) { case ABSOLUTE: return new Absolute(UnsignedLong.fromLongBits(buffer.getLong())); case COUNTER: return new Counter(UnsignedLong.fromLongBits(buffer.getLong())); case DERIVE: return new Derive(UnsignedLong.fromLongBits(buffer.getLong())); case GAUGE: return new Gauge(buffer.getDouble()); default: throw new IllegalArgumentException(String.format("Unknown metric type: %s", type)); } }
@Override public String call(List<Sample> samples) { JSONBuilder bldr = new JSONBuilder(); for(Sample sample : samples) { if (isNaN(sample)) continue; //System.err.println("Importing: " + sample); bldr.newObject(); bldr.attr("timestamp", sample.getTimestamp().asMillis()); bldr.attr("resource", sample.getResource().getId()); bldr.attr("name", sample.getName()); bldr.attr("type", sample.getType().name()); if (sample.getType() == MetricType.GAUGE) { bldr.attr("value", sample.getValue().doubleValue()); } else { bldr.attr("value", sample.getValue().longValue()); } } return bldr.toString(); } };
@Override public void serialize(SampleDTO value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeStartObject(); jgen.writeStringField("name", value.getName()); jgen.writeNumberField("timestamp", value.getTimestamp()); jgen.writeStringField("type", value.getType().toString()); jgen.writeObjectField("value", value.getValue()); // Since attributes is optional, be compact and omit from JSON output when unused. if (value.getAttributes() != null && !value.getAttributes().isEmpty()) { jgen.writeObjectField("attributes", value.getAttributes()); } // Omit the context field when it is set to the default if (!Context.DEFAULT_CONTEXT.equals(value.getContext())) { jgen.writeStringField("context", value.getContext().getId()); } jgen.writeEndObject(); }