Skip to content

Commit 643cda4

Browse files
committed
Migrate to updated micrometer Tags class
See gh-11575
1 parent fd237f8 commit 643cda4

File tree

7 files changed

+94
-59
lines changed

7 files changed

+94
-59
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/RabbitMetricsConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.rabbitmq.client.ConnectionFactory;
2222
import io.micrometer.core.instrument.MeterRegistry;
23-
import io.micrometer.core.instrument.Tag;
2423
import io.micrometer.core.instrument.Tags;
2524

2625
import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory;
@@ -66,9 +65,11 @@ public void bindConnectionFactoriesToRegistry(
6665

6766
private void bindConnectionFactoryToRegistry(String beanName,
6867
AbstractConnectionFactory connectionFactory) {
69-
Iterable<Tag> tags = Tags.zip("name", getConnectionFactoryName(beanName));
70-
new RabbitMetrics(connectionFactory.getRabbitConnectionFactory(), this.metricName,
71-
tags).bindTo(this.registry);
68+
ConnectionFactory rabbitConnectionFactory = connectionFactory
69+
.getRabbitConnectionFactory();
70+
String connectionFactoryName = getConnectionFactoryName(beanName);
71+
new RabbitMetrics(rabbitConnectionFactory, this.metricName,
72+
Tags.of("name", connectionFactoryName)).bindTo(this.registry);
7273
}
7374

7475
/**

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/jdbc/DataSourcePoolMetricsConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import javax.sql.DataSource;
2323

2424
import io.micrometer.core.instrument.MeterRegistry;
25-
import io.micrometer.core.instrument.Tag;
2625
import io.micrometer.core.instrument.Tags;
2726

2827
import org.springframework.beans.factory.annotation.Autowired;
@@ -68,9 +67,9 @@ public void bindDataSourcesToRegistry(Map<String, DataSource> dataSources) {
6867
}
6968

7069
private void bindDataSourceToRegistry(String beanName, DataSource dataSource) {
71-
Iterable<Tag> tags = Tags.zip("name", getDataSourceName(beanName));
70+
String dataSourceName = getDataSourceName(beanName);
7271
new DataSourcePoolMetrics(dataSource, this.metadataProviders, this.metricName,
73-
tags).bindTo(this.registry);
72+
Tags.of("name", dataSourceName)).bindTo(this.registry);
7473
}
7574

7675
/**

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
package org.springframework.boot.actuate.metrics.cache;
1818

19-
import java.util.ArrayList;
20-
import java.util.Arrays;
2119
import java.util.Collection;
22-
import java.util.List;
2320

2421
import io.micrometer.core.instrument.MeterRegistry;
2522
import io.micrometer.core.instrument.Tag;
@@ -69,8 +66,7 @@ public CacheMetricsRegistrar(MeterRegistry registry, String metricName,
6966
* @return {@code true} if the {@code cache} is supported and was registered
7067
*/
7168
public boolean bindCacheToRegistry(Cache cache, Tag... tags) {
72-
List<Tag> allTags = new ArrayList<>(Arrays.asList(tags));
73-
MeterBinder meterBinder = getMeterBinder(cache, allTags);
69+
MeterBinder meterBinder = getMeterBinder(cache, Tags.of(tags));
7470
if (meterBinder != null) {
7571
meterBinder.bindTo(this.registry);
7672
return true;
@@ -79,16 +75,16 @@ public boolean bindCacheToRegistry(Cache cache, Tag... tags) {
7975
}
8076

8177
@SuppressWarnings({ "unchecked", "rawtypes" })
82-
private MeterBinder getMeterBinder(Cache cache, Iterable<Tag> tags) {
83-
Iterable<Tag> withAdditionalTags = Tags.concat(tags, getAdditionalTags(cache));
78+
private MeterBinder getMeterBinder(Cache cache, Tags tags) {
79+
tags = tags.and(getAdditionalTags(cache));
8480
for (CacheMeterBinderProvider<?> binderProvider : this.binderProviders) {
8581
Class<?> cacheType = ResolvableType
8682
.forClass(CacheMeterBinderProvider.class, binderProvider.getClass())
8783
.resolveGeneric();
8884
if (cacheType.isInstance(cache)) {
8985
try {
9086
MeterBinder meterBinder = ((CacheMeterBinderProvider) binderProvider)
91-
.getMeterBinder(cache, this.metricName, withAdditionalTags);
87+
.getMeterBinder(cache, this.metricName, tags);
9288
if (meterBinder != null) {
9389
return meterBinder;
9490
}
@@ -121,7 +117,7 @@ private MeterBinder getMeterBinder(Cache cache, Iterable<Tag> tags) {
121117
* @return a list of additional tags to associate to that {@code cache}.
122118
*/
123119
protected Iterable<Tag> getAdditionalTags(Cache cache) {
124-
return Tags.zip("name", cache.getName());
120+
return Tags.of("name", cache.getName());
125121
}
126122

127123
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/integration/SpringIntegrationMetrics.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,7 +45,7 @@
4545
*/
4646
public class SpringIntegrationMetrics implements MeterBinder, SmartInitializingSingleton {
4747

48-
private final Iterable<Tag> tags;
48+
private final Tags tags;
4949

5050
private Collection<MeterRegistry> registries = new ArrayList<>();
5151

@@ -56,9 +56,9 @@ public SpringIntegrationMetrics(IntegrationManagementConfigurer configurer) {
5656
}
5757

5858
public SpringIntegrationMetrics(IntegrationManagementConfigurer configurer,
59-
Iterable<Tag> tags) {
59+
Iterable<? extends Tag> tags) {
6060
this.configurer = configurer;
61-
this.tags = tags;
61+
this.tags = Tags.of(tags);
6262
}
6363

6464
@Override
@@ -81,7 +81,7 @@ public void bindTo(MeterRegistry registry) {
8181
private void addSourceMetrics(MeterRegistry registry) {
8282
for (String source : this.configurer.getSourceNames()) {
8383
MessageSourceMetrics sourceMetrics = this.configurer.getSourceMetrics(source);
84-
Iterable<Tag> tagsWithSource = Tags.concat(this.tags, "source", source);
84+
Iterable<Tag> tagsWithSource = this.tags.and("source", source);
8585
registerFunctionCounter(registry, sourceMetrics, tagsWithSource,
8686
"spring.integration.source.messages",
8787
"The number of successful handler calls",
@@ -93,7 +93,7 @@ private void addHandlerMetrics(MeterRegistry registry) {
9393
for (String handler : this.configurer.getHandlerNames()) {
9494
MessageHandlerMetrics handlerMetrics = this.configurer
9595
.getHandlerMetrics(handler);
96-
Iterable<Tag> tagsWithHandler = Tags.concat(this.tags, "handler", handler);
96+
Iterable<Tag> tagsWithHandler = this.tags.and("handler", handler);
9797
registerTimedGauge(registry, handlerMetrics, tagsWithHandler,
9898
"spring.integration.handler.duration.max",
9999
"The maximum handler duration",
@@ -116,7 +116,7 @@ private void addChannelMetrics(MeterRegistry registry) {
116116
for (String channel : this.configurer.getChannelNames()) {
117117
MessageChannelMetrics channelMetrics = this.configurer
118118
.getChannelMetrics(channel);
119-
Iterable<Tag> tagsWithChannel = Tags.concat(this.tags, "channel", channel);
119+
Iterable<Tag> tagsWithChannel = this.tags.and("channel", channel);
120120
registerFunctionCounter(registry, channelMetrics, tagsWithChannel,
121121
"spring.integration.channel.sendErrors",
122122
"The number of failed sends (either throwing an exception or rejected by the channel)",

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/server/RouterFunctionMetrics.java

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616

1717
package org.springframework.boot.actuate.metrics.web.reactive.server;
1818

19-
import java.util.Arrays;
20-
import java.util.Collection;
21-
import java.util.Collections;
2219
import java.util.concurrent.TimeUnit;
2320
import java.util.function.BiFunction;
2421

2522
import io.micrometer.core.instrument.MeterRegistry;
2623
import io.micrometer.core.instrument.Tag;
2724
import io.micrometer.core.instrument.Tags;
25+
import reactor.core.publisher.Mono;
2826

27+
import org.springframework.util.Assert;
2928
import org.springframework.web.reactive.function.server.HandlerFilterFunction;
29+
import org.springframework.web.reactive.function.server.HandlerFunction;
3030
import org.springframework.web.reactive.function.server.RouterFunction;
3131
import org.springframework.web.reactive.function.server.ServerRequest;
3232
import org.springframework.web.reactive.function.server.ServerResponse;
@@ -41,61 +41,61 @@ public class RouterFunctionMetrics {
4141

4242
private final MeterRegistry registry;
4343

44-
private BiFunction<ServerRequest, ServerResponse, Collection<Tag>> defaultTags = (
45-
ServerRequest request, ServerResponse response) -> response != null
46-
? Arrays.asList(method(request), status(response))
47-
: Collections.singletonList(method(request));
44+
private final BiFunction<ServerRequest, ServerResponse, Iterable<Tag>> defaultTags;
4845

4946
public RouterFunctionMetrics(MeterRegistry registry) {
47+
Assert.notNull(registry, "Registry must not be null");
5048
this.registry = registry;
49+
this.defaultTags = this::defaultTags;
50+
}
51+
52+
private RouterFunctionMetrics(MeterRegistry registry,
53+
BiFunction<ServerRequest, ServerResponse, Iterable<Tag>> defaultTags) {
54+
Assert.notNull(registry, "Registry must not be null");
55+
Assert.notNull(defaultTags, "DefaultTags must not be null");
56+
this.registry = registry;
57+
this.defaultTags = defaultTags;
58+
}
59+
60+
private Iterable<Tag> defaultTags(ServerRequest request, ServerResponse response) {
61+
if (response == null) {
62+
return Tags.of(getMethodTag(request));
63+
}
64+
return Tags.of(getMethodTag(request), getStatusTag(response));
5165
}
5266

5367
/**
54-
* Configures the default tags.
68+
* Returns a new {@link RouterFunctionMetrics} instance with the specified default
69+
* tags.
5570
* @param defaultTags Generate a list of tags to apply to the timer.
5671
* {@code ServerResponse} may be null.
5772
* @return {@code this} for further configuration
5873
*/
5974
public RouterFunctionMetrics defaultTags(
60-
BiFunction<ServerRequest, ServerResponse, Collection<Tag>> defaultTags) {
61-
this.defaultTags = defaultTags;
62-
return this;
75+
BiFunction<ServerRequest, ServerResponse, Iterable<Tag>> defaultTags) {
76+
return new RouterFunctionMetrics(this.registry, defaultTags);
6377
}
6478

6579
public HandlerFilterFunction<ServerResponse, ServerResponse> timer(String name) {
66-
return timer(name, Collections.emptyList());
80+
return timer(name, Tags.empty());
6781
}
6882

6983
public HandlerFilterFunction<ServerResponse, ServerResponse> timer(String name,
7084
String... tags) {
71-
return timer(name, Tags.zip(tags));
85+
return timer(name, Tags.of(tags));
7286
}
7387

7488
public HandlerFilterFunction<ServerResponse, ServerResponse> timer(String name,
7589
Iterable<Tag> tags) {
76-
return (request, next) -> {
77-
final long start = System.nanoTime();
78-
return next.handle(request).doOnSuccess((response) -> {
79-
Iterable<Tag> allTags = Tags.concat(tags,
80-
this.defaultTags.apply(request, response));
81-
this.registry.timer(name, allTags).record(System.nanoTime() - start,
82-
TimeUnit.NANOSECONDS);
83-
}).doOnError((error) -> {
84-
// FIXME how do we get the response under an error condition?
85-
Iterable<Tag> allTags = Tags.concat(tags,
86-
this.defaultTags.apply(request, null));
87-
this.registry.timer(name, allTags).record(System.nanoTime() - start,
88-
TimeUnit.NANOSECONDS);
89-
});
90-
};
90+
return new MetricsFilter(name, Tags.of(tags));
9191
}
9292

9393
/**
9494
* Creates a {@code method} tag from the method of the given {@code request}.
9595
* @param request The HTTP request.
9696
* @return A "method" tag whose value is a capitalized method (e.g. GET).
9797
*/
98-
public static Tag method(ServerRequest request) {
98+
public static Tag getMethodTag(ServerRequest request) {
9999
return Tag.of("method", request.method().toString());
100100
}
101101

@@ -104,8 +104,48 @@ public static Tag method(ServerRequest request) {
104104
* @param response The HTTP response.
105105
* @return A "status" tag whose value is the numeric status code.
106106
*/
107-
public static Tag status(ServerResponse response) {
107+
public static Tag getStatusTag(ServerResponse response) {
108108
return Tag.of("status", response.statusCode().toString());
109109
}
110110

111+
/**
112+
* {@link HandlerFilterFunction} to handle calling micrometer.
113+
*/
114+
private class MetricsFilter
115+
implements HandlerFilterFunction<ServerResponse, ServerResponse> {
116+
117+
private final String name;
118+
119+
private final Tags tags;
120+
121+
MetricsFilter(String name, Tags tags) {
122+
this.name = name;
123+
this.tags = tags;
124+
}
125+
126+
@Override
127+
public Mono<ServerResponse> filter(ServerRequest request,
128+
HandlerFunction<ServerResponse> next) {
129+
long start = System.nanoTime();
130+
return next.handle(request).doOnSuccess((response) -> {
131+
timer(start, request, response);
132+
}).doOnError((error) -> {
133+
// FIXME how do we get the response under an error condition?
134+
timer(start, request, null);
135+
});
136+
}
137+
138+
private Iterable<Tag> getDefaultTags(ServerRequest request,
139+
ServerResponse response) {
140+
return RouterFunctionMetrics.this.defaultTags.apply(request, response);
141+
}
142+
143+
private void timer(long start, ServerRequest request, ServerResponse response) {
144+
Tags allTags = this.tags.and(getDefaultTags(request, response));
145+
RouterFunctionMetrics.this.registry.timer(this.name, allTags)
146+
.record(System.nanoTime() - start, TimeUnit.NANOSECONDS);
147+
}
148+
149+
}
150+
111151
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetrics.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import io.micrometer.core.annotation.TimedSet;
3535
import io.micrometer.core.instrument.LongTaskTimer;
3636
import io.micrometer.core.instrument.MeterRegistry;
37-
import io.micrometer.core.instrument.Tag;
3837
import io.micrometer.core.instrument.Tags;
3938
import io.micrometer.core.instrument.Timer;
4039
import org.apache.commons.logging.Log;
@@ -252,22 +251,22 @@ private static class TimerConfig {
252251

253252
private final String name;
254253

255-
private final Iterable<Tag> extraTags;
254+
private final Tags extraTags;
256255

257256
private final double[] percentiles;
258257

259258
private final boolean histogram;
260259

261260
TimerConfig(String name, boolean histogram) {
262261
this.name = name;
263-
this.extraTags = Collections.emptyList();
262+
this.extraTags = Tags.empty();
264263
this.percentiles = new double[0];
265264
this.histogram = histogram;
266265
}
267266

268267
TimerConfig(Timed timed, Supplier<String> name) {
269268
this.name = buildName(timed, name);
270-
this.extraTags = Tags.zip(timed.extraTags());
269+
this.extraTags = Tags.of(timed.extraTags());
271270
this.percentiles = timed.percentiles();
272271
this.histogram = timed.histogram();
273272
}
@@ -285,7 +284,7 @@ public String getName() {
285284
return this.name;
286285
}
287286

288-
Iterable<Tag> getExtraTags() {
287+
Tags getExtraTags() {
289288
return this.extraTags;
290289
}
291290

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/amqp/RabbitMetricsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void connectionFactoryIsInstrumented() {
4343
public void connectionFactoryWithTagsIsInstrumented() {
4444
ConnectionFactory connectionFactory = mockConnectionFactory();
4545
SimpleMeterRegistry registry = new SimpleMeterRegistry();
46-
new RabbitMetrics(connectionFactory, "test", Tags.zip("env", "prod"))
46+
new RabbitMetrics(connectionFactory, "test", Tags.of("env", "prod"))
4747
.bindTo(registry);
4848
assertThat(registry.get("test.connections").tags("env", "prod").meter())
4949
.isNotNull();

0 commit comments

Comments
 (0)