forked from infinispan/infinispan
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MetricsCollector.java
227 lines (191 loc) · 8.17 KB
/
MetricsCollector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package org.infinispan.metrics.impl;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.infinispan.commons.stat.TimerTracker;
import org.infinispan.configuration.global.GlobalConfiguration;
import org.infinispan.configuration.global.GlobalMetricsConfiguration;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.factories.annotations.Start;
import org.infinispan.factories.annotations.Stop;
import org.infinispan.factories.annotations.SurvivesRestarts;
import org.infinispan.factories.impl.ComponentRef;
import org.infinispan.factories.impl.MBeanMetadata;
import org.infinispan.factories.scopes.Scope;
import org.infinispan.factories.scopes.Scopes;
import org.infinispan.metrics.Constants;
import org.infinispan.metrics.config.MicrometerMeterRegistryConfiguration;
import org.infinispan.remoting.transport.Transport;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Timer;
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry;
/**
* Keeps a reference to the Micrometer MeterRegistry. Optional component in component registry. Availability based on
* available jars in classpath! See {@link MetricsCollectorFactory}.
*
* @author anistor@redhat.com
* @since 10.1
*/
@Scope(Scopes.GLOBAL)
@SurvivesRestarts
public class MetricsCollector implements Constants {
private static final Log log = LogFactory.getLog(MetricsCollector.class);
private MeterRegistry registry;
private Tag nodeTag;
private Tag cacheManagerTag;
@Inject
GlobalConfiguration globalConfig;
@Inject
ComponentRef<Transport> transportRef;
protected MetricsCollector() {
}
public PrometheusMeterRegistry registry() {
return registry instanceof PrometheusMeterRegistry ? (PrometheusMeterRegistry) registry : null;
}
@Start
protected void start() {
createMeterRegistry();
new BaseAdditionalMetrics().bindTo(registry);
new VendorAdditionalMetrics().bindTo(registry);
Transport transport = transportRef.running();
String nodeName = transport != null ? transport.getAddress().toString() : globalConfig.transport().nodeName();
if (nodeName == null) {
//TODO [anistor] Maybe we should just ensure a unique node name was set in all tests and also in real life usage, even for local cache managers
nodeName = generateRandomName();
//throw new CacheConfigurationException("Node name must always be specified in configuration if metrics are enabled.");
}
nodeTag = Tag.of(NODE_TAG_NAME, nodeName);
if (globalConfig.metrics().namesAsTags()) {
cacheManagerTag = Tag.of(CACHE_MANAGER_TAG_NAME, globalConfig.cacheManagerName());
}
}
@Stop
protected void stop() {
if (registry != null) {
registry.close();
registry = null;
}
}
/**
* Generate a not so random name based on host name.
*/
private static String generateRandomName() {
String hostName = null;
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (Throwable ignored) {
}
if (hostName == null) {
try {
hostName = InetAddress.getByName(null).getHostName();
} catch (Throwable ignored) {
}
}
if (hostName == null) {
hostName = "localhost";
} else {
int dotPos = hostName.indexOf('.');
if (dotPos > 0 && !Character.isDigit(hostName.charAt(0))) {
hostName = hostName.substring(0, dotPos);
}
}
int rand = 1 + ThreadLocalRandom.current().nextInt(Short.MAX_VALUE * 2);
return hostName + '-' + rand;
}
public Set<Object> registerMetrics(Object instance, Collection<MBeanMetadata.AttributeMetadata> attributes, String namePrefix, String cacheName) {
return registerMetrics(instance, attributes, namePrefix, asTag(CACHE_TAG_NAME, cacheName), nodeTag);
}
public Set<Object> registerMetrics(Object instance, Collection<MBeanMetadata.AttributeMetadata> attributes, String namePrefix, String cacheName, String nodeName) {
return registerMetrics(instance, attributes, namePrefix, asTag(CACHE_TAG_NAME, cacheName), asTag(NODE_TAG_NAME, nodeName));
}
private Set<Object> registerMetrics(Object instance, Collection<MBeanMetadata.AttributeMetadata> attributes, String namePrefix, Tag ...initialTags) {
Set<Object> metricIds = new HashSet<>(attributes.size());
GlobalMetricsConfiguration metricsCfg = globalConfig.metrics();
List<Tag> tags = prepareTags(initialTags);
for (MBeanMetadata.AttributeMetadata attr : attributes) {
Supplier<Number> getter = (Supplier<Number>) attr.getter(instance);
Consumer<TimerTracker> setter = (Consumer<TimerTracker>) attr.setter(instance);
if (getter != null || setter != null) {
String metricName = VendorAdditionalMetrics.PREFIX + namePrefix + NameUtils.decamelize(attr.getName());
if (getter != null) {
if (metricsCfg.gauges()) {
Gauge gauge = Gauge.builder(metricName, getter)
.tags(tags)
.strongReference(true)
.description(attr.getDescription())
.register(registry);
Meter.Id id = gauge.getId();
if (log.isTraceEnabled()) {
log.tracef("Registering gauge metric %s", id);
}
metricIds.add(id);
}
} else {
if (metricsCfg.histograms()) {
Timer timer = Timer.builder(metricName)
.tags(tags)
.description(attr.getDescription())
.register(registry);
Meter.Id id = timer.getId();
if (log.isTraceEnabled()) {
log.tracef("Registering histogram metric %s", id);
}
setter.accept(new TimerTrackerImpl(timer));
metricIds.add(id);
}
}
}
}
if (log.isTraceEnabled()) {
log.tracef("Registered %d metrics. Metric registry @%x contains %d metrics.",
metricIds.size(), System.identityHashCode(registry), registry.getMeters().size());
}
return metricIds;
}
private List<Tag> prepareTags(Tag ...tags) {
List<Tag> allTags = Arrays.stream(tags).filter(Objects::nonNull).collect(Collectors.toList());
if (cacheManagerTag != null) allTags.add(cacheManagerTag);
return allTags;
}
private Tag asTag(String key, String value) {
return value != null
? Tag.of(key, value)
: null;
}
public void unregisterMetric(Object metricId) {
if (registry == null) {
return;
}
Meter removed = registry.remove((Meter.Id) metricId);
if (log.isTraceEnabled()) {
if (removed != null) {
log.tracef("Unregistered metric \"%s\". Metric registry @%x contains %d metrics.",
metricId, System.identityHashCode(registry), registry.getMeters().size());
} else {
log.tracef("Could not remove unexisting metric \"%s\". Metric registry @%x contains %d metrics.",
metricId, System.identityHashCode(registry), registry.getMeters().size());
}
}
}
private void createMeterRegistry() {
MicrometerMeterRegistryConfiguration configuration = globalConfig.module(MicrometerMeterRegistryConfiguration.class);
registry = configuration == null ? null : configuration.meterRegistry();
if (registry == null) {
registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
}
}
}