Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Pavlin Radoslavov | 41d5adb | 2014-10-23 13:44:21 -0700 | [diff] [blame] | 16 | package org.onlab.metrics; |
| 17 | |
| 18 | import com.codahale.metrics.Gauge; |
| 19 | import com.codahale.metrics.Meter; |
| 20 | |
| 21 | /** |
| 22 | * Metric measurements for events. |
| 23 | */ |
| 24 | public class EventMetric { |
| 25 | private static final String GAUGE_TIMESTAMP_NAME = "Timestamp.EpochMs"; |
| 26 | private static final String METER_RATE_NAME = "Rate"; |
| 27 | |
| 28 | private final MetricsService metricsService; |
| 29 | private final String componentName; |
| 30 | private final String featureName; |
| 31 | |
| 32 | private MetricsComponent metricsComponent; |
| 33 | private MetricsFeature metricsFeature; |
| 34 | |
| 35 | private volatile long lastEventTimestampEpochMs = 0; |
| 36 | private Gauge<Long> lastEventTimestampGauge; |
| 37 | private Meter eventRateMeter; |
| 38 | |
| 39 | /** |
| 40 | * Constructor. |
| 41 | * |
| 42 | * @param metricsService the Metrics Service to use for Metrics |
| 43 | * registration and deregistration |
| 44 | * @param componentName the Metrics Component Name to use for Metrics |
| 45 | * registration and deregistration |
| 46 | * @param featureName the Metrics Feature Name to use for Metrics |
| 47 | * registration and deregistration |
| 48 | */ |
| 49 | public EventMetric(MetricsService metricsService, String componentName, |
| 50 | String featureName) { |
| 51 | this.metricsService = metricsService; |
| 52 | this.componentName = componentName; |
| 53 | this.featureName = featureName; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Registers the metrics. |
| 58 | */ |
| 59 | public void registerMetrics() { |
| 60 | metricsComponent = metricsService.registerComponent(componentName); |
| 61 | metricsFeature = metricsComponent.registerFeature(featureName); |
| 62 | |
| 63 | lastEventTimestampEpochMs = 0; |
| 64 | lastEventTimestampGauge = |
| 65 | metricsService.registerMetric(metricsComponent, |
| 66 | metricsFeature, |
| 67 | GAUGE_TIMESTAMP_NAME, |
| 68 | new Gauge<Long>() { |
| 69 | @Override |
| 70 | public Long getValue() { |
| 71 | return lastEventTimestampEpochMs; |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | eventRateMeter = metricsService.createMeter(metricsComponent, |
| 76 | metricsFeature, |
| 77 | METER_RATE_NAME); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Removes the metrics. |
| 82 | */ |
| 83 | public void removeMetrics() { |
| 84 | lastEventTimestampEpochMs = 0; |
| 85 | metricsService.removeMetric(metricsComponent, |
| 86 | metricsFeature, |
| 87 | GAUGE_TIMESTAMP_NAME); |
| 88 | metricsService.removeMetric(metricsComponent, |
| 89 | metricsFeature, |
| 90 | METER_RATE_NAME); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Updates the metric measurements for a single event. |
| 95 | */ |
| 96 | public void eventReceived() { |
| 97 | lastEventTimestampEpochMs = System.currentTimeMillis(); |
| 98 | eventRateMeter.mark(1); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Gets the last event timestamp Gauge (ms from the Epoch). |
| 103 | * |
| 104 | * @return the last event timestamp Gauge (ms from the Epoch) |
| 105 | */ |
| 106 | public Gauge<Long> lastEventTimestampGauge() { |
| 107 | return lastEventTimestampGauge; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Gets the event rate meter. |
| 112 | * |
| 113 | * @return the event rate meter |
| 114 | */ |
| 115 | public Meter eventRateMeter() { |
| 116 | return eventRateMeter; |
| 117 | } |
| 118 | } |