Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import com.codahale.metrics.Counter; |
| 19 | import com.codahale.metrics.Gauge; |
| 20 | import com.codahale.metrics.Meter; |
| 21 | import com.codahale.metrics.Metric; |
| 22 | import com.codahale.metrics.Histogram; |
| 23 | import com.codahale.metrics.Timer; |
| 24 | |
| 25 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 26 | import org.onosproject.codec.CodecContext; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | |
Jian Li | 64dd889 | 2015-12-30 14:13:18 -0800 | [diff] [blame] | 29 | import java.util.concurrent.TimeUnit; |
| 30 | |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | |
| 33 | /** |
| 34 | * Codec for the Metric class. |
| 35 | */ |
| 36 | public class MetricCodec extends JsonCodec<Metric> { |
| 37 | |
| 38 | // JSON field names |
| 39 | private static final String COUNTER = "counter"; |
| 40 | |
| 41 | private static final String GAUGE = "gauge"; |
| 42 | private static final String VALUE = "value"; |
| 43 | |
| 44 | private static final String METER = "meter"; |
| 45 | private static final String MEAN_RATE = "mean_rate"; |
| 46 | private static final String ONE_MIN_RATE = "1_min_rate"; |
| 47 | private static final String FIVE_MIN_RATE = "5_min_rate"; |
| 48 | private static final String FIFT_MIN_RATE = "15_min_rate"; |
| 49 | |
| 50 | private static final String HISTOGRAM = "histogram"; |
| 51 | private static final String MIN = "min"; |
| 52 | private static final String MAX = "max"; |
| 53 | private static final String MEAN = "mean"; |
| 54 | private static final String STDDEV = "stddev"; |
| 55 | |
| 56 | private static final String TIMER = "timer"; |
| 57 | |
| 58 | @Override |
| 59 | public ObjectNode encode(Metric metric, CodecContext context) { |
| 60 | checkNotNull(metric, "Metric cannot be null"); |
| 61 | |
| 62 | ObjectNode objectNode = context.mapper().createObjectNode(); |
| 63 | ObjectNode dataNode = context.mapper().createObjectNode(); |
| 64 | |
| 65 | if (metric instanceof Counter) { |
| 66 | dataNode.put(COUNTER, ((Counter) metric).getCount()); |
| 67 | objectNode.set(COUNTER, dataNode); |
| 68 | } else if (metric instanceof Gauge) { |
| 69 | objectNode.put(VALUE, ((Gauge) metric).getValue().toString()); |
| 70 | objectNode.set(GAUGE, dataNode); |
| 71 | } else if (metric instanceof Meter) { |
| 72 | dataNode.put(COUNTER, ((Meter) metric).getCount()); |
| 73 | dataNode.put(MEAN_RATE, ((Meter) metric).getMeanRate()); |
| 74 | dataNode.put(ONE_MIN_RATE, ((Meter) metric).getOneMinuteRate()); |
| 75 | dataNode.put(FIVE_MIN_RATE, ((Meter) metric).getFiveMinuteRate()); |
| 76 | dataNode.put(FIFT_MIN_RATE, ((Meter) metric).getFifteenMinuteRate()); |
| 77 | objectNode.set(METER, dataNode); |
| 78 | } else if (metric instanceof Histogram) { |
| 79 | dataNode.put(COUNTER, ((Histogram) metric).getCount()); |
| 80 | dataNode.put(MEAN, ((Histogram) metric).getSnapshot().getMean()); |
| 81 | dataNode.put(MIN, ((Histogram) metric).getSnapshot().getMin()); |
| 82 | dataNode.put(MAX, ((Histogram) metric).getSnapshot().getMax()); |
| 83 | dataNode.put(STDDEV, ((Histogram) metric).getSnapshot().getStdDev()); |
| 84 | objectNode.set(HISTOGRAM, dataNode); |
| 85 | } else if (metric instanceof Timer) { |
| 86 | dataNode.put(COUNTER, ((Timer) metric).getCount()); |
| 87 | dataNode.put(MEAN_RATE, ((Timer) metric).getMeanRate()); |
| 88 | dataNode.put(ONE_MIN_RATE, ((Timer) metric).getOneMinuteRate()); |
| 89 | dataNode.put(FIVE_MIN_RATE, ((Timer) metric).getFiveMinuteRate()); |
| 90 | dataNode.put(FIFT_MIN_RATE, ((Timer) metric).getFifteenMinuteRate()); |
| 91 | dataNode.put(MEAN, nanoToMs(((Timer) metric).getSnapshot().getMean())); |
| 92 | dataNode.put(MIN, nanoToMs(((Timer) metric).getSnapshot().getMin())); |
| 93 | dataNode.put(MAX, nanoToMs(((Timer) metric).getSnapshot().getMax())); |
| 94 | dataNode.put(STDDEV, nanoToMs(((Timer) metric).getSnapshot().getStdDev())); |
| 95 | objectNode.set(TIMER, dataNode); |
| 96 | } |
| 97 | return objectNode; |
| 98 | } |
| 99 | |
| 100 | private double nanoToMs(double nano) { |
Jian Li | 64dd889 | 2015-12-30 14:13:18 -0800 | [diff] [blame] | 101 | return TimeUnit.MILLISECONDS.convert((long) nano, TimeUnit.NANOSECONDS); |
Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame] | 102 | } |
| 103 | } |