Pavlin Radoslavov

Added a new class EventMetric that can be used for measuring events:

rate and last event timestamp (ms from epoch)

Change-Id: Ifce6c2ca911f3b288a0efd9061befc936d06da51
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 +}