Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.metrics; |
| 2 | |
| 3 | import com.codahale.metrics.json.MetricsModule; |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | import org.codehaus.jackson.JsonGenerator; |
| 6 | import org.codehaus.jackson.map.SerializerProvider; |
| 7 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
| 8 | |
| 9 | import java.io.IOException; |
| 10 | import java.util.concurrent.TimeUnit; |
| 11 | |
| 12 | /** |
| 13 | * JSON serializer for the Metrics resource. |
| 14 | */ |
| 15 | public class MetricsObjectSerializer extends SerializerBase<MetricsObjectResource> { |
| 16 | |
| 17 | /** |
| 18 | * Public constructor - just calls its super class constructor. |
| 19 | */ |
| 20 | public MetricsObjectSerializer() { |
| 21 | super(MetricsObjectResource.class); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Convenience method to serialize a Metrics field. |
| 26 | * |
| 27 | * @param jsonGenerator generator to use for serialization |
| 28 | * @param fieldName name of the top level field |
| 29 | * @param serializedObjectJSON JSON representation from the Metrics serializer |
| 30 | * @param object internal resource for the Metric |
| 31 | * @throws IOException if JSON generation fails. |
| 32 | */ |
| 33 | private void serializeItem(final JsonGenerator jsonGenerator, |
| 34 | final String fieldName, |
| 35 | final String serializedObjectJSON, |
| 36 | final MetricsObjectResource.BaseMetricObject object) |
| 37 | throws IOException { |
| 38 | jsonGenerator.writeStartObject(); |
| 39 | jsonGenerator.writeStringField("name", object.getName()); |
| 40 | |
| 41 | // If you write the JSON for the Metric using a StringField, the |
| 42 | // generator applies an extra set of double quotes and breaks the |
| 43 | // syntax. You have to use the raw JSON output to get it right. |
| 44 | jsonGenerator.writeRaw(",\"" + fieldName + "\": " + serializedObjectJSON); |
| 45 | jsonGenerator.writeEndObject(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Serialize a MetricsObjectResource into JSON. For each kind of Metric, |
| 50 | * his serializes common ONOS defined fields like name and |
| 51 | * then calls the Metrics serializer to make the JSON string |
| 52 | * for the actual Metric. |
| 53 | * |
| 54 | * @param metrics resource for all ONOS Metrics |
| 55 | * @param jsonGenerator generator to use for the JSON output |
| 56 | * @param serializerProvider unused, needed for Override |
| 57 | * @throws IOException if any of the JSON serializations fail |
| 58 | */ |
| 59 | @Override |
| 60 | @SuppressWarnings("rawtypes") |
| 61 | public void serialize(final MetricsObjectResource metrics, |
| 62 | final JsonGenerator jsonGenerator, |
| 63 | final SerializerProvider serializerProvider) |
| 64 | throws IOException { |
| 65 | |
| 66 | final ObjectMapper mapper = new ObjectMapper().registerModule( |
| 67 | new MetricsModule(TimeUnit.SECONDS, TimeUnit.MILLISECONDS, false)); |
| 68 | jsonGenerator.writeStartObject(); |
| 69 | |
| 70 | // serialize Timers |
| 71 | jsonGenerator.writeArrayFieldStart("timers"); |
| 72 | |
| 73 | for (final MetricsObjectResource.TimerObjectResource timer : |
| 74 | metrics.getTimers()) { |
| 75 | final String timerJSON = mapper.writeValueAsString(timer.getTimer()); |
| 76 | serializeItem(jsonGenerator, "timer", timerJSON, timer); |
| 77 | } |
| 78 | jsonGenerator.writeEndArray(); |
| 79 | |
| 80 | // Serialize Gauges |
| 81 | jsonGenerator.writeArrayFieldStart("gauges"); |
| 82 | |
| 83 | for (final MetricsObjectResource.GaugeObjectResource gauge : |
| 84 | metrics.getGauges()) { |
| 85 | final String gaugeJSON = mapper.writeValueAsString(gauge.getGauge()); |
| 86 | serializeItem(jsonGenerator, "gauge", gaugeJSON, gauge); |
| 87 | } |
| 88 | jsonGenerator.writeEndArray(); |
| 89 | |
| 90 | // Serialize Counters |
| 91 | jsonGenerator.writeArrayFieldStart("counters"); |
| 92 | |
| 93 | for (final MetricsObjectResource.CounterObjectResource counter : |
| 94 | metrics.getCounters()) { |
| 95 | final String counterJSON = mapper.writeValueAsString(counter.getCounter()); |
| 96 | serializeItem(jsonGenerator, "counter", counterJSON, counter); |
| 97 | } |
| 98 | jsonGenerator.writeEndArray(); |
| 99 | |
| 100 | // Serialize Meters |
| 101 | jsonGenerator.writeArrayFieldStart("meters"); |
| 102 | |
| 103 | for (final MetricsObjectResource.MeterObjectResource meter : |
| 104 | metrics.getMeters()) { |
| 105 | final String meterJSON = mapper.writeValueAsString(meter.getMeter()); |
| 106 | serializeItem(jsonGenerator, "meter", meterJSON, meter); |
| 107 | } |
| 108 | jsonGenerator.writeEndArray(); |
| 109 | |
| 110 | // Serialize Histograms |
| 111 | jsonGenerator.writeArrayFieldStart("histograms"); |
| 112 | |
| 113 | for (final MetricsObjectResource.HistogramObjectResource histogram : |
| 114 | metrics.getHistograms()) { |
| 115 | final String histogramJSON = mapper.writeValueAsString(histogram.getHistogram()); |
| 116 | serializeItem(jsonGenerator, "histogram", histogramJSON, histogram); |
| 117 | } |
| 118 | jsonGenerator.writeEndArray(); |
| 119 | |
| 120 | jsonGenerator.writeEndObject(); |
| 121 | } |
| 122 | |
| 123 | } |