2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.metrics.exporters;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.io.metrics.MetricsConfiguration;
18 import org.openhab.io.metrics.MetricsExporter;
20 import io.micrometer.core.instrument.Clock;
21 import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
22 import io.micrometer.jmx.JmxConfig;
23 import io.micrometer.jmx.JmxMeterRegistry;
26 * The {@link JmxMetricsExporter} class implements a MetricsExporter for Java Management Extensions (JMX).
28 * @author Wouter Born - Initial contribution
31 public class JmxMetricsExporter extends MetricsExporter {
33 private @Nullable JmxMeterRegistry jmxMeterRegistry;
34 private @Nullable CompositeMeterRegistry meterRegistry;
37 public void start(CompositeMeterRegistry meterRegistry, MetricsConfiguration metricsConfiguration) {
38 jmxMeterRegistry = new JmxMeterRegistry(getJmxConfig(), Clock.SYSTEM);
39 meterRegistry.add(jmxMeterRegistry);
43 public void shutdown() {
44 JmxMeterRegistry jmxMeterRegistry = this.jmxMeterRegistry;
45 if (jmxMeterRegistry != null) {
46 jmxMeterRegistry.stop();
47 this.jmxMeterRegistry = null;
50 CompositeMeterRegistry meterRegistry = this.meterRegistry;
51 if (meterRegistry != null) {
52 meterRegistry.remove(jmxMeterRegistry);
53 this.meterRegistry = null;
57 private JmxConfig getJmxConfig() {
58 return new JmxConfig() {
60 @io.micrometer.core.lang.Nullable
62 public String get(@Nullable String k) {
63 return null; // accept the rest of the defaults
69 protected boolean isEnabled(MetricsConfiguration config) {
70 return config.jmxMetricsEnabled;