/
MicrometerRegistryService.java
104 lines (87 loc) · 3.79 KB
/
MicrometerRegistryService.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
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.extension.micrometer;
import static org.wildfly.extension.micrometer.MicrometerExtensionLogger.MICROMETER_LOGGER;
import static org.wildfly.extension.micrometer.MicrometerSubsystemDefinition.MICROMETER_REGISTRY_RUNTIME_CAPABILITY;
import java.io.IOException;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.jboss.as.controller.CapabilityServiceBuilder;
import org.jboss.as.controller.OperationContext;
import org.jboss.msc.Service;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StopContext;
import org.wildfly.extension.micrometer.jmx.JmxMicrometerCollector;
import org.wildfly.extension.micrometer.registry.NoOpRegistry;
import org.wildfly.extension.micrometer.registry.WildFlyOtlpRegistry;
import org.wildfly.extension.micrometer.registry.WildFlyRegistry;
class MicrometerRegistryService implements Service {
private final Consumer<WildFlyRegistry> registriesConsumer;
private final WildFlyMicrometerConfig config;
private WildFlyRegistry registry;
/**
* Installs a service that provides {@link WildFlyRegistry}, and provides a {@link Supplier} the
* subsystem can use to obtain that registry.
*
* @param context the management operation context to use to install the service. Cannot be {@code null}
* @param config the configuration object for the registry
* @return the {@link Supplier}. Will not return {@code null}.
*/
static Supplier<WildFlyRegistry> install(OperationContext context, WildFlyMicrometerConfig config) {
CapabilityServiceBuilder<?> serviceBuilder = context.getCapabilityServiceTarget()
.addCapability(MICROMETER_REGISTRY_RUNTIME_CAPABILITY);
RegistrySupplier registrySupplier =
new RegistrySupplier(serviceBuilder.provides(MICROMETER_REGISTRY_RUNTIME_CAPABILITY.getCapabilityServiceName()));
serviceBuilder.setInstance(new MicrometerRegistryService(registrySupplier, config))
.install();
return registrySupplier;
}
private MicrometerRegistryService(Consumer<WildFlyRegistry> registriesConsumer, WildFlyMicrometerConfig config) {
this.registriesConsumer = registriesConsumer;
this.config = config;
}
@Override
public void start(StartContext context) {
if (config.url() != null) {
registry = new WildFlyOtlpRegistry(config);
} else {
MICROMETER_LOGGER.noOpRegistryChosen();
registry = new NoOpRegistry();
}
try {
// register metrics from JMX MBeans for base metrics
new JmxMicrometerCollector(registry).init();
} catch (IOException e) {
throw MICROMETER_LOGGER.failedInitializeJMXRegistrar(e);
}
registriesConsumer.accept(registry);
}
@Override
public void stop(StopContext context) {
if (registry != null) {
registry.close();
registry = null;
}
registriesConsumer.accept(null);
}
/* Caches the WildFlyRegistry created in Service.start for use by the subsystem. */
private static final class RegistrySupplier implements Consumer<WildFlyRegistry>, Supplier<WildFlyRegistry> {
private final Consumer<WildFlyRegistry> wrapped;
private volatile WildFlyRegistry registry;
private RegistrySupplier(Consumer<WildFlyRegistry> wrapped) {
this.wrapped = wrapped;
}
@Override
public void accept(WildFlyRegistry registry) {
this.registry = registry;
// Pass the registry on to MSC's consumer
wrapped.accept(registry);
}
@Override
public WildFlyRegistry get() {
return registry;
}
}
}