Commit 643cda48 authored by Phillip Webb's avatar Phillip Webb

Migrate to updated micrometer Tags class

See gh-11575
parent fd237f85
...@@ -20,7 +20,6 @@ import java.util.Map; ...@@ -20,7 +20,6 @@ import java.util.Map;
import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.ConnectionFactory;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.Tags;
import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory; import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory;
...@@ -66,9 +65,11 @@ public class RabbitMetricsConfiguration { ...@@ -66,9 +65,11 @@ public class RabbitMetricsConfiguration {
private void bindConnectionFactoryToRegistry(String beanName, private void bindConnectionFactoryToRegistry(String beanName,
AbstractConnectionFactory connectionFactory) { AbstractConnectionFactory connectionFactory) {
Iterable<Tag> tags = Tags.zip("name", getConnectionFactoryName(beanName)); ConnectionFactory rabbitConnectionFactory = connectionFactory
new RabbitMetrics(connectionFactory.getRabbitConnectionFactory(), this.metricName, .getRabbitConnectionFactory();
tags).bindTo(this.registry); String connectionFactoryName = getConnectionFactoryName(beanName);
new RabbitMetrics(rabbitConnectionFactory, this.metricName,
Tags.of("name", connectionFactoryName)).bindTo(this.registry);
} }
/** /**
......
...@@ -22,7 +22,6 @@ import java.util.Map; ...@@ -22,7 +22,6 @@ import java.util.Map;
import javax.sql.DataSource; import javax.sql.DataSource;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.Tags;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -68,9 +67,9 @@ public class DataSourcePoolMetricsConfiguration { ...@@ -68,9 +67,9 @@ public class DataSourcePoolMetricsConfiguration {
} }
private void bindDataSourceToRegistry(String beanName, DataSource dataSource) { private void bindDataSourceToRegistry(String beanName, DataSource dataSource) {
Iterable<Tag> tags = Tags.zip("name", getDataSourceName(beanName)); String dataSourceName = getDataSourceName(beanName);
new DataSourcePoolMetrics(dataSource, this.metadataProviders, this.metricName, new DataSourcePoolMetrics(dataSource, this.metadataProviders, this.metricName,
tags).bindTo(this.registry); Tags.of("name", dataSourceName)).bindTo(this.registry);
} }
/** /**
......
...@@ -16,10 +16,7 @@ ...@@ -16,10 +16,7 @@
package org.springframework.boot.actuate.metrics.cache; package org.springframework.boot.actuate.metrics.cache;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tag;
...@@ -69,8 +66,7 @@ public class CacheMetricsRegistrar { ...@@ -69,8 +66,7 @@ public class CacheMetricsRegistrar {
* @return {@code true} if the {@code cache} is supported and was registered * @return {@code true} if the {@code cache} is supported and was registered
*/ */
public boolean bindCacheToRegistry(Cache cache, Tag... tags) { public boolean bindCacheToRegistry(Cache cache, Tag... tags) {
List<Tag> allTags = new ArrayList<>(Arrays.asList(tags)); MeterBinder meterBinder = getMeterBinder(cache, Tags.of(tags));
MeterBinder meterBinder = getMeterBinder(cache, allTags);
if (meterBinder != null) { if (meterBinder != null) {
meterBinder.bindTo(this.registry); meterBinder.bindTo(this.registry);
return true; return true;
...@@ -79,8 +75,8 @@ public class CacheMetricsRegistrar { ...@@ -79,8 +75,8 @@ public class CacheMetricsRegistrar {
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
private MeterBinder getMeterBinder(Cache cache, Iterable<Tag> tags) { private MeterBinder getMeterBinder(Cache cache, Tags tags) {
Iterable<Tag> withAdditionalTags = Tags.concat(tags, getAdditionalTags(cache)); tags = tags.and(getAdditionalTags(cache));
for (CacheMeterBinderProvider<?> binderProvider : this.binderProviders) { for (CacheMeterBinderProvider<?> binderProvider : this.binderProviders) {
Class<?> cacheType = ResolvableType Class<?> cacheType = ResolvableType
.forClass(CacheMeterBinderProvider.class, binderProvider.getClass()) .forClass(CacheMeterBinderProvider.class, binderProvider.getClass())
...@@ -88,7 +84,7 @@ public class CacheMetricsRegistrar { ...@@ -88,7 +84,7 @@ public class CacheMetricsRegistrar {
if (cacheType.isInstance(cache)) { if (cacheType.isInstance(cache)) {
try { try {
MeterBinder meterBinder = ((CacheMeterBinderProvider) binderProvider) MeterBinder meterBinder = ((CacheMeterBinderProvider) binderProvider)
.getMeterBinder(cache, this.metricName, withAdditionalTags); .getMeterBinder(cache, this.metricName, tags);
if (meterBinder != null) { if (meterBinder != null) {
return meterBinder; return meterBinder;
} }
...@@ -121,7 +117,7 @@ public class CacheMetricsRegistrar { ...@@ -121,7 +117,7 @@ public class CacheMetricsRegistrar {
* @return a list of additional tags to associate to that {@code cache}. * @return a list of additional tags to associate to that {@code cache}.
*/ */
protected Iterable<Tag> getAdditionalTags(Cache cache) { protected Iterable<Tag> getAdditionalTags(Cache cache) {
return Tags.zip("name", cache.getName()); return Tags.of("name", cache.getName());
} }
} }
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -45,7 +45,7 @@ import org.springframework.integration.support.management.PollableChannelManagem ...@@ -45,7 +45,7 @@ import org.springframework.integration.support.management.PollableChannelManagem
*/ */
public class SpringIntegrationMetrics implements MeterBinder, SmartInitializingSingleton { public class SpringIntegrationMetrics implements MeterBinder, SmartInitializingSingleton {
private final Iterable<Tag> tags; private final Tags tags;
private Collection<MeterRegistry> registries = new ArrayList<>(); private Collection<MeterRegistry> registries = new ArrayList<>();
...@@ -56,9 +56,9 @@ public class SpringIntegrationMetrics implements MeterBinder, SmartInitializingS ...@@ -56,9 +56,9 @@ public class SpringIntegrationMetrics implements MeterBinder, SmartInitializingS
} }
public SpringIntegrationMetrics(IntegrationManagementConfigurer configurer, public SpringIntegrationMetrics(IntegrationManagementConfigurer configurer,
Iterable<Tag> tags) { Iterable<? extends Tag> tags) {
this.configurer = configurer; this.configurer = configurer;
this.tags = tags; this.tags = Tags.of(tags);
} }
@Override @Override
...@@ -81,7 +81,7 @@ public class SpringIntegrationMetrics implements MeterBinder, SmartInitializingS ...@@ -81,7 +81,7 @@ public class SpringIntegrationMetrics implements MeterBinder, SmartInitializingS
private void addSourceMetrics(MeterRegistry registry) { private void addSourceMetrics(MeterRegistry registry) {
for (String source : this.configurer.getSourceNames()) { for (String source : this.configurer.getSourceNames()) {
MessageSourceMetrics sourceMetrics = this.configurer.getSourceMetrics(source); MessageSourceMetrics sourceMetrics = this.configurer.getSourceMetrics(source);
Iterable<Tag> tagsWithSource = Tags.concat(this.tags, "source", source); Iterable<Tag> tagsWithSource = this.tags.and("source", source);
registerFunctionCounter(registry, sourceMetrics, tagsWithSource, registerFunctionCounter(registry, sourceMetrics, tagsWithSource,
"spring.integration.source.messages", "spring.integration.source.messages",
"The number of successful handler calls", "The number of successful handler calls",
...@@ -93,7 +93,7 @@ public class SpringIntegrationMetrics implements MeterBinder, SmartInitializingS ...@@ -93,7 +93,7 @@ public class SpringIntegrationMetrics implements MeterBinder, SmartInitializingS
for (String handler : this.configurer.getHandlerNames()) { for (String handler : this.configurer.getHandlerNames()) {
MessageHandlerMetrics handlerMetrics = this.configurer MessageHandlerMetrics handlerMetrics = this.configurer
.getHandlerMetrics(handler); .getHandlerMetrics(handler);
Iterable<Tag> tagsWithHandler = Tags.concat(this.tags, "handler", handler); Iterable<Tag> tagsWithHandler = this.tags.and("handler", handler);
registerTimedGauge(registry, handlerMetrics, tagsWithHandler, registerTimedGauge(registry, handlerMetrics, tagsWithHandler,
"spring.integration.handler.duration.max", "spring.integration.handler.duration.max",
"The maximum handler duration", "The maximum handler duration",
...@@ -116,7 +116,7 @@ public class SpringIntegrationMetrics implements MeterBinder, SmartInitializingS ...@@ -116,7 +116,7 @@ public class SpringIntegrationMetrics implements MeterBinder, SmartInitializingS
for (String channel : this.configurer.getChannelNames()) { for (String channel : this.configurer.getChannelNames()) {
MessageChannelMetrics channelMetrics = this.configurer MessageChannelMetrics channelMetrics = this.configurer
.getChannelMetrics(channel); .getChannelMetrics(channel);
Iterable<Tag> tagsWithChannel = Tags.concat(this.tags, "channel", channel); Iterable<Tag> tagsWithChannel = this.tags.and("channel", channel);
registerFunctionCounter(registry, channelMetrics, tagsWithChannel, registerFunctionCounter(registry, channelMetrics, tagsWithChannel,
"spring.integration.channel.sendErrors", "spring.integration.channel.sendErrors",
"The number of failed sends (either throwing an exception or rejected by the channel)", "The number of failed sends (either throwing an exception or rejected by the channel)",
......
...@@ -16,17 +16,17 @@ ...@@ -16,17 +16,17 @@
package org.springframework.boot.actuate.metrics.web.reactive.server; package org.springframework.boot.actuate.metrics.web.reactive.server;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.Tags;
import reactor.core.publisher.Mono;
import org.springframework.util.Assert;
import org.springframework.web.reactive.function.server.HandlerFilterFunction; import org.springframework.web.reactive.function.server.HandlerFilterFunction;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
...@@ -41,53 +41,53 @@ public class RouterFunctionMetrics { ...@@ -41,53 +41,53 @@ public class RouterFunctionMetrics {
private final MeterRegistry registry; private final MeterRegistry registry;
private BiFunction<ServerRequest, ServerResponse, Collection<Tag>> defaultTags = ( private final BiFunction<ServerRequest, ServerResponse, Iterable<Tag>> defaultTags;
ServerRequest request, ServerResponse response) -> response != null
? Arrays.asList(method(request), status(response))
: Collections.singletonList(method(request));
public RouterFunctionMetrics(MeterRegistry registry) { public RouterFunctionMetrics(MeterRegistry registry) {
Assert.notNull(registry, "Registry must not be null");
this.registry = registry; this.registry = registry;
this.defaultTags = this::defaultTags;
}
private RouterFunctionMetrics(MeterRegistry registry,
BiFunction<ServerRequest, ServerResponse, Iterable<Tag>> defaultTags) {
Assert.notNull(registry, "Registry must not be null");
Assert.notNull(defaultTags, "DefaultTags must not be null");
this.registry = registry;
this.defaultTags = defaultTags;
}
private Iterable<Tag> defaultTags(ServerRequest request, ServerResponse response) {
if (response == null) {
return Tags.of(getMethodTag(request));
}
return Tags.of(getMethodTag(request), getStatusTag(response));
} }
/** /**
* Configures the default tags. * Returns a new {@link RouterFunctionMetrics} instance with the specified default
* tags.
* @param defaultTags Generate a list of tags to apply to the timer. * @param defaultTags Generate a list of tags to apply to the timer.
* {@code ServerResponse} may be null. * {@code ServerResponse} may be null.
* @return {@code this} for further configuration * @return {@code this} for further configuration
*/ */
public RouterFunctionMetrics defaultTags( public RouterFunctionMetrics defaultTags(
BiFunction<ServerRequest, ServerResponse, Collection<Tag>> defaultTags) { BiFunction<ServerRequest, ServerResponse, Iterable<Tag>> defaultTags) {
this.defaultTags = defaultTags; return new RouterFunctionMetrics(this.registry, defaultTags);
return this;
} }
public HandlerFilterFunction<ServerResponse, ServerResponse> timer(String name) { public HandlerFilterFunction<ServerResponse, ServerResponse> timer(String name) {
return timer(name, Collections.emptyList()); return timer(name, Tags.empty());
} }
public HandlerFilterFunction<ServerResponse, ServerResponse> timer(String name, public HandlerFilterFunction<ServerResponse, ServerResponse> timer(String name,
String... tags) { String... tags) {
return timer(name, Tags.zip(tags)); return timer(name, Tags.of(tags));
} }
public HandlerFilterFunction<ServerResponse, ServerResponse> timer(String name, public HandlerFilterFunction<ServerResponse, ServerResponse> timer(String name,
Iterable<Tag> tags) { Iterable<Tag> tags) {
return (request, next) -> { return new MetricsFilter(name, Tags.of(tags));
final long start = System.nanoTime();
return next.handle(request).doOnSuccess((response) -> {
Iterable<Tag> allTags = Tags.concat(tags,
this.defaultTags.apply(request, response));
this.registry.timer(name, allTags).record(System.nanoTime() - start,
TimeUnit.NANOSECONDS);
}).doOnError((error) -> {
// FIXME how do we get the response under an error condition?
Iterable<Tag> allTags = Tags.concat(tags,
this.defaultTags.apply(request, null));
this.registry.timer(name, allTags).record(System.nanoTime() - start,
TimeUnit.NANOSECONDS);
});
};
} }
/** /**
...@@ -95,7 +95,7 @@ public class RouterFunctionMetrics { ...@@ -95,7 +95,7 @@ public class RouterFunctionMetrics {
* @param request The HTTP request. * @param request The HTTP request.
* @return A "method" tag whose value is a capitalized method (e.g. GET). * @return A "method" tag whose value is a capitalized method (e.g. GET).
*/ */
public static Tag method(ServerRequest request) { public static Tag getMethodTag(ServerRequest request) {
return Tag.of("method", request.method().toString()); return Tag.of("method", request.method().toString());
} }
...@@ -104,8 +104,48 @@ public class RouterFunctionMetrics { ...@@ -104,8 +104,48 @@ public class RouterFunctionMetrics {
* @param response The HTTP response. * @param response The HTTP response.
* @return A "status" tag whose value is the numeric status code. * @return A "status" tag whose value is the numeric status code.
*/ */
public static Tag status(ServerResponse response) { public static Tag getStatusTag(ServerResponse response) {
return Tag.of("status", response.statusCode().toString()); return Tag.of("status", response.statusCode().toString());
} }
/**
* {@link HandlerFilterFunction} to handle calling micrometer.
*/
private class MetricsFilter
implements HandlerFilterFunction<ServerResponse, ServerResponse> {
private final String name;
private final Tags tags;
MetricsFilter(String name, Tags tags) {
this.name = name;
this.tags = tags;
}
@Override
public Mono<ServerResponse> filter(ServerRequest request,
HandlerFunction<ServerResponse> next) {
long start = System.nanoTime();
return next.handle(request).doOnSuccess((response) -> {
timer(start, request, response);
}).doOnError((error) -> {
// FIXME how do we get the response under an error condition?
timer(start, request, null);
});
}
private Iterable<Tag> getDefaultTags(ServerRequest request,
ServerResponse response) {
return RouterFunctionMetrics.this.defaultTags.apply(request, response);
}
private void timer(long start, ServerRequest request, ServerResponse response) {
Tags allTags = this.tags.and(getDefaultTags(request, response));
RouterFunctionMetrics.this.registry.timer(this.name, allTags)
.record(System.nanoTime() - start, TimeUnit.NANOSECONDS);
}
}
} }
...@@ -34,7 +34,6 @@ import io.micrometer.core.annotation.Timed; ...@@ -34,7 +34,6 @@ import io.micrometer.core.annotation.Timed;
import io.micrometer.core.annotation.TimedSet; import io.micrometer.core.annotation.TimedSet;
import io.micrometer.core.instrument.LongTaskTimer; import io.micrometer.core.instrument.LongTaskTimer;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer; import io.micrometer.core.instrument.Timer;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
...@@ -252,7 +251,7 @@ public class WebMvcMetrics { ...@@ -252,7 +251,7 @@ public class WebMvcMetrics {
private final String name; private final String name;
private final Iterable<Tag> extraTags; private final Tags extraTags;
private final double[] percentiles; private final double[] percentiles;
...@@ -260,14 +259,14 @@ public class WebMvcMetrics { ...@@ -260,14 +259,14 @@ public class WebMvcMetrics {
TimerConfig(String name, boolean histogram) { TimerConfig(String name, boolean histogram) {
this.name = name; this.name = name;
this.extraTags = Collections.emptyList(); this.extraTags = Tags.empty();
this.percentiles = new double[0]; this.percentiles = new double[0];
this.histogram = histogram; this.histogram = histogram;
} }
TimerConfig(Timed timed, Supplier<String> name) { TimerConfig(Timed timed, Supplier<String> name) {
this.name = buildName(timed, name); this.name = buildName(timed, name);
this.extraTags = Tags.zip(timed.extraTags()); this.extraTags = Tags.of(timed.extraTags());
this.percentiles = timed.percentiles(); this.percentiles = timed.percentiles();
this.histogram = timed.histogram(); this.histogram = timed.histogram();
} }
...@@ -285,7 +284,7 @@ public class WebMvcMetrics { ...@@ -285,7 +284,7 @@ public class WebMvcMetrics {
return this.name; return this.name;
} }
Iterable<Tag> getExtraTags() { Tags getExtraTags() {
return this.extraTags; return this.extraTags;
} }
......
...@@ -43,7 +43,7 @@ public class RabbitMetricsTests { ...@@ -43,7 +43,7 @@ public class RabbitMetricsTests {
public void connectionFactoryWithTagsIsInstrumented() { public void connectionFactoryWithTagsIsInstrumented() {
ConnectionFactory connectionFactory = mockConnectionFactory(); ConnectionFactory connectionFactory = mockConnectionFactory();
SimpleMeterRegistry registry = new SimpleMeterRegistry(); SimpleMeterRegistry registry = new SimpleMeterRegistry();
new RabbitMetrics(connectionFactory, "test", Tags.zip("env", "prod")) new RabbitMetrics(connectionFactory, "test", Tags.of("env", "prod"))
.bindTo(registry); .bindTo(registry);
assertThat(registry.get("test.connections").tags("env", "prod").meter()) assertThat(registry.get("test.connections").tags("env", "prod").meter())
.isNotNull(); .isNotNull();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment