Skip to content

Package: MetricsServiceImpl

MetricsServiceImpl

nameinstructionbranchcomplexitylinemethod
MetricsServiceImpl()
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
convertToDotNotation(String[])
M: 37 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
enableJmxSupport()
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getCounter(String, String, String[])
M: 26 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getHistogram(String, String, String[])
M: 26 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getMetricName(String, String, String[])
M: 19 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMetricRegistry()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getTimer(String, String, String[])
M: 26 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
isJmxEnabled()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
registerGauge(Gauge, String, String, String[])
M: 29 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.commons.metric;
14:
15: import com.codahale.metrics.Counter;
16: import com.codahale.metrics.Gauge;
17: import com.codahale.metrics.Histogram;
18: import com.codahale.metrics.JmxReporter;
19: import com.codahale.metrics.JmxReporter.Builder;
20: import com.codahale.metrics.MetricRegistry;
21: import com.codahale.metrics.SharedMetricRegistries;
22: import com.codahale.metrics.Timer;
23: import org.eclipse.kapua.KapuaException;
24: import org.eclipse.kapua.commons.setting.system.SystemSetting;
25: import org.eclipse.kapua.commons.setting.system.SystemSettingKey;
26: import org.slf4j.Logger;
27: import org.slf4j.LoggerFactory;
28:
29: import java.text.MessageFormat;
30: import java.util.concurrent.TimeUnit;
31:
32: /**
33: * Metric report exporter handler.
34: * It provides methods for register/unregister metrics in the context
35: *
36: * @since 1.0
37: */
38: public class MetricsServiceImpl implements MetricsService {
39:
40: private static final Logger logger = LoggerFactory.getLogger(MetricsServiceImpl.class);
41:
42: public static final String METRICS_NAME_FORMAT = "{0}.{1}.{2}";
43:
44: private MetricRegistry metricRegistry;
45:
46: private JmxReporter jmxReporter;
47:
48: /**
49: * Default metric service constructor
50: */
51: MetricsServiceImpl() {
52: try {
53: metricRegistry = SharedMetricRegistries.getDefault();
54: logger.info("Default Metric Registry loaded");
55: } catch (IllegalStateException e) {
56: metricRegistry = new MetricRegistry();
57: logger.warn("Unable to load Default Metric Registry - creating a new one");
58: }
59:
60:• if (isJmxEnabled()) {
61: enableJmxSupport();
62: }
63: }
64:
65: private void enableJmxSupport() {
66: final Builder builder = JmxReporter.forRegistry(this.metricRegistry);
67: builder.convertDurationsTo(TimeUnit.MILLISECONDS);
68: builder.convertRatesTo(TimeUnit.SECONDS);
69: builder.inDomain("org.eclipse.kapua");
70: this.jmxReporter = builder.build();
71: this.jmxReporter.start();
72: /*
73: * As Kapua services don't have any proper lifecycle management we can only
74: * start the reporter but never stop it.
75: */
76: }
77:
78: @Override
79: public MetricRegistry getMetricRegistry() {
80: return metricRegistry;
81: }
82:
83: @Override
84: public Counter getCounter(String module, String component, String... names) {
85: String name = getMetricName(module, component, names);
86: Counter counter = metricRegistry.getCounters().get(name);
87:• if (counter == null) {
88: logger.debug("Creating a Counter: {}", name);
89: counter = metricRegistry.counter(name);
90: }
91: return counter;
92: }
93:
94: @Override
95: public Histogram getHistogram(String module, String component, String... names) {
96: String name = getMetricName(module, component, names);
97: Histogram histogram = metricRegistry.getHistograms().get(name);
98:• if (histogram == null) {
99: logger.debug("Creating a Histogram: {}", name);
100: histogram = metricRegistry.histogram(name);
101: }
102: return histogram;
103: }
104:
105: @Override
106: public Timer getTimer(String module, String component, String... names) {
107: String name = getMetricName(module, component, names);
108: Timer timer = metricRegistry.getTimers().get(name);
109:• if (timer == null) {
110: logger.debug("Creating a Timer: {}", name);
111: timer = metricRegistry.timer(name);
112: }
113: return timer;
114: }
115:
116: @Override
117: public void registerGauge(Gauge<?> gauge, String module, String component, String... names) throws KapuaException {
118: String name = getMetricName(module, component, names);
119:• if (metricRegistry.getGauges().get(name) != null) {
120: throw KapuaException.internalError(MessageFormat.format("A metric with the name {0} is already defined!", name));
121: } else {
122: metricRegistry.register(name, gauge);
123: }
124: }
125:
126: /**
127: * Build the metric name based on module, component and metric names
128: *
129: * @param module
130: * @param component
131: * @param metricsName
132: * @return
133: */
134: private String getMetricName(String module, String component, String... metricsName) {
135: return MessageFormat.format(METRICS_NAME_FORMAT, module, component, convertToDotNotation(metricsName));
136: }
137:
138: /**
139: * Convert the metric names to a concatenated dot separated string
140: *
141: * @param metricsName
142: * @return
143: */
144: private String convertToDotNotation(String... metricsName) {
145: StringBuilder builder = new StringBuilder();
146: boolean firstMetricName = true;
147:• for (String s : metricsName) {
148:• if (!firstMetricName) {
149: builder.append('.');
150: }
151: firstMetricName = false;
152: builder.append(s);
153: }
154: return builder.toString();
155: }
156:
157: /**
158: * Tests is JMX is enabled
159: * <p>
160: * The default is that JMX support is enabled
161: * </p>
162: *
163: * @return {@code true} JMX should be enabled, {@code false} otherwise
164: */
165: private static boolean isJmxEnabled() {
166: return SystemSetting.getInstance().getBoolean(SystemSettingKey.METRICS_ENABLE_JMX, false);
167: }
168:
169: }