Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* test cleanup

* docs cleanup and minor fixes

* javadoc cleanup/fixes

* imports cleanup

* whitespace

* use for loop instead of forEach

* misc cleanup, minor tweaks
  • Loading branch information
burtbeckwith authored May 18, 2022
1 parent 515b5d0 commit 98109ea
Show file tree
Hide file tree
Showing 145 changed files with 1,241 additions and 2,296 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ reflectionsVersion=0.10.2

title=Micronaut Micrometer
projectDesc=Provides integration between Micronaut and Micrometer
projectUrl=http://micronaut.io
projectUrl=https://micronaut.io
githubSlug=micronaut-projects/micronaut-micrometer
developers=Graeme Rocher

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
*/
package io.micronaut.configuration.metrics.aggregator;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micronaut.core.annotation.NonNull;

import javax.validation.constraints.NotNull;
import java.util.Collection;

/**
* Default implementation of {@link MeterRegistryConfigurer} that adds the binders and filters
* to the micrometer meter registry. This is specifically needed for the {@link io.micronaut.configuration.metrics.management.endpoint.MetricsEndpoint}
* to the micrometer meter registry. This is specifically needed for the {@link io.micronaut.configuration.metrics.management.endpoint.MetricsEndpoint}
*
* @author Christian Oestreich
* @since 1.0
Expand All @@ -37,12 +38,11 @@ public class CompositeMeterRegistryConfigurer implements MeterRegistryConfigurer
/**
* Constructor for the micrometer meter registry configurer.
*
* @param binders list of binder beans
* @param filters list of filter beans
* @param binders binder beans
* @param filters filter beans
*/
public CompositeMeterRegistryConfigurer(
Collection<MeterBinder> binders,
Collection<MeterFilter> filters) {
public CompositeMeterRegistryConfigurer(Collection<MeterBinder> binders,
Collection<MeterFilter> filters) {
this.binders = binders;
this.filters = filters;
}
Expand All @@ -56,7 +56,7 @@ public CompositeMeterRegistryConfigurer(
* @param meterRegistry Meter registry to bind metrics to.
*/
@Override
public void configure(@NotNull CompositeMeterRegistry meterRegistry) {
public void configure(@NonNull CompositeMeterRegistry meterRegistry) {
addFilters(meterRegistry);
addBinders(meterRegistry);
}
Expand All @@ -67,28 +67,33 @@ public Class<CompositeMeterRegistry> getType() {
}

/**
* Add filters to the meter regitry. More details available at https://micrometer.io/docs/concepts#_meter_filters
* Add filters to the meter registry. More details available at https://micrometer.io/docs/concepts#_meter_filters
*
* @param meterRegistry the meter registry to configure
* @param meterRegistry the registry
*/
private void addFilters(@NotNull CompositeMeterRegistry meterRegistry) {
private void addFilters(@NonNull CompositeMeterRegistry meterRegistry) {
if (filters != null && !filters.isEmpty()) {
filters.forEach(meterRegistry.config()::meterFilter);
MeterRegistry.Config config = meterRegistry.config();
for (MeterFilter filter : filters) {
config.meterFilter(filter);
}
}
}

/**
* Add binders to the meter registry. There are default binders available.
* Add binders to the meter registry. There are default binders available.
* <p>
* {@link io.micronaut.configuration.metrics.binder.jvm.JvmMeterRegistryBinderFactory}
* {@link io.micronaut.configuration.metrics.binder.logging.LogbackMeterRegistryBinderFactory}
* {@link io.micronaut.configuration.metrics.binder.system.SystemMeterRegistryBinderFactoryFactory}
*
* @param meterRegistry the meter registry
*/
private void addBinders(@NotNull CompositeMeterRegistry meterRegistry) {
private void addBinders(@NonNull CompositeMeterRegistry meterRegistry) {
if (binders != null && !binders.isEmpty()) {
binders.forEach((binder) -> binder.bindTo(meterRegistry));
for (MeterBinder binder : binders) {
binder.bindTo(meterRegistry);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import io.micrometer.core.instrument.MeterRegistry;

/**
* Class that will configure meter registries. This is done on bean added event so that
* composite registry can be skipped and non-composite registries can be added to composite.
* Configures meter registries. This is done on bean added event so composite
* registry can be skipped and non-composite registries can be added to composite.
*
* @author Christian Oestreich
* @param <T> an instance of a meter registry that will be configured
Expand All @@ -28,14 +28,14 @@
public interface MeterRegistryConfigurer<T extends MeterRegistry> {

/**
* Method to configure a meter registry with binders, filters, etc.
* Configures a meter registry with binders, filters, etc.
*
* @param meterRegistry Meter Registry
*/
void configure(T meterRegistry);

/**
* Method to determine if this configurer supports the meter registry type.
* Determines if this configurer supports the meter registry type.
*
* @param meterRegistry a meter registry
* @return boolean whether is supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,29 @@

import io.micrometer.core.instrument.MeterRegistry;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.util.StringUtils;

import java.lang.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static io.micronaut.configuration.metrics.micrometer.MeterRegistryFactory.MICRONAUT_METRICS_ENABLED;
import static io.micronaut.core.util.StringUtils.FALSE;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PACKAGE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* An annotation that can be applied to any bean that requires Metrics is enabled.
* Apply to a bean that requires metrics to be enabled.
*
* @author Graeme Rocher
* @since 1.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PACKAGE, ElementType.TYPE, ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Requires(property = MICRONAUT_METRICS_ENABLED, notEquals = StringUtils.FALSE)
@Retention(RUNTIME)
@Target({PACKAGE, TYPE, ANNOTATION_TYPE, METHOD})
@Requires(property = MICRONAUT_METRICS_ENABLED, notEquals = FALSE)
@Requires(beans = MeterRegistry.class)
public @interface RequiresMetrics {
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
/**
* Annotations specific to metrics configuration.
* Metrics configuration annotations.
*
* @author graemerocher
* @since 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.event.BeanCreatedEvent;
import io.micronaut.context.event.BeanCreatedEventListener;
import io.micronaut.core.util.StringUtils;
import jakarta.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.cache.CacheManager;

import static io.micronaut.configuration.metrics.micrometer.MeterRegistryFactory.MICRONAUT_METRICS_BINDERS;
import static io.micronaut.core.util.StringUtils.FALSE;

/**
* Instruments the active JCache manager.
Expand All @@ -40,10 +40,11 @@
@Singleton
@RequiresMetrics
@Requires(beans = CacheManager.class)
@Requires(property = MICRONAUT_METRICS_BINDERS + ".cache.enabled", notEquals = StringUtils.FALSE)
@Requires(property = MICRONAUT_METRICS_BINDERS + ".cache.enabled", notEquals = FALSE)
public class JCacheMetricsBinder implements BeanCreatedEventListener<CacheManager> {

private static final Logger LOG = LoggerFactory.getLogger(JCacheMetricsBinder.class);

private final BeanProvider<MeterRegistry> meterRegistryProvider;

/**
Expand All @@ -60,19 +61,12 @@ public CacheManager onCreated(BeanCreatedEvent<CacheManager> event) {
final MeterRegistry meterRegistry = meterRegistryProvider.get();
final CacheManager cacheManager = event.getBean();
for (String cacheName : cacheManager.getCacheNames()) {

try {
JCacheMetrics.monitor(
meterRegistry,
cacheManager.getCache(cacheName)
);
JCacheMetrics.monitor(meterRegistry, cacheManager.getCache(cacheName));
} catch (Exception e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Unable to instrument JCache CacheManager with metrics: " + e.getMessage(), e);
}
LOG.warn("Unable to instrument JCache CacheManager with metrics: {}", e.getMessage(), e);
}
}
return cacheManager;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
*/
package io.micronaut.configuration.metrics.binder.cache;

import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.FunctionCounter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.TimeGauge;
import io.micrometer.core.instrument.binder.cache.CacheMeterBinder;
import io.micrometer.core.lang.NonNullApi;
import io.micrometer.core.lang.NonNullFields;
Expand All @@ -37,7 +42,7 @@
*
* NOTE: This is a fork of https://github.com/micrometer-metrics/micrometer/blob/master/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/cache/CaffeineCacheMetrics.java
*
* The reason for the fork is because Micronaut repackages caffeine under a different package
* The fork is needed because Micronaut repackages caffeine under a different package.
*
* @author Clint Checketts
* @author graemerocher
Expand Down Expand Up @@ -180,4 +185,3 @@ protected void bindImplementationSpecificMetrics(MeterRegistry registry) {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.event.BeanCreatedEvent;
import io.micronaut.context.event.BeanCreatedEventListener;
import io.micronaut.core.util.StringUtils;
import jakarta.inject.Singleton;

import java.util.Collections;

import static io.micronaut.configuration.metrics.micrometer.MeterRegistryFactory.MICRONAUT_METRICS_BINDERS;
import static io.micronaut.core.util.StringUtils.FALSE;

/**
* A cache Metrics binder for Micronaut's re-packaged version of Caffeine.
Expand All @@ -36,7 +36,7 @@
* @since 1.0
* @deprecated Since cache was separated from core this is no longer necessary
*/
@Requires(property = MICRONAUT_METRICS_BINDERS + ".cache.enabled", notEquals = StringUtils.FALSE)
@Requires(property = MICRONAUT_METRICS_BINDERS + ".cache.enabled", notEquals = FALSE)
@Requires(classes = Cache.class)
@RequiresMetrics
@Singleton
Expand All @@ -62,12 +62,11 @@ public Cache<?> onCreated(BeanCreatedEvent<Cache<?>> event) {
if (nativeCache instanceof io.micronaut.caffeine.cache.Cache) {
MicronautCaffeineCacheMetrics.monitor(
meterRegistry,
(io.micronaut.caffeine.cache.Cache) nativeCache,
(io.micronaut.caffeine.cache.Cache<?, ?>) nativeCache,
cache.getName(),
Collections.emptyList()
);
}
return cache;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
/**
* Contains classes that binds metrics Micronaut's re-packaged version of Caffeine.
* Classes that bind metrics for Micronaut's repackaged version of Caffeine.
*
* @author graemerocher
* @since 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.jdbc.metadata.DataSourcePoolMetadata;

import javax.sql.DataSource;
import javax.validation.constraints.NotNull;
import java.util.function.Function;

/**
Expand All @@ -42,8 +42,6 @@ public class DataSourcePoolMetricsBinder implements MeterBinder {
private final Iterable<Tag> tags;

/**
* Constructor for creaging data source pool metrics.
*
* @param dataSource The datasource to bind metrics for
* @param metadataProvider A composite object of all the metadataProviders
* @param dataSourceName The name of the datasource
Expand All @@ -59,13 +57,13 @@ public class DataSourcePoolMetricsBinder implements MeterBinder {
}

/**
* Method for getting metadataProvider object for datasource that will bind the pool metrics.
* Binds the pool metrics.
*
* @param meterRegistry the meter registry object
* @param meterRegistry the meter registry
*/
@Override
public void bindTo(@NotNull MeterRegistry meterRegistry) {
if (this.metadataProvider != null) {
public void bindTo(@NonNull MeterRegistry meterRegistry) {
if (metadataProvider != null) {
bindPoolMetadata(meterRegistry, "active", DataSourcePoolMetadata::getActive);
bindPoolMetadata(meterRegistry, "max", DataSourcePoolMetadata::getMax);
bindPoolMetadata(meterRegistry, "min", DataSourcePoolMetadata::getMin);
Expand All @@ -76,14 +74,12 @@ public void bindTo(@NotNull MeterRegistry meterRegistry) {
private <N extends Number> void bindPoolMetadata(MeterRegistry registry,
String metricName,
Function<DataSourcePoolMetadata, N> function) {
bindDataSource(registry,
metricName,
this.getValueFunction(function));
bindDataSource(registry, metricName, getValueFunction(function));
}

private <N extends Number> Function<DataSource, N> getValueFunction(
Function<DataSourcePoolMetadata, N> function) {
return dataSource -> function.apply(this.metadataProvider);
return dataSource -> function.apply(metadataProvider);
}

/**
Expand All @@ -97,10 +93,10 @@ private <N extends Number> Function<DataSource, N> getValueFunction(
private <N extends Number> void bindDataSource(MeterRegistry meterRegistry,
String metricName,
Function<DataSource, N> function) {
if (function.apply(this.dataSource) != null) {
if (function.apply(dataSource) != null) {
meterRegistry.gauge("jdbc.connections." + metricName,
this.tags,
this.dataSource,
tags,
dataSource,
m -> function.apply(m).doubleValue());
}
}
Expand Down
Loading

0 comments on commit 98109ea

Please sign in to comment.