blob: 2f0b4d88ef3dd85b55f0bae86c98568aa067b168 [file] [log] [blame]
Jian Li72315152015-12-10 17:20:43 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.codec.impl;
17
18import com.codahale.metrics.Counter;
19import com.codahale.metrics.Gauge;
20import com.codahale.metrics.Meter;
21import com.codahale.metrics.Metric;
22import com.codahale.metrics.Histogram;
23import com.codahale.metrics.Timer;
24
25import com.fasterxml.jackson.databind.node.ObjectNode;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Codec for the Metric class.
33 */
34public class MetricCodec extends JsonCodec<Metric> {
35
36 // JSON field names
37 private static final String COUNTER = "counter";
38
39 private static final String GAUGE = "gauge";
40 private static final String VALUE = "value";
41
42 private static final String METER = "meter";
43 private static final String MEAN_RATE = "mean_rate";
44 private static final String ONE_MIN_RATE = "1_min_rate";
45 private static final String FIVE_MIN_RATE = "5_min_rate";
46 private static final String FIFT_MIN_RATE = "15_min_rate";
47
48 private static final String HISTOGRAM = "histogram";
49 private static final String MIN = "min";
50 private static final String MAX = "max";
51 private static final String MEAN = "mean";
52 private static final String STDDEV = "stddev";
53
54 private static final String TIMER = "timer";
55
56 @Override
57 public ObjectNode encode(Metric metric, CodecContext context) {
58 checkNotNull(metric, "Metric cannot be null");
59
60 ObjectNode objectNode = context.mapper().createObjectNode();
61 ObjectNode dataNode = context.mapper().createObjectNode();
62
63 if (metric instanceof Counter) {
64 dataNode.put(COUNTER, ((Counter) metric).getCount());
65 objectNode.set(COUNTER, dataNode);
66 } else if (metric instanceof Gauge) {
67 objectNode.put(VALUE, ((Gauge) metric).getValue().toString());
68 objectNode.set(GAUGE, dataNode);
69 } else if (metric instanceof Meter) {
70 dataNode.put(COUNTER, ((Meter) metric).getCount());
71 dataNode.put(MEAN_RATE, ((Meter) metric).getMeanRate());
72 dataNode.put(ONE_MIN_RATE, ((Meter) metric).getOneMinuteRate());
73 dataNode.put(FIVE_MIN_RATE, ((Meter) metric).getFiveMinuteRate());
74 dataNode.put(FIFT_MIN_RATE, ((Meter) metric).getFifteenMinuteRate());
75 objectNode.set(METER, dataNode);
76 } else if (metric instanceof Histogram) {
77 dataNode.put(COUNTER, ((Histogram) metric).getCount());
78 dataNode.put(MEAN, ((Histogram) metric).getSnapshot().getMean());
79 dataNode.put(MIN, ((Histogram) metric).getSnapshot().getMin());
80 dataNode.put(MAX, ((Histogram) metric).getSnapshot().getMax());
81 dataNode.put(STDDEV, ((Histogram) metric).getSnapshot().getStdDev());
82 objectNode.set(HISTOGRAM, dataNode);
83 } else if (metric instanceof Timer) {
84 dataNode.put(COUNTER, ((Timer) metric).getCount());
85 dataNode.put(MEAN_RATE, ((Timer) metric).getMeanRate());
86 dataNode.put(ONE_MIN_RATE, ((Timer) metric).getOneMinuteRate());
87 dataNode.put(FIVE_MIN_RATE, ((Timer) metric).getFiveMinuteRate());
88 dataNode.put(FIFT_MIN_RATE, ((Timer) metric).getFifteenMinuteRate());
89 dataNode.put(MEAN, nanoToMs(((Timer) metric).getSnapshot().getMean()));
90 dataNode.put(MIN, nanoToMs(((Timer) metric).getSnapshot().getMin()));
91 dataNode.put(MAX, nanoToMs(((Timer) metric).getSnapshot().getMax()));
92 dataNode.put(STDDEV, nanoToMs(((Timer) metric).getSnapshot().getStdDev()));
93 objectNode.set(TIMER, dataNode);
94 }
95 return objectNode;
96 }
97
98 private double nanoToMs(double nano) {
99 return nano / 1_000_000D;
100 }
101}