blob: 6cf8effbfde4a597b0b593cccebf3e83432a0172 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Pavlin Radoslavov41d5adb2014-10-23 13:44:21 -070019package org.onlab.metrics;
20
21import com.codahale.metrics.Gauge;
22import com.codahale.metrics.Meter;
23
24/**
25 * Metric measurements for events.
26 */
27public class EventMetric {
28 private static final String GAUGE_TIMESTAMP_NAME = "Timestamp.EpochMs";
29 private static final String METER_RATE_NAME = "Rate";
30
31 private final MetricsService metricsService;
32 private final String componentName;
33 private final String featureName;
34
35 private MetricsComponent metricsComponent;
36 private MetricsFeature metricsFeature;
37
38 private volatile long lastEventTimestampEpochMs = 0;
39 private Gauge<Long> lastEventTimestampGauge;
40 private Meter eventRateMeter;
41
42 /**
43 * Constructor.
44 *
45 * @param metricsService the Metrics Service to use for Metrics
46 * registration and deregistration
47 * @param componentName the Metrics Component Name to use for Metrics
48 * registration and deregistration
49 * @param featureName the Metrics Feature Name to use for Metrics
50 * registration and deregistration
51 */
52 public EventMetric(MetricsService metricsService, String componentName,
53 String featureName) {
54 this.metricsService = metricsService;
55 this.componentName = componentName;
56 this.featureName = featureName;
57 }
58
59 /**
60 * Registers the metrics.
61 */
62 public void registerMetrics() {
63 metricsComponent = metricsService.registerComponent(componentName);
64 metricsFeature = metricsComponent.registerFeature(featureName);
65
66 lastEventTimestampEpochMs = 0;
67 lastEventTimestampGauge =
68 metricsService.registerMetric(metricsComponent,
69 metricsFeature,
70 GAUGE_TIMESTAMP_NAME,
71 new Gauge<Long>() {
72 @Override
73 public Long getValue() {
74 return lastEventTimestampEpochMs;
75 }
76 });
77
78 eventRateMeter = metricsService.createMeter(metricsComponent,
79 metricsFeature,
80 METER_RATE_NAME);
81 }
82
83 /**
84 * Removes the metrics.
85 */
86 public void removeMetrics() {
87 lastEventTimestampEpochMs = 0;
88 metricsService.removeMetric(metricsComponent,
89 metricsFeature,
90 GAUGE_TIMESTAMP_NAME);
91 metricsService.removeMetric(metricsComponent,
92 metricsFeature,
93 METER_RATE_NAME);
94 }
95
96 /**
97 * Updates the metric measurements for a single event.
98 */
99 public void eventReceived() {
100 lastEventTimestampEpochMs = System.currentTimeMillis();
101 eventRateMeter.mark(1);
102 }
103
104 /**
105 * Gets the last event timestamp Gauge (ms from the Epoch).
106 *
107 * @return the last event timestamp Gauge (ms from the Epoch)
108 */
109 public Gauge<Long> lastEventTimestampGauge() {
110 return lastEventTimestampGauge;
111 }
112
113 /**
114 * Gets the event rate meter.
115 *
116 * @return the event rate meter
117 */
118 public Meter eventRateMeter() {
119 return eventRateMeter;
120 }
121}