Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate micrometer metering #2133

Merged
merged 1 commit into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bom/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,6 @@
<version>0.9.12</version>
<scope>compile</scope>
</dependency>

</dependencies>

</project>
9 changes: 9 additions & 0 deletions bundles/org.openhab.core.io.monitor/bnd.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Bundle-SymbolicName: ${project.artifactId}
Import-Package: \
org.eclipse.jdt.annotation.*;resolution:=optional,\
org.openhab.*;version=!,\
org.osgi.framework,\
org.osgi.service.*,\
org.slf4j.*,\
com.google.gson.*;version="[2.8,3)"
Export-Package: io.micrometer.core.*
38 changes: 38 additions & 0 deletions bundles/org.openhab.core.io.monitor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,44 @@
<artifactId>org.openhab.core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.automation</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<version>1.6.3</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>embed-dependencies</id>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<includeTypes>jar</includeTypes>
<excludeGroupIds>org.apache.karaf.features,org.openhab.core.bundles</excludeGroupIds>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<excludeTransitive>true</excludeTransitive>
<type>jar</type>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.io.monitor;

import org.eclipse.jdt.annotation.NonNullByDefault;

import io.micrometer.core.instrument.composite.CompositeMeterRegistry;

/**
* The {@link MeterRegistryProvider} interface provides a means to retrieve the default OH meter registry instance
*
* @author Robert Bach - Initial contribution
*/
@NonNullByDefault
public interface MeterRegistryProvider {

CompositeMeterRegistry getOHMeterRegistry();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.io.monitor.internal;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.io.monitor.MeterRegistryProvider;
import org.openhab.core.io.monitor.internal.metrics.BundleStateMetric;
import org.openhab.core.io.monitor.internal.metrics.EventCountMetric;
import org.openhab.core.io.monitor.internal.metrics.JVMMetric;
import org.openhab.core.io.monitor.internal.metrics.OpenhabCoreMeterBinder;
import org.openhab.core.io.monitor.internal.metrics.RuleMetric;
import org.openhab.core.io.monitor.internal.metrics.ThingStateMetric;
import org.openhab.core.io.monitor.internal.metrics.ThreadPoolMetric;
import org.openhab.core.service.ReadyMarker;
import org.openhab.core.service.ReadyMarkerFilter;
import org.openhab.core.service.ReadyService;
import org.openhab.core.service.StartLevelService;
import org.openhab.core.thing.ThingRegistry;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;

/**
* The {@link DefaultMetricsRegistration} class registers all openHAB internal metrics with the global MeterRegistry.
*
* @author Robert Bach - Initial contribution
*/
@Component(immediate = true, service = MeterRegistryProvider.class)
@NonNullByDefault
public class DefaultMetricsRegistration implements ReadyService.ReadyTracker, MeterRegistryProvider {

private final Logger logger = LoggerFactory.getLogger(DefaultMetricsRegistration.class);
public static final Tag OH_CORE_METRIC_TAG = Tag.of("openhab_core_metric", "true");
private final BundleContext bundleContext;
private final Set<OpenhabCoreMeterBinder> meters = new HashSet<>();
private final CompositeMeterRegistry registry = Metrics.globalRegistry;
private final ReadyService readyService;
private final ThingRegistry thingRegistry;

@Activate
public DefaultMetricsRegistration(BundleContext bundleContext, final @Reference ReadyService readyService,
final @Reference ThingRegistry thingRegistry) {
this.bundleContext = bundleContext;
this.readyService = readyService;
this.thingRegistry = thingRegistry;
}

@Activate
protected void activate() {
logger.trace("Activating DefaultMetricsRegistration...");
readyService.registerTracker(this, new ReadyMarkerFilter().withType(StartLevelService.STARTLEVEL_MARKER_TYPE)
.withIdentifier(Integer.toString(StartLevelService.STARTLEVEL_RULES)));
}

@Deactivate
public void deactivate() {
unregisterMeters();
readyService.unregisterTracker(this);
}

private void registerMeters() {
logger.debug("Registering meters...");
Set<Tag> tags = Set.of(OH_CORE_METRIC_TAG);
meters.add(new JVMMetric(tags));
meters.add(new ThreadPoolMetric(tags));
meters.add(new BundleStateMetric(bundleContext, tags));
meters.add(new ThingStateMetric(bundleContext, thingRegistry, tags));
meters.add(new EventCountMetric(bundleContext, tags));
meters.add(new RuleMetric(bundleContext, tags));
meters.add(new ThreadPoolMetric(tags));

meters.forEach(m -> m.bindTo(registry));
}

private void unregisterMeters() {
this.meters.forEach(OpenhabCoreMeterBinder::unbind);
}

@Override
public void onReadyMarkerAdded(ReadyMarker readyMarker) {
registerMeters();
}

@Override
public void onReadyMarkerRemoved(ReadyMarker readyMarker) {
unregisterMeters();
}

@Override
public CompositeMeterRegistry getOHMeterRegistry() {
return registry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.Set;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.events.Event;
import org.openhab.core.events.EventFilter;
import org.openhab.core.events.EventSubscriber;
Expand All @@ -37,6 +39,7 @@
* @author Kai Kreuzer - Initial contribution
*/
@Component
@NonNullByDefault
public class EventLogger implements EventSubscriber, ReadyTracker {

private final Map<String, Logger> eventLoggers = new HashMap<>();
Expand All @@ -63,6 +66,7 @@ public Set<String> getSubscribedEventTypes() {
}

@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.io.monitor.internal.metrics;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.Nullable;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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.Tags;

/**
* The {@link BundleStateMetric} class implements a set of gauge metrics for the OSGI bundles states
*
* @author Robert Bach - Initial contribution
*/
public class BundleStateMetric implements OpenhabCoreMeterBinder, BundleListener {
private final Logger logger = LoggerFactory.getLogger(BundleStateMetric.class);
public static final String METRIC_NAME = "openhab.bundle.state";
private static final String BUNDLE_TAG_NAME = "bundle";
private final Meter.Id commonMeterId;
private final Map<Meter.Id, AtomicInteger> registeredMeters = new HashMap<>();
@Nullable
private MeterRegistry meterRegistry = null;
private final BundleContext bundleContext;

public BundleStateMetric(BundleContext bundleContext, Collection<Tag> tags) {
commonMeterId = new Meter.Id(METRIC_NAME, Tags.of(tags), "state", "openHAB OSGi bundles state",
Meter.Type.GAUGE);
this.bundleContext = bundleContext;
}

@Override
public void bindTo(@io.micrometer.core.lang.NonNull MeterRegistry meterRegistry) {
unbind();
logger.debug("BundleStateMetric is being bound...");
this.meterRegistry = meterRegistry;
Stream.of(bundleContext.getBundles()).forEach(bundle -> {
createOrUpdateMetricForBundleState(bundle.getSymbolicName(), bundle.getState());
});
bundleContext.addBundleListener(this);
}

@Override
public void bundleChanged(BundleEvent bundleEvent) {
if (meterRegistry == null) {
return;
}
String bundleName = bundleEvent.getBundle().getSymbolicName();
int state = bundleEvent.getBundle().getState();
createOrUpdateMetricForBundleState(bundleName, state);
}

private void createOrUpdateMetricForBundleState(String bundleName, int state) {
Meter.Id uniqueId = commonMeterId.withTag(Tag.of(BUNDLE_TAG_NAME, bundleName));
AtomicInteger bundleStateHolder = registeredMeters.get(uniqueId);
if (bundleStateHolder == null) {
bundleStateHolder = new AtomicInteger();
Gauge.builder(uniqueId.getName(), bundleStateHolder, AtomicInteger::get).baseUnit(uniqueId.getBaseUnit())
.description(uniqueId.getDescription()).tags(uniqueId.getTags()).register(meterRegistry);
registeredMeters.put(uniqueId, bundleStateHolder);
}
bundleStateHolder.set(state);
}

@Override
public void unbind() {
if (meterRegistry == null) {
return;
}
bundleContext.removeBundleListener(this);
registeredMeters.keySet().forEach(meterRegistry::remove);
registeredMeters.clear();
meterRegistry = null;
}
}
Loading