Thomas Vachuska

Merge remote-tracking branch 'origin/master'

...@@ -5,8 +5,6 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -5,8 +5,6 @@ import static org.slf4j.LoggerFactory.getLogger;
5 import java.util.LinkedList; 5 import java.util.LinkedList;
6 import java.util.List; 6 import java.util.List;
7 7
8 -import com.codahale.metrics.Gauge;
9 -import com.codahale.metrics.Meter;
10 import com.google.common.collect.ImmutableList; 8 import com.google.common.collect.ImmutableList;
11 import org.apache.felix.scr.annotations.Activate; 9 import org.apache.felix.scr.annotations.Activate;
12 import org.apache.felix.scr.annotations.Component; 10 import org.apache.felix.scr.annotations.Component;
...@@ -14,8 +12,7 @@ import org.apache.felix.scr.annotations.Deactivate; ...@@ -14,8 +12,7 @@ import org.apache.felix.scr.annotations.Deactivate;
14 import org.apache.felix.scr.annotations.Reference; 12 import org.apache.felix.scr.annotations.Reference;
15 import org.apache.felix.scr.annotations.ReferenceCardinality; 13 import org.apache.felix.scr.annotations.ReferenceCardinality;
16 import org.apache.felix.scr.annotations.Service; 14 import org.apache.felix.scr.annotations.Service;
17 -import org.onlab.metrics.MetricsComponent; 15 +import org.onlab.metrics.EventMetric;
18 -import org.onlab.metrics.MetricsFeature;
19 import org.onlab.metrics.MetricsService; 16 import org.onlab.metrics.MetricsService;
20 import org.onlab.onos.net.intent.IntentEvent; 17 import org.onlab.onos.net.intent.IntentEvent;
21 import org.onlab.onos.net.intent.IntentListener; 18 import org.onlab.onos.net.intent.IntentListener;
...@@ -33,56 +30,32 @@ public class IntentMetrics implements IntentMetricsService, ...@@ -33,56 +30,32 @@ public class IntentMetrics implements IntentMetricsService,
33 30
34 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 31 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
35 protected IntentService intentService; 32 protected IntentService intentService;
33 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
34 + protected MetricsService metricsService;
35 +
36 private LinkedList<IntentEvent> lastEvents = new LinkedList<>(); 36 private LinkedList<IntentEvent> lastEvents = new LinkedList<>();
37 private static final int LAST_EVENTS_MAX_N = 100; 37 private static final int LAST_EVENTS_MAX_N = 100;
38 38
39 // 39 //
40 // Metrics 40 // Metrics
41 // 41 //
42 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
43 - protected MetricsService metricsService;
44 - //
45 private static final String COMPONENT_NAME = "Intent"; 42 private static final String COMPONENT_NAME = "Intent";
46 private static final String FEATURE_SUBMITTED_NAME = "Submitted"; 43 private static final String FEATURE_SUBMITTED_NAME = "Submitted";
47 private static final String FEATURE_INSTALLED_NAME = "Installed"; 44 private static final String FEATURE_INSTALLED_NAME = "Installed";
48 private static final String FEATURE_WITHDRAW_REQUESTED_NAME = 45 private static final String FEATURE_WITHDRAW_REQUESTED_NAME =
49 "WithdrawRequested"; 46 "WithdrawRequested";
50 private static final String FEATURE_WITHDRAWN_NAME = "Withdrawn"; 47 private static final String FEATURE_WITHDRAWN_NAME = "Withdrawn";
51 - private static final String GAUGE_TIMESTAMP_NAME = "Timestamp.EpochMs";
52 - private static final String METER_RATE_NAME = "Rate";
53 // 48 //
54 - private MetricsComponent metricsComponent; 49 + // Event metrics:
55 - private MetricsFeature metricsFeatureSubmitted; 50 + // - Intent Submitted API operation
56 - private MetricsFeature metricsFeatureInstalled; 51 + // - Intent Installed operation completion
57 - private MetricsFeature metricsFeatureWithdrawRequested; 52 + // - Intent Withdraw Requested API operation
58 - private MetricsFeature metricsFeatureWithdrawn; 53 + // - Intent Withdrawn operation completion
59 // 54 //
60 - // Timestamps: 55 + private EventMetric intentSubmittedEventMetric;
61 - // - Intent Submitted API operation (ms from the Epoch) 56 + private EventMetric intentInstalledEventMetric;
62 - // - Intent Installed operation completion (ms from the Epoch) 57 + private EventMetric intentWithdrawRequestedEventMetric;
63 - // - Intent Withdraw Requested API operation (ms from the Epoch) 58 + private EventMetric intentWithdrawnEventMetric;
64 - // - Intent Withdrawn operation completion (ms from the Epoch)
65 - //
66 - private volatile long intentSubmittedTimestampEpochMs = 0;
67 - private volatile long intentInstalledTimestampEpochMs = 0;
68 - private volatile long intentWithdrawRequestedTimestampEpochMs = 0;
69 - private volatile long intentWithdrawnTimestampEpochMs = 0;
70 - //
71 - private Gauge<Long> intentSubmittedTimestampEpochMsGauge;
72 - private Gauge<Long> intentInstalledTimestampEpochMsGauge;
73 - private Gauge<Long> intentWithdrawRequestedTimestampEpochMsGauge;
74 - private Gauge<Long> intentWithdrawnTimestampEpochMsGauge;
75 - //
76 - // Rate meters:
77 - // - Rate of the Submitted Intent API operations
78 - // - Rate of the Installed Intent operations
79 - // - Rate of the Withdrawn Requested Intent API operations
80 - // - Rate of the Withdrawn Intent operations
81 - //
82 - private Meter intentSubmittedRateMeter;
83 - private Meter intentInstalledRateMeter;
84 - private Meter intentWithdrawRequestedRateMeter;
85 - private Meter intentWithdrawnRateMeter;
86 59
87 @Activate 60 @Activate
88 protected void activate() { 61 protected void activate() {
...@@ -108,43 +81,23 @@ public class IntentMetrics implements IntentMetricsService, ...@@ -108,43 +81,23 @@ public class IntentMetrics implements IntentMetricsService,
108 } 81 }
109 82
110 @Override 83 @Override
111 - public Gauge<Long> intentSubmittedTimestampEpochMsGauge() { 84 + public EventMetric intentSubmittedEventMetric() {
112 - return intentSubmittedTimestampEpochMsGauge; 85 + return intentSubmittedEventMetric;
113 - }
114 -
115 - @Override
116 - public Gauge<Long> intentInstalledTimestampEpochMsGauge() {
117 - return intentInstalledTimestampEpochMsGauge;
118 - }
119 -
120 - @Override
121 - public Gauge<Long> intentWithdrawRequestedTimestampEpochMsGauge() {
122 - return intentWithdrawRequestedTimestampEpochMsGauge;
123 - }
124 -
125 - @Override
126 - public Gauge<Long> intentWithdrawnTimestampEpochMsGauge() {
127 - return intentWithdrawnTimestampEpochMsGauge;
128 - }
129 -
130 - @Override
131 - public Meter intentSubmittedRateMeter() {
132 - return intentSubmittedRateMeter;
133 } 86 }
134 87
135 @Override 88 @Override
136 - public Meter intentInstalledRateMeter() { 89 + public EventMetric intentInstalledEventMetric() {
137 - return intentInstalledRateMeter; 90 + return intentInstalledEventMetric;
138 } 91 }
139 92
140 @Override 93 @Override
141 - public Meter intentWithdrawRequestedRateMeter() { 94 + public EventMetric intentWithdrawRequestedEventMetric() {
142 - return intentWithdrawRequestedRateMeter; 95 + return intentWithdrawRequestedEventMetric;
143 } 96 }
144 97
145 @Override 98 @Override
146 - public Meter intentWithdrawnRateMeter() { 99 + public EventMetric intentWithdrawnEventMetric() {
147 - return intentWithdrawnRateMeter; 100 + return intentWithdrawnEventMetric;
148 } 101 }
149 102
150 @Override 103 @Override
...@@ -156,26 +109,21 @@ public class IntentMetrics implements IntentMetricsService, ...@@ -156,26 +109,21 @@ public class IntentMetrics implements IntentMetricsService,
156 // 109 //
157 switch (event.type()) { 110 switch (event.type()) {
158 case SUBMITTED: 111 case SUBMITTED:
159 - intentSubmittedTimestampEpochMs = System.currentTimeMillis(); 112 + intentSubmittedEventMetric.eventReceived();
160 - intentSubmittedRateMeter.mark(1);
161 break; 113 break;
162 case INSTALLED: 114 case INSTALLED:
163 - intentInstalledTimestampEpochMs = System.currentTimeMillis(); 115 + intentInstalledEventMetric.eventReceived();
164 - intentInstalledRateMeter.mark(1);
165 break; 116 break;
166 case FAILED: 117 case FAILED:
167 // TODO: Just ignore? 118 // TODO: Just ignore?
168 break; 119 break;
169 /* 120 /*
170 case WITHDRAW_REQUESTED: 121 case WITHDRAW_REQUESTED:
171 - intentWithdrawRequestedTimestampEpochMs = 122 + intentWithdrawRequestedEventMetric.eventReceived();
172 - System.currentTimeMillis();
173 - intentWithdrawRequestedRateMeter.mark(1);
174 break; 123 break;
175 */ 124 */
176 case WITHDRAWN: 125 case WITHDRAWN:
177 - intentWithdrawnTimestampEpochMs = System.currentTimeMillis(); 126 + intentWithdrawnEventMetric.eventReceived();
178 - intentWithdrawnRateMeter.mark(1);
179 break; 127 break;
180 default: 128 default:
181 break; 129 break;
...@@ -199,10 +147,6 @@ public class IntentMetrics implements IntentMetricsService, ...@@ -199,10 +147,6 @@ public class IntentMetrics implements IntentMetricsService,
199 */ 147 */
200 private void clear() { 148 private void clear() {
201 synchronized (lastEvents) { 149 synchronized (lastEvents) {
202 - intentSubmittedTimestampEpochMs = 0;
203 - intentInstalledTimestampEpochMs = 0;
204 - intentWithdrawRequestedTimestampEpochMs = 0;
205 - intentWithdrawnTimestampEpochMs = 0;
206 lastEvents.clear(); 150 lastEvents.clear();
207 } 151 }
208 } 152 }
...@@ -211,109 +155,32 @@ public class IntentMetrics implements IntentMetricsService, ...@@ -211,109 +155,32 @@ public class IntentMetrics implements IntentMetricsService,
211 * Registers the metrics. 155 * Registers the metrics.
212 */ 156 */
213 private void registerMetrics() { 157 private void registerMetrics() {
214 - metricsComponent = metricsService.registerComponent(COMPONENT_NAME); 158 + intentSubmittedEventMetric =
215 - // 159 + new EventMetric(metricsService, COMPONENT_NAME,
216 - metricsFeatureSubmitted = 160 + FEATURE_SUBMITTED_NAME);
217 - metricsComponent.registerFeature(FEATURE_SUBMITTED_NAME); 161 + intentInstalledEventMetric =
218 - metricsFeatureInstalled = 162 + new EventMetric(metricsService, COMPONENT_NAME,
219 - metricsComponent.registerFeature(FEATURE_INSTALLED_NAME); 163 + FEATURE_INSTALLED_NAME);
220 - metricsFeatureWithdrawRequested = 164 + intentWithdrawRequestedEventMetric =
221 - metricsComponent.registerFeature(FEATURE_WITHDRAW_REQUESTED_NAME); 165 + new EventMetric(metricsService, COMPONENT_NAME,
222 - metricsFeatureWithdrawn = 166 + FEATURE_WITHDRAW_REQUESTED_NAME);
223 - metricsComponent.registerFeature(FEATURE_WITHDRAWN_NAME); 167 + intentWithdrawnEventMetric =
224 - // 168 + new EventMetric(metricsService, COMPONENT_NAME,
225 - intentSubmittedTimestampEpochMsGauge = 169 + FEATURE_WITHDRAWN_NAME);
226 - metricsService.registerMetric(metricsComponent, 170 +
227 - metricsFeatureSubmitted, 171 + intentSubmittedEventMetric.registerMetrics();
228 - GAUGE_TIMESTAMP_NAME, 172 + intentInstalledEventMetric.registerMetrics();
229 - new Gauge<Long>() { 173 + intentWithdrawRequestedEventMetric.registerMetrics();
230 - @Override 174 + intentWithdrawnEventMetric.registerMetrics();
231 - public Long getValue() {
232 - return intentSubmittedTimestampEpochMs;
233 - }
234 - });
235 - //
236 - intentInstalledTimestampEpochMsGauge =
237 - metricsService.registerMetric(metricsComponent,
238 - metricsFeatureInstalled,
239 - GAUGE_TIMESTAMP_NAME,
240 - new Gauge<Long>() {
241 - @Override
242 - public Long getValue() {
243 - return intentInstalledTimestampEpochMs;
244 - }
245 - });
246 - //
247 - intentWithdrawRequestedTimestampEpochMsGauge =
248 - metricsService.registerMetric(metricsComponent,
249 - metricsFeatureWithdrawRequested,
250 - GAUGE_TIMESTAMP_NAME,
251 - new Gauge<Long>() {
252 - @Override
253 - public Long getValue() {
254 - return intentWithdrawRequestedTimestampEpochMs;
255 - }
256 - });
257 - //
258 - intentWithdrawnTimestampEpochMsGauge =
259 - metricsService.registerMetric(metricsComponent,
260 - metricsFeatureWithdrawn,
261 - GAUGE_TIMESTAMP_NAME,
262 - new Gauge<Long>() {
263 - @Override
264 - public Long getValue() {
265 - return intentWithdrawnTimestampEpochMs;
266 - }
267 - });
268 - //
269 - intentSubmittedRateMeter =
270 - metricsService.createMeter(metricsComponent,
271 - metricsFeatureSubmitted,
272 - METER_RATE_NAME);
273 - //
274 - intentInstalledRateMeter =
275 - metricsService.createMeter(metricsComponent,
276 - metricsFeatureInstalled,
277 - METER_RATE_NAME);
278 - //
279 - intentWithdrawRequestedRateMeter =
280 - metricsService.createMeter(metricsComponent,
281 - metricsFeatureWithdrawRequested,
282 - METER_RATE_NAME);
283 - //
284 - intentWithdrawnRateMeter =
285 - metricsService.createMeter(metricsComponent,
286 - metricsFeatureWithdrawn,
287 - METER_RATE_NAME);
288 } 175 }
289 176
290 /** 177 /**
291 * Removes the metrics. 178 * Removes the metrics.
292 */ 179 */
293 private void removeMetrics() { 180 private void removeMetrics() {
294 - metricsService.removeMetric(metricsComponent, 181 + intentSubmittedEventMetric.removeMetrics();
295 - metricsFeatureSubmitted, 182 + intentInstalledEventMetric.removeMetrics();
296 - GAUGE_TIMESTAMP_NAME); 183 + intentWithdrawRequestedEventMetric.removeMetrics();
297 - metricsService.removeMetric(metricsComponent, 184 + intentWithdrawnEventMetric.removeMetrics();
298 - metricsFeatureInstalled,
299 - GAUGE_TIMESTAMP_NAME);
300 - metricsService.removeMetric(metricsComponent,
301 - metricsFeatureWithdrawRequested,
302 - GAUGE_TIMESTAMP_NAME);
303 - metricsService.removeMetric(metricsComponent,
304 - metricsFeatureWithdrawn,
305 - GAUGE_TIMESTAMP_NAME);
306 - metricsService.removeMetric(metricsComponent,
307 - metricsFeatureSubmitted,
308 - METER_RATE_NAME);
309 - metricsService.removeMetric(metricsComponent,
310 - metricsFeatureInstalled,
311 - METER_RATE_NAME);
312 - metricsService.removeMetric(metricsComponent,
313 - metricsFeatureWithdrawRequested,
314 - METER_RATE_NAME);
315 - metricsService.removeMetric(metricsComponent,
316 - metricsFeatureWithdrawn,
317 - METER_RATE_NAME);
318 } 185 }
319 } 186 }
......
1 package org.onlab.onos.metrics.intent; 1 package org.onlab.onos.metrics.intent;
2 2
3 import java.util.List; 3 import java.util.List;
4 - 4 +import org.onlab.metrics.EventMetric;
5 -import com.codahale.metrics.Gauge;
6 -import com.codahale.metrics.Meter;
7 import org.onlab.onos.net.intent.IntentEvent; 5 import org.onlab.onos.net.intent.IntentEvent;
8 6
9 /** 7 /**
...@@ -18,68 +16,32 @@ public interface IntentMetricsService { ...@@ -18,68 +16,32 @@ public interface IntentMetricsService {
18 public List<IntentEvent> getEvents(); 16 public List<IntentEvent> getEvents();
19 17
20 /** 18 /**
21 - * Gets the Metrics' Gauge for the intent SUBMITTED event timestamp 19 + * Gets the Event Metric for the intent SUBMITTED events.
22 - * (ms from the epoch).
23 * 20 *
24 - * @return the Metrics' Gauge for the intent SUBMITTED event timestamp 21 + * @return the Event Metric for the intent SUBMITTED events.
25 - * (ms from the epoch)
26 */ 22 */
27 - public Gauge<Long> intentSubmittedTimestampEpochMsGauge(); 23 + public EventMetric intentSubmittedEventMetric();
28 24
29 /** 25 /**
30 - * Gets the Metrics' Gauge for the intent INSTALLED event timestamp 26 + * Gets the Event Metric for the intent INSTALLED events.
31 - * (ms from the epoch).
32 * 27 *
33 - * @return the Metrics' Gauge for the intent INSTALLED event timestamp 28 + * @return the Event Metric for the intent INSTALLED events.
34 - * (ms from the epoch)
35 */ 29 */
36 - public Gauge<Long> intentInstalledTimestampEpochMsGauge(); 30 + public EventMetric intentInstalledEventMetric();
37 31
38 /** 32 /**
39 - * Gets the Metrics' Gauge for the intent WITHDRAW_REQUESTED event 33 + * Gets the Event Metric for the intent WITHDRAW_REQUESTED events.
40 - * timestamp (ms from the epoch).
41 * 34 *
42 * TODO: This intent event is not implemented yet. 35 * TODO: This intent event is not implemented yet.
43 * 36 *
44 - * @return the Metrics' Gauge for the intent WITHDRAW_REQUESTED event 37 + * @return the Event Metric for the intent WITHDRAW_REQUESTED events.
45 - * timestamp (ms from the epoch)
46 - */
47 - public Gauge<Long> intentWithdrawRequestedTimestampEpochMsGauge();
48 -
49 - /**
50 - * Gets the Metrics' Gauge for the intent WITHDRAWN event timestamp
51 - * (ms from the epoch).
52 - *
53 - * @return the Metrics' Gauge for the intent WITHDRAWN event timestamp
54 - * (ms from the epoch)
55 - */
56 - public Gauge<Long> intentWithdrawnTimestampEpochMsGauge();
57 -
58 - /**
59 - * Gets the Metrics' Meter for the submitted intents event rate.
60 - *
61 - * @return the Metrics' Meter for the submitted intents event rate
62 - */
63 - public Meter intentSubmittedRateMeter();
64 -
65 - /**
66 - * Gets the Metrics' Meter for the installed intents event rate.
67 - *
68 - * @return the Metrics' Meter for the installed intent event rate
69 - */
70 - public Meter intentInstalledRateMeter();
71 -
72 - /**
73 - * Gets the Metrics' Meter for the withdraw requested intents event rate.
74 - *
75 - * @return the Metrics' Meter for the withdraw requested intents event rate
76 */ 38 */
77 - public Meter intentWithdrawRequestedRateMeter(); 39 + public EventMetric intentWithdrawRequestedEventMetric();
78 40
79 /** 41 /**
80 - * Gets the Metrics' Meter for the withdraw completed intents event rate. 42 + * Gets the Event Metric for the intent WITHDRAWN events.
81 * 43 *
82 - * @return the Metrics' Meter for the withdraw completed intents event rate 44 + * @return the Event Metric for the intent WITHDRAWN events.
83 */ 45 */
84 - public Meter intentWithdrawnRateMeter(); 46 + public EventMetric intentWithdrawnEventMetric();
85 } 47 }
......
...@@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.JsonNode; ...@@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.JsonNode;
11 import com.fasterxml.jackson.databind.ObjectMapper; 11 import com.fasterxml.jackson.databind.ObjectMapper;
12 import com.fasterxml.jackson.databind.node.ObjectNode; 12 import com.fasterxml.jackson.databind.node.ObjectNode;
13 import org.apache.karaf.shell.commands.Command; 13 import org.apache.karaf.shell.commands.Command;
14 +import org.onlab.metrics.EventMetric;
14 import org.onlab.onos.cli.AbstractShellCommand; 15 import org.onlab.onos.cli.AbstractShellCommand;
15 import org.onlab.onos.metrics.intent.IntentMetricsService; 16 import org.onlab.onos.metrics.intent.IntentMetricsService;
16 17
...@@ -29,8 +30,6 @@ public class IntentEventsMetricsCommand extends AbstractShellCommand { ...@@ -29,8 +30,6 @@ public class IntentEventsMetricsCommand extends AbstractShellCommand {
29 @Override 30 @Override
30 protected void execute() { 31 protected void execute() {
31 IntentMetricsService service = get(IntentMetricsService.class); 32 IntentMetricsService service = get(IntentMetricsService.class);
32 - Gauge<Long> gauge;
33 - Meter meter;
34 33
35 if (outputJson()) { 34 if (outputJson()) {
36 ObjectMapper mapper = new ObjectMapper() 35 ObjectMapper mapper = new ObjectMapper()
...@@ -38,49 +37,49 @@ public class IntentEventsMetricsCommand extends AbstractShellCommand { ...@@ -38,49 +37,49 @@ public class IntentEventsMetricsCommand extends AbstractShellCommand {
38 TimeUnit.MILLISECONDS, 37 TimeUnit.MILLISECONDS,
39 false)); 38 false));
40 ObjectNode result = mapper.createObjectNode(); 39 ObjectNode result = mapper.createObjectNode();
41 - // 40 + result = json(mapper, result, "intentSubmitted",
42 - gauge = service.intentSubmittedTimestampEpochMsGauge(); 41 + service.intentSubmittedEventMetric());
43 - result.put("intentSubmittedTimestamp", json(mapper, gauge)); 42 + result = json(mapper, result, "intentInstalled",
44 - gauge = service.intentInstalledTimestampEpochMsGauge(); 43 + service.intentInstalledEventMetric());
45 - result.put("intentInstalledTimestamp", json(mapper, gauge)); 44 + result = json(mapper, result, "intentWithdrawRequested",
46 - gauge = service.intentWithdrawRequestedTimestampEpochMsGauge(); 45 + service.intentWithdrawRequestedEventMetric());
47 - result.put("intentWithdrawRequestedTimestamp", 46 + result = json(mapper, result, "intentWithdrawn",
48 - json(mapper, gauge)); 47 + service.intentWithdrawnEventMetric());
49 - gauge = service.intentWithdrawnTimestampEpochMsGauge();
50 - result.put("intentWithdrawnTimestamp", json(mapper, gauge));
51 - //
52 - meter = service.intentSubmittedRateMeter();
53 - result.put("intentSubmittedRate", json(mapper, meter));
54 - meter = service.intentInstalledRateMeter();
55 - result.put("intentInstalledRate", json(mapper, meter));
56 - meter = service.intentWithdrawRequestedRateMeter();
57 - result.put("intentWithdrawRequestedRate", json(mapper, meter));
58 - meter = service.intentWithdrawnRateMeter();
59 - result.put("intentWithdrawnRate", json(mapper, meter));
60 - //
61 print("%s", result); 48 print("%s", result);
62 } else { 49 } else {
63 - gauge = service.intentSubmittedTimestampEpochMsGauge(); 50 + printEventMetric("Submitted",
64 - printGauge("Submitted", gauge); 51 + service.intentSubmittedEventMetric());
65 - gauge = service.intentInstalledTimestampEpochMsGauge(); 52 + printEventMetric("Installed",
66 - printGauge("Installed", gauge); 53 + service.intentInstalledEventMetric());
67 - gauge = service.intentWithdrawRequestedTimestampEpochMsGauge(); 54 + printEventMetric("Withdraw Requested",
68 - printGauge("Withdraw Requested", gauge); 55 + service.intentWithdrawRequestedEventMetric());
69 - gauge = service.intentWithdrawnTimestampEpochMsGauge(); 56 + printEventMetric("Withdrawn",
70 - printGauge("Withdrawn", gauge); 57 + service.intentWithdrawnEventMetric());
71 - //
72 - meter = service.intentSubmittedRateMeter();
73 - printMeter("Submitted", meter);
74 - meter = service.intentInstalledRateMeter();
75 - printMeter("Installed", meter);
76 - meter = service.intentWithdrawRequestedRateMeter();
77 - printMeter("Withdraw Requested", meter);
78 - meter = service.intentWithdrawnRateMeter();
79 - printMeter("Withdrawn", meter);
80 } 58 }
81 } 59 }
82 60
83 /** 61 /**
62 + * Produces JSON node for an Event Metric.
63 + *
64 + * @param mapper the JSON object mapper to use
65 + * @param objectNode the JSON object node to use
66 + * @param propertyPrefix the property prefix to use
67 + * @param eventMetric the Event Metric with the data
68 + * @return JSON object node for the Event Metric
69 + */
70 + private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
71 + String propertyPrefix, EventMetric eventMetric) {
72 + String gaugeName = propertyPrefix + "Timestamp";
73 + String meterName = propertyPrefix + "Rate";
74 + Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
75 + Meter meter = eventMetric.eventRateMeter();
76 +
77 + objectNode.put(gaugeName, json(mapper, gauge));
78 + objectNode.put(meterName, json(mapper, meter));
79 + return objectNode;
80 + }
81 +
82 + /**
84 * Produces JSON node for an Object. 83 * Produces JSON node for an Object.
85 * 84 *
86 * @param mapper the JSON object mapper to use 85 * @param mapper the JSON object mapper to use
...@@ -94,8 +93,8 @@ public class IntentEventsMetricsCommand extends AbstractShellCommand { ...@@ -94,8 +93,8 @@ public class IntentEventsMetricsCommand extends AbstractShellCommand {
94 // 93 //
95 try { 94 try {
96 final String objectJson = mapper.writeValueAsString(object); 95 final String objectJson = mapper.writeValueAsString(object);
97 - JsonNode objectNode = mapper.readTree(objectJson); 96 + JsonNode jsonNode = mapper.readTree(objectJson);
98 - return objectNode; 97 + return jsonNode;
99 } catch (JsonProcessingException e) { 98 } catch (JsonProcessingException e) {
100 log.error("Error writing value as JSON string", e); 99 log.error("Error writing value as JSON string", e);
101 } catch (IOException e) { 100 } catch (IOException e) {
...@@ -105,28 +104,26 @@ public class IntentEventsMetricsCommand extends AbstractShellCommand { ...@@ -105,28 +104,26 @@ public class IntentEventsMetricsCommand extends AbstractShellCommand {
105 } 104 }
106 105
107 /** 106 /**
108 - * Prints a Gauge. 107 + * Prints an Event Metric.
109 * 108 *
110 * @param operationStr the string with the intent operation to print 109 * @param operationStr the string with the intent operation to print
111 - * @param gauge the Gauge to print 110 + * @param eventMetric the Event Metric to print
112 */ 111 */
113 - private void printGauge(String operationStr, Gauge<Long> gauge) { 112 + private void printEventMetric(String operationStr,
113 + EventMetric eventMetric) {
114 + Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
115 + Meter meter = eventMetric.eventRateMeter();
116 + TimeUnit rateUnit = TimeUnit.SECONDS;
117 + double rateFactor = rateUnit.toSeconds(1);
118 +
119 + // Print the Gauge
114 print(FORMAT_GAUGE, operationStr, gauge.getValue()); 120 print(FORMAT_GAUGE, operationStr, gauge.getValue());
115 - }
116 121
117 - /** 122 + // Print the Meter
118 - * Prints a Meter. 123 + print(FORMAT_METER, operationStr, meter.getCount(),
119 - * 124 + meter.getMeanRate() * rateFactor,
120 - * @param operationStr the string with the intent operation to print 125 + meter.getOneMinuteRate() * rateFactor,
121 - * @param meter the Meter to print 126 + meter.getFiveMinuteRate() * rateFactor,
122 - */ 127 + meter.getFifteenMinuteRate() * rateFactor);
123 - private void printMeter(String operationStr, Meter meter) {
124 - TimeUnit rateUnit = TimeUnit.SECONDS;
125 - double rateFactor = rateUnit.toSeconds(1);
126 - print(FORMAT_METER, operationStr, meter.getCount(),
127 - meter.getMeanRate() * rateFactor,
128 - meter.getOneMinuteRate() * rateFactor,
129 - meter.getFiveMinuteRate() * rateFactor,
130 - meter.getFifteenMinuteRate() * rateFactor);
131 } 128 }
132 } 129 }
......
...@@ -5,8 +5,6 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -5,8 +5,6 @@ import static org.slf4j.LoggerFactory.getLogger;
5 import java.util.LinkedList; 5 import java.util.LinkedList;
6 import java.util.List; 6 import java.util.List;
7 7
8 -import com.codahale.metrics.Gauge;
9 -import com.codahale.metrics.Meter;
10 import com.google.common.collect.ImmutableList; 8 import com.google.common.collect.ImmutableList;
11 import org.apache.felix.scr.annotations.Activate; 9 import org.apache.felix.scr.annotations.Activate;
12 import org.apache.felix.scr.annotations.Component; 10 import org.apache.felix.scr.annotations.Component;
...@@ -14,8 +12,7 @@ import org.apache.felix.scr.annotations.Deactivate; ...@@ -14,8 +12,7 @@ import org.apache.felix.scr.annotations.Deactivate;
14 import org.apache.felix.scr.annotations.Reference; 12 import org.apache.felix.scr.annotations.Reference;
15 import org.apache.felix.scr.annotations.ReferenceCardinality; 13 import org.apache.felix.scr.annotations.ReferenceCardinality;
16 import org.apache.felix.scr.annotations.Service; 14 import org.apache.felix.scr.annotations.Service;
17 -import org.onlab.metrics.MetricsComponent; 15 +import org.onlab.metrics.EventMetric;
18 -import org.onlab.metrics.MetricsFeature;
19 import org.onlab.metrics.MetricsService; 16 import org.onlab.metrics.MetricsService;
20 import org.onlab.onos.event.Event; 17 import org.onlab.onos.event.Event;
21 import org.onlab.onos.net.device.DeviceEvent; 18 import org.onlab.onos.net.device.DeviceEvent;
...@@ -48,6 +45,8 @@ public class TopologyMetrics implements TopologyMetricsService { ...@@ -48,6 +45,8 @@ public class TopologyMetrics implements TopologyMetricsService {
48 protected LinkService linkService; 45 protected LinkService linkService;
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected TopologyService topologyService; 47 protected TopologyService topologyService;
48 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 + protected MetricsService metricsService;
51 50
52 private LinkedList<Event> lastEvents = new LinkedList<>(); 51 private LinkedList<Event> lastEvents = new LinkedList<>();
53 private static final int LAST_EVENTS_MAX_N = 100; 52 private static final int LAST_EVENTS_MAX_N = 100;
...@@ -61,22 +60,22 @@ public class TopologyMetrics implements TopologyMetricsService { ...@@ -61,22 +60,22 @@ public class TopologyMetrics implements TopologyMetricsService {
61 // 60 //
62 // Metrics 61 // Metrics
63 // 62 //
64 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 - protected MetricsService metricsService;
66 - //
67 private static final String COMPONENT_NAME = "Topology"; 63 private static final String COMPONENT_NAME = "Topology";
68 - private static final String FEATURE_NAME = "EventNotification"; 64 + private static final String FEATURE_DEVICE_NAME = "DeviceEvent";
69 - private static final String GAUGE_NAME = "LastEventTimestamp.EpochMs"; 65 + private static final String FEATURE_HOST_NAME = "HostEvent";
70 - private static final String METER_NAME = "EventRate"; 66 + private static final String FEATURE_LINK_NAME = "LinkEvent";
67 + private static final String FEATURE_GRAPH_NAME = "GraphEvent";
71 // 68 //
72 - private MetricsComponent metricsComponent; 69 + // Event metrics:
73 - private MetricsFeature metricsFeatureEventNotification; 70 + // - Device events
71 + // - Host events
72 + // - Link events
73 + // - Topology Graph events
74 // 74 //
75 - // Timestamp of the last Topology event (ms from the Epoch) 75 + private EventMetric topologyDeviceEventMetric;
76 - private volatile long lastEventTimestampEpochMs = 0; 76 + private EventMetric topologyHostEventMetric;
77 - private Gauge<Long> lastEventTimestampEpochMsGauge; 77 + private EventMetric topologyLinkEventMetric;
78 - // Rate of the Topology events published to the Topology listeners 78 + private EventMetric topologyGraphEventMetric;
79 - private Meter eventRateMeter;
80 79
81 @Activate 80 @Activate
82 protected void activate() { 81 protected void activate() {
...@@ -113,27 +112,34 @@ public class TopologyMetrics implements TopologyMetricsService { ...@@ -113,27 +112,34 @@ public class TopologyMetrics implements TopologyMetricsService {
113 } 112 }
114 113
115 @Override 114 @Override
116 - public Gauge<Long> lastEventTimestampEpochMsGauge() { 115 + public EventMetric topologyDeviceEventMetric() {
117 - return lastEventTimestampEpochMsGauge; 116 + return topologyDeviceEventMetric;
117 + }
118 +
119 + @Override
120 + public EventMetric topologyHostEventMetric() {
121 + return topologyHostEventMetric;
118 } 122 }
119 123
120 @Override 124 @Override
121 - public Meter eventRateMeter() { 125 + public EventMetric topologyLinkEventMetric() {
122 - return eventRateMeter; 126 + return topologyLinkEventMetric;
127 + }
128 +
129 + @Override
130 + public EventMetric topologyGraphEventMetric() {
131 + return topologyGraphEventMetric;
123 } 132 }
124 133
125 /** 134 /**
126 * Records an event. 135 * Records an event.
127 * 136 *
128 * @param event the event to record 137 * @param event the event to record
129 - * @param updateEventRateMeter if true, update the Event Rate Meter 138 + * @param eventMetric the Event Metric to use
130 */ 139 */
131 - private void recordEvent(Event event, boolean updateEventRateMeter) { 140 + private void recordEvent(Event event, EventMetric eventMetric) {
132 synchronized (lastEvents) { 141 synchronized (lastEvents) {
133 - lastEventTimestampEpochMs = System.currentTimeMillis(); 142 + eventMetric.eventReceived();
134 - if (updateEventRateMeter) {
135 - eventRateMeter.mark(1);
136 - }
137 143
138 // 144 //
139 // Keep only the last N events, where N = LAST_EVENTS_MAX_N 145 // Keep only the last N events, where N = LAST_EVENTS_MAX_N
...@@ -151,7 +157,7 @@ public class TopologyMetrics implements TopologyMetricsService { ...@@ -151,7 +157,7 @@ public class TopologyMetrics implements TopologyMetricsService {
151 private class InnerDeviceListener implements DeviceListener { 157 private class InnerDeviceListener implements DeviceListener {
152 @Override 158 @Override
153 public void event(DeviceEvent event) { 159 public void event(DeviceEvent event) {
154 - recordEvent(event, true); 160 + recordEvent(event, topologyDeviceEventMetric);
155 log.debug("Device Event: time = {} type = {} event = {}", 161 log.debug("Device Event: time = {} type = {} event = {}",
156 event.time(), event.type(), event); 162 event.time(), event.type(), event);
157 } 163 }
...@@ -163,7 +169,7 @@ public class TopologyMetrics implements TopologyMetricsService { ...@@ -163,7 +169,7 @@ public class TopologyMetrics implements TopologyMetricsService {
163 private class InnerHostListener implements HostListener { 169 private class InnerHostListener implements HostListener {
164 @Override 170 @Override
165 public void event(HostEvent event) { 171 public void event(HostEvent event) {
166 - recordEvent(event, true); 172 + recordEvent(event, topologyHostEventMetric);
167 log.debug("Host Event: time = {} type = {} event = {}", 173 log.debug("Host Event: time = {} type = {} event = {}",
168 event.time(), event.type(), event); 174 event.time(), event.type(), event);
169 } 175 }
...@@ -175,7 +181,7 @@ public class TopologyMetrics implements TopologyMetricsService { ...@@ -175,7 +181,7 @@ public class TopologyMetrics implements TopologyMetricsService {
175 private class InnerLinkListener implements LinkListener { 181 private class InnerLinkListener implements LinkListener {
176 @Override 182 @Override
177 public void event(LinkEvent event) { 183 public void event(LinkEvent event) {
178 - recordEvent(event, true); 184 + recordEvent(event, topologyLinkEventMetric);
179 log.debug("Link Event: time = {} type = {} event = {}", 185 log.debug("Link Event: time = {} type = {} event = {}",
180 event.time(), event.type(), event); 186 event.time(), event.type(), event);
181 } 187 }
...@@ -187,11 +193,7 @@ public class TopologyMetrics implements TopologyMetricsService { ...@@ -187,11 +193,7 @@ public class TopologyMetrics implements TopologyMetricsService {
187 private class InnerTopologyListener implements TopologyListener { 193 private class InnerTopologyListener implements TopologyListener {
188 @Override 194 @Override
189 public void event(TopologyEvent event) { 195 public void event(TopologyEvent event) {
190 - // 196 + recordEvent(event, topologyGraphEventMetric);
191 - // NOTE: Don't update the eventRateMeter, because the real
192 - // events are already captured/counted.
193 - //
194 - recordEvent(event, false);
195 log.debug("Topology Event: time = {} type = {} event = {}", 197 log.debug("Topology Event: time = {} type = {} event = {}",
196 event.time(), event.type(), event); 198 event.time(), event.type(), event);
197 for (Event reason : event.reasons()) { 199 for (Event reason : event.reasons()) {
...@@ -206,7 +208,6 @@ public class TopologyMetrics implements TopologyMetricsService { ...@@ -206,7 +208,6 @@ public class TopologyMetrics implements TopologyMetricsService {
206 */ 208 */
207 private void clear() { 209 private void clear() {
208 synchronized (lastEvents) { 210 synchronized (lastEvents) {
209 - lastEventTimestampEpochMs = 0;
210 lastEvents.clear(); 211 lastEvents.clear();
211 } 212 }
212 } 213 }
...@@ -215,35 +216,32 @@ public class TopologyMetrics implements TopologyMetricsService { ...@@ -215,35 +216,32 @@ public class TopologyMetrics implements TopologyMetricsService {
215 * Registers the metrics. 216 * Registers the metrics.
216 */ 217 */
217 private void registerMetrics() { 218 private void registerMetrics() {
218 - metricsComponent = metricsService.registerComponent(COMPONENT_NAME); 219 + topologyDeviceEventMetric =
219 - metricsFeatureEventNotification = 220 + new EventMetric(metricsService, COMPONENT_NAME,
220 - metricsComponent.registerFeature(FEATURE_NAME); 221 + FEATURE_DEVICE_NAME);
221 - lastEventTimestampEpochMsGauge = 222 + topologyHostEventMetric =
222 - metricsService.registerMetric(metricsComponent, 223 + new EventMetric(metricsService, COMPONENT_NAME,
223 - metricsFeatureEventNotification, 224 + FEATURE_HOST_NAME);
224 - GAUGE_NAME, 225 + topologyLinkEventMetric =
225 - new Gauge<Long>() { 226 + new EventMetric(metricsService, COMPONENT_NAME,
226 - @Override 227 + FEATURE_LINK_NAME);
227 - public Long getValue() { 228 + topologyGraphEventMetric =
228 - return lastEventTimestampEpochMs; 229 + new EventMetric(metricsService, COMPONENT_NAME,
229 - } 230 + FEATURE_GRAPH_NAME);
230 - }); 231 +
231 - eventRateMeter = 232 + topologyDeviceEventMetric.registerMetrics();
232 - metricsService.createMeter(metricsComponent, 233 + topologyHostEventMetric.registerMetrics();
233 - metricsFeatureEventNotification, 234 + topologyLinkEventMetric.registerMetrics();
234 - METER_NAME); 235 + topologyGraphEventMetric.registerMetrics();
235 -
236 } 236 }
237 237
238 /** 238 /**
239 * Removes the metrics. 239 * Removes the metrics.
240 */ 240 */
241 private void removeMetrics() { 241 private void removeMetrics() {
242 - metricsService.removeMetric(metricsComponent, 242 + topologyDeviceEventMetric.removeMetrics();
243 - metricsFeatureEventNotification, 243 + topologyHostEventMetric.removeMetrics();
244 - GAUGE_NAME); 244 + topologyLinkEventMetric.removeMetrics();
245 - metricsService.removeMetric(metricsComponent, 245 + topologyGraphEventMetric.removeMetrics();
246 - metricsFeatureEventNotification,
247 - METER_NAME);
248 } 246 }
249 } 247 }
......
1 package org.onlab.onos.metrics.topology; 1 package org.onlab.onos.metrics.topology;
2 2
3 import java.util.List; 3 import java.util.List;
4 - 4 +import org.onlab.metrics.EventMetric;
5 -import com.codahale.metrics.Gauge;
6 -import com.codahale.metrics.Meter;
7 import org.onlab.onos.event.Event; 5 import org.onlab.onos.event.Event;
8 6
9 /** 7 /**
...@@ -18,18 +16,30 @@ public interface TopologyMetricsService { ...@@ -18,18 +16,30 @@ public interface TopologyMetricsService {
18 public List<Event> getEvents(); 16 public List<Event> getEvents();
19 17
20 /** 18 /**
21 - * Gets the Metrics' Gauge for the last topology event timestamp 19 + * Gets the Event Metric for the Device Events.
22 - * (ms from the epoch). 20 + *
21 + * @return the Event Metric for the Device Events.
22 + */
23 + public EventMetric topologyDeviceEventMetric();
24 +
25 + /**
26 + * Gets the Event Metric for the Host Events.
27 + *
28 + * @return the Event Metric for the Host Events.
29 + */
30 + public EventMetric topologyHostEventMetric();
31 +
32 + /**
33 + * Gets the Event Metric for the Link Events.
23 * 34 *
24 - * @return the Metrics' Gauge for the last topology event timestamp 35 + * @return the Event Metric for the Link Events.
25 - * (ms from the epoch)
26 */ 36 */
27 - public Gauge<Long> lastEventTimestampEpochMsGauge(); 37 + public EventMetric topologyLinkEventMetric();
28 38
29 /** 39 /**
30 - * Gets the Metrics' Meter for the topology events rate. 40 + * Gets the Event Metric for the Topology Graph Events.
31 * 41 *
32 - * @return the Metrics' Meter for the topology events rate 42 + * @return the Event Metric for the Topology Graph Events.
33 */ 43 */
34 - public Meter eventRateMeter(); 44 + public EventMetric topologyGraphEventMetric();
35 } 45 }
......
...@@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.JsonNode; ...@@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.JsonNode;
11 import com.fasterxml.jackson.databind.ObjectMapper; 11 import com.fasterxml.jackson.databind.ObjectMapper;
12 import com.fasterxml.jackson.databind.node.ObjectNode; 12 import com.fasterxml.jackson.databind.node.ObjectNode;
13 import org.apache.karaf.shell.commands.Command; 13 import org.apache.karaf.shell.commands.Command;
14 +import org.onlab.metrics.EventMetric;
14 import org.onlab.onos.cli.AbstractShellCommand; 15 import org.onlab.onos.cli.AbstractShellCommand;
15 import org.onlab.onos.metrics.topology.TopologyMetricsService; 16 import org.onlab.onos.metrics.topology.TopologyMetricsService;
16 17
...@@ -22,15 +23,13 @@ import org.onlab.onos.metrics.topology.TopologyMetricsService; ...@@ -22,15 +23,13 @@ import org.onlab.onos.metrics.topology.TopologyMetricsService;
22 public class TopologyEventsMetricsCommand extends AbstractShellCommand { 23 public class TopologyEventsMetricsCommand extends AbstractShellCommand {
23 24
24 private static final String FORMAT_GAUGE = 25 private static final String FORMAT_GAUGE =
25 - "Last Topology Event Timestamp (ms from epoch)=%d"; 26 + "Topology %s Event Timestamp (ms from epoch)=%d";
26 private static final String FORMAT_METER = 27 private static final String FORMAT_METER =
27 - "Topology Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f"; 28 + "Topology %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
28 29
29 @Override 30 @Override
30 protected void execute() { 31 protected void execute() {
31 TopologyMetricsService service = get(TopologyMetricsService.class); 32 TopologyMetricsService service = get(TopologyMetricsService.class);
32 - Gauge<Long> gauge = service.lastEventTimestampEpochMsGauge();
33 - Meter meter = service.eventRateMeter();
34 33
35 if (outputJson()) { 34 if (outputJson()) {
36 ObjectMapper mapper = new ObjectMapper() 35 ObjectMapper mapper = new ObjectMapper()
...@@ -38,32 +37,89 @@ public class TopologyEventsMetricsCommand extends AbstractShellCommand { ...@@ -38,32 +37,89 @@ public class TopologyEventsMetricsCommand extends AbstractShellCommand {
38 TimeUnit.MILLISECONDS, 37 TimeUnit.MILLISECONDS,
39 false)); 38 false));
40 ObjectNode result = mapper.createObjectNode(); 39 ObjectNode result = mapper.createObjectNode();
41 - try { 40 + result = json(mapper, result, "topologyDeviceEvent",
42 - // 41 + service.topologyDeviceEventMetric());
43 - // NOTE: The API for custom serializers is incomplete, 42 + result = json(mapper, result, "topologyHostEvent",
44 - // hence we have to parse the JSON string to create JsonNode. 43 + service.topologyHostEventMetric());
45 - // 44 + result = json(mapper, result, "topologyLinkEvent",
46 - final String gaugeJson = mapper.writeValueAsString(gauge); 45 + service.topologyLinkEventMetric());
47 - final String meterJson = mapper.writeValueAsString(meter); 46 + result = json(mapper, result, "topologyGraphEvent",
48 - JsonNode gaugeNode = mapper.readTree(gaugeJson); 47 + service.topologyGraphEventMetric());
49 - JsonNode meterNode = mapper.readTree(meterJson);
50 - result.put("lastTopologyEventTimestamp", gaugeNode);
51 - result.put("topologyEventRate", meterNode);
52 - } catch (JsonProcessingException e) {
53 - log.error("Error writing value as JSON string", e);
54 - } catch (IOException e) {
55 - log.error("Error writing value as JSON string", e);
56 - }
57 print("%s", result); 48 print("%s", result);
58 } else { 49 } else {
59 - TimeUnit rateUnit = TimeUnit.SECONDS; 50 + printEventMetric("Device", service.topologyDeviceEventMetric());
60 - double rateFactor = rateUnit.toSeconds(1); 51 + printEventMetric("Host", service.topologyHostEventMetric());
61 - print(FORMAT_GAUGE, gauge.getValue()); 52 + printEventMetric("Link", service.topologyLinkEventMetric());
62 - print(FORMAT_METER, meter.getCount(), 53 + printEventMetric("Graph", service.topologyGraphEventMetric());
63 - meter.getMeanRate() * rateFactor,
64 - meter.getOneMinuteRate() * rateFactor,
65 - meter.getFiveMinuteRate() * rateFactor,
66 - meter.getFifteenMinuteRate() * rateFactor);
67 } 54 }
68 } 55 }
56 +
57 + /**
58 + * Produces JSON node for an Event Metric.
59 + *
60 + * @param mapper the JSON object mapper to use
61 + * @param objectNode the JSON object node to use
62 + * @param propertyPrefix the property prefix to use
63 + * @param eventMetric the Event Metric with the data
64 + * @return JSON object node for the Event Metric
65 + */
66 + private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
67 + String propertyPrefix, EventMetric eventMetric) {
68 + String gaugeName = propertyPrefix + "Timestamp";
69 + String meterName = propertyPrefix + "Rate";
70 + Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
71 + Meter meter = eventMetric.eventRateMeter();
72 +
73 + objectNode.put(gaugeName, json(mapper, gauge));
74 + objectNode.put(meterName, json(mapper, meter));
75 + return objectNode;
76 + }
77 +
78 + /**
79 + * Produces JSON node for an Object.
80 + *
81 + * @param mapper the JSON object mapper to use
82 + * @param object the Object with the data
83 + * @return JSON node for the Object
84 + */
85 + private JsonNode json(ObjectMapper mapper, Object object) {
86 + //
87 + // NOTE: The API for custom serializers is incomplete,
88 + // hence we have to parse the JSON string to create JsonNode.
89 + //
90 + try {
91 + final String objectJson = mapper.writeValueAsString(object);
92 + JsonNode jsonNode = mapper.readTree(objectJson);
93 + return jsonNode;
94 + } catch (JsonProcessingException e) {
95 + log.error("Error writing value as JSON string", e);
96 + } catch (IOException e) {
97 + log.error("Error writing value as JSON string", e);
98 + }
99 + return null;
100 + }
101 +
102 + /**
103 + * Prints an Event Metric.
104 + *
105 + * @param operationStr the string with the intent operation to print
106 + * @param eventMetric the Event Metric to print
107 + */
108 + private void printEventMetric(String operationStr,
109 + EventMetric eventMetric) {
110 + Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
111 + Meter meter = eventMetric.eventRateMeter();
112 + TimeUnit rateUnit = TimeUnit.SECONDS;
113 + double rateFactor = rateUnit.toSeconds(1);
114 +
115 + // Print the Gauge
116 + print(FORMAT_GAUGE, operationStr, gauge.getValue());
117 +
118 + // Print the Meter
119 + print(FORMAT_METER, operationStr, meter.getCount(),
120 + meter.getMeanRate() * rateFactor,
121 + meter.getOneMinuteRate() * rateFactor,
122 + meter.getFiveMinuteRate() * rateFactor,
123 + meter.getFifteenMinuteRate() * rateFactor);
124 + }
69 } 125 }
......
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
35 <bundle>mvn:io.netty/netty-transport-native-epoll/4.0.23.Final</bundle> 35 <bundle>mvn:io.netty/netty-transport-native-epoll/4.0.23.Final</bundle>
36 <bundle>mvn:commons-pool/commons-pool/1.6</bundle> 36 <bundle>mvn:commons-pool/commons-pool/1.6</bundle>
37 37
38 - <bundle>mvn:com.hazelcast/hazelcast/3.3</bundle> 38 + <bundle>mvn:com.hazelcast/hazelcast/3.3.2</bundle>
39 <bundle>mvn:io.dropwizard.metrics/metrics-core/3.1.0</bundle> 39 <bundle>mvn:io.dropwizard.metrics/metrics-core/3.1.0</bundle>
40 <bundle>mvn:io.dropwizard.metrics/metrics-json/3.1.0</bundle> 40 <bundle>mvn:io.dropwizard.metrics/metrics-json/3.1.0</bundle>
41 <bundle>mvn:com.eclipsesource.minimal-json/minimal-json/0.9.1</bundle> 41 <bundle>mvn:com.eclipsesource.minimal-json/minimal-json/0.9.1</bundle>
......
...@@ -192,7 +192,7 @@ ...@@ -192,7 +192,7 @@
192 <dependency> 192 <dependency>
193 <groupId>com.hazelcast</groupId> 193 <groupId>com.hazelcast</groupId>
194 <artifactId>hazelcast</artifactId> 194 <artifactId>hazelcast</artifactId>
195 - <version>3.3</version> 195 + <version>3.3.2</version>
196 </dependency> 196 </dependency>
197 <dependency> 197 <dependency>
198 <groupId>com.eclipsesource.minimal-json</groupId> 198 <groupId>com.eclipsesource.minimal-json</groupId>
......
1 +package org.onlab.metrics;
2 +
3 +import com.codahale.metrics.Gauge;
4 +import com.codahale.metrics.Meter;
5 +
6 +/**
7 + * Metric measurements for events.
8 + */
9 +public class EventMetric {
10 + private static final String GAUGE_TIMESTAMP_NAME = "Timestamp.EpochMs";
11 + private static final String METER_RATE_NAME = "Rate";
12 +
13 + private final MetricsService metricsService;
14 + private final String componentName;
15 + private final String featureName;
16 +
17 + private MetricsComponent metricsComponent;
18 + private MetricsFeature metricsFeature;
19 +
20 + private volatile long lastEventTimestampEpochMs = 0;
21 + private Gauge<Long> lastEventTimestampGauge;
22 + private Meter eventRateMeter;
23 +
24 + /**
25 + * Constructor.
26 + *
27 + * @param metricsService the Metrics Service to use for Metrics
28 + * registration and deregistration
29 + * @param componentName the Metrics Component Name to use for Metrics
30 + * registration and deregistration
31 + * @param featureName the Metrics Feature Name to use for Metrics
32 + * registration and deregistration
33 + */
34 + public EventMetric(MetricsService metricsService, String componentName,
35 + String featureName) {
36 + this.metricsService = metricsService;
37 + this.componentName = componentName;
38 + this.featureName = featureName;
39 + }
40 +
41 + /**
42 + * Registers the metrics.
43 + */
44 + public void registerMetrics() {
45 + metricsComponent = metricsService.registerComponent(componentName);
46 + metricsFeature = metricsComponent.registerFeature(featureName);
47 +
48 + lastEventTimestampEpochMs = 0;
49 + lastEventTimestampGauge =
50 + metricsService.registerMetric(metricsComponent,
51 + metricsFeature,
52 + GAUGE_TIMESTAMP_NAME,
53 + new Gauge<Long>() {
54 + @Override
55 + public Long getValue() {
56 + return lastEventTimestampEpochMs;
57 + }
58 + });
59 +
60 + eventRateMeter = metricsService.createMeter(metricsComponent,
61 + metricsFeature,
62 + METER_RATE_NAME);
63 + }
64 +
65 + /**
66 + * Removes the metrics.
67 + */
68 + public void removeMetrics() {
69 + lastEventTimestampEpochMs = 0;
70 + metricsService.removeMetric(metricsComponent,
71 + metricsFeature,
72 + GAUGE_TIMESTAMP_NAME);
73 + metricsService.removeMetric(metricsComponent,
74 + metricsFeature,
75 + METER_RATE_NAME);
76 + }
77 +
78 + /**
79 + * Updates the metric measurements for a single event.
80 + */
81 + public void eventReceived() {
82 + lastEventTimestampEpochMs = System.currentTimeMillis();
83 + eventRateMeter.mark(1);
84 + }
85 +
86 + /**
87 + * Gets the last event timestamp Gauge (ms from the Epoch).
88 + *
89 + * @return the last event timestamp Gauge (ms from the Epoch)
90 + */
91 + public Gauge<Long> lastEventTimestampGauge() {
92 + return lastEventTimestampGauge;
93 + }
94 +
95 + /**
96 + * Gets the event rate meter.
97 + *
98 + * @return the event rate meter
99 + */
100 + public Meter eventRateMeter() {
101 + return eventRateMeter;
102 + }
103 +}