blob: 3006b0ce91427d743bccc8d073630c7f787b2209 [file] [log] [blame]
Ray Milkey26921af2014-06-30 16:27:40 -07001package net.onrc.onos.core.metrics;
2
Ray Milkey49d67be2014-07-10 13:47:01 -07003import com.codahale.metrics.Counter;
4import com.codahale.metrics.Histogram;
5import com.codahale.metrics.Meter;
6import com.codahale.metrics.Metric;
Ray Milkey26921af2014-06-30 16:27:40 -07007import com.codahale.metrics.MetricRegistry;
Ray Milkey49d67be2014-07-10 13:47:01 -07008import com.codahale.metrics.Timer;
Ray Milkey26921af2014-06-30 16:27:40 -07009
10/**
11 * This class acts a singleton to hold the Metrics registry for ONOS.
12 */
13public final class OnosMetrics {
14
15 /**
16 * Hide constructor. The only way to get the registry is through the
17 * singleton getter.
18 */
19 private OnosMetrics() {}
20
Ray Milkey49d67be2014-07-10 13:47:01 -070021 /**
22 * Components that can hold Metrics. This is used as the first part of
23 * a Metric's name.
24 */
25 public enum MetricsComponents {
26 /**
27 * Global scope, not associated with a particular component.
28 */
29 GLOBAL("Global"),
30
31 /**
32 * Topology component.
33 */
34 TOPOLOGY("Topology");
35
36 private final String name;
37
38 /**
39 * Constructor allows specifying an alternate string name.
40 *
41 * @param name string for the name of the component
42 */
43 private MetricsComponents(String name) {
44 this.name = name;
45 }
46
47 @Override
48 public String toString() {
49 return name;
50 }
51 }
52
53 /**
54 * Features that can hold Metrics. This is used as the second part of
55 * a Metric's name.
56 */
57 public enum MetricsFeatures {
58 /**
59 * Global scope, not associated with a particular feature.
60 */
61 GLOBAL("Global"),
62
63 /**
64 * Topology Intents Framework feature. (example)
65 */
66 TOPOLOGY_INTENTS("IntentsFramework");
67
68 private final String name;
69
70 /**
71 * Constructor allows specifying an alternate string name.
72 *
73 * @param name string for the name of the component
74 */
75 private MetricsFeatures(String name) {
76 this.name = name;
77 }
78
79 @Override
80 public String toString() {
81 return name;
82 }
83 }
84
Ray Milkey26921af2014-06-30 16:27:40 -070085 private static final MetricRegistry METRICS_REGISTRY = new MetricRegistry();
86
87 /**
Ray Milkey49d67be2014-07-10 13:47:01 -070088 * Generates a name for a Metric from its component and feature.
89 *
90 * @param component component the metric is defined in
91 * @param feature feature the metric is defined in
92 * @param metricName local name of the metric
93 *
94 * @return full name of the metric
95 */
96 public static String generateName(final MetricsComponents component,
97 final MetricsFeatures feature,
98 final String metricName) {
99 return MetricRegistry.name(component.toString(),
100 feature.toString(),
101 metricName);
102 }
103
104 /**
105 * Creates a Counter metric.
106 *
107 * @param component component the Counter is defined in
108 * @param feature feature the Counter is defined in
109 * @param metricName local name of the metric
110 * @return Counter Meteric
111 */
112 public static Counter createCounter(final MetricsComponents component,
113 final MetricsFeatures feature,
114 final String metricName) {
115 final String name = generateName(component, feature, metricName);
116 return METRICS_REGISTRY.counter(name);
117 }
118
119 /**
120 * Creates a Histogram metric.
121 *
122 * @param component component the Histogram is defined in
123 * @param feature feature the Histogram is defined in
124 * @param metricName local name of the metric
125 * @return Histogram Metric
126 */
127 public static Histogram createHistogram(final MetricsComponents component,
128 final MetricsFeatures feature,
129 final String metricName) {
130 final String name = generateName(component, feature, metricName);
131 return METRICS_REGISTRY.histogram(name);
132 }
133
134 /**
135 * Creates a Timer metric.
136 *
137 * @param component component the Timer is defined in
138 * @param feature feature the Timeer is defined in
139 * @param metricName local name of the metric
140 * @return Timer Metric
141 */
142 public static Timer createTimer(final MetricsComponents component,
143 final MetricsFeatures feature,
144 final String metricName) {
145 final String name = generateName(component, feature, metricName);
146 return METRICS_REGISTRY.timer(name);
147 }
148
149 /**
150 * Creates a Meter metric.
151 *
152 * @param component component the Meter is defined in
153 * @param feature feature the Meter is defined in
154 * @param metricName local name of the metric
155 * @return Meter Metric
156 */
157 public static Meter createMeter(final MetricsComponents component,
158 final MetricsFeatures feature,
159 final String metricName) {
160 final String name = generateName(component, feature, metricName);
161 return METRICS_REGISTRY.meter(name);
162 }
163
164 /**
165 * Registers an already created Metric. This is used for situation where a
166 * caller needs to allocate its own Metric, but still register it with the
167 * system.
168 *
169 * @param component component the Metric is defined in
170 * @param feature feature the metric is defined in
171 * @param metricName local name of the metric
172 * @param metric Metric to register
173 */
174 public static void registerMetric(MetricsComponents component,
175 MetricsFeatures feature,
176 String metricName,
177 Metric metric) {
178 final String name = generateName(component, feature, metricName);
179 METRICS_REGISTRY.register(name, metric);
180 }
181
182 /**
Ray Milkey26921af2014-06-30 16:27:40 -0700183 * Get the singleton Metrics registry. A single instance of
184 * the registry is statically allocated and then used by all callers.
185 *
186 * @return Metrics registry
187 */
188 public static MetricRegistry getMetricsRegistry() {
189 return METRICS_REGISTRY;
190 }
191}
192