Skip to content

Commit a0f39d6

Browse files
committed
Polish 'Align Wavefront application tags support with Spring Boot 2.x'
See gh-32844
1 parent 42bb4c0 commit a0f39d6

File tree

4 files changed

+30
-45
lines changed

4 files changed

+30
-45
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/wavefront/WavefrontMetricsExportAutoConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ public WavefrontMeterRegistry wavefrontMeterRegistry(WavefrontConfig wavefrontCo
7777

7878
@Bean
7979
@ConditionalOnBean(ApplicationTags.class)
80-
MeterRegistryCustomizer<WavefrontMeterRegistry> applicationTagsCustomizer(ApplicationTags applicationTags) {
81-
Tags commonTags = Tags.of(applicationTags.toPointTags().entrySet().stream()
82-
.map(WavefrontMetricsExportAutoConfiguration::asTag).toList());
80+
MeterRegistryCustomizer<WavefrontMeterRegistry> wavefrontApplicationTagsCustomizer(
81+
ApplicationTags wavefrontApplicationTags) {
82+
Tags commonTags = Tags.of(wavefrontApplicationTags.toPointTags().entrySet().stream().map(this::asTag).toList());
8383
return (registry) -> registry.config().commonTags(commonTags);
8484
}
8585

86-
private static Tag asTag(Map.Entry<String, String> entry) {
86+
private Tag asTag(Map.Entry<String, String> entry) {
8787
return Tag.of(entry.getKey(), entry.getValue());
8888
}
8989

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/wavefront/WavefrontTracingAutoConfiguration.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.actuate.autoconfigure.tracing.wavefront;
1818

1919
import java.util.Collections;
20+
import java.util.function.Supplier;
2021

2122
import brave.handler.SpanHandler;
2223
import com.wavefront.sdk.common.WavefrontSender;
@@ -40,10 +41,12 @@
4041
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4142
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4243
import org.springframework.boot.context.properties.EnableConfigurationProperties;
44+
import org.springframework.boot.context.properties.PropertyMapper;
4345
import org.springframework.context.annotation.Bean;
4446
import org.springframework.context.annotation.Configuration;
4547
import org.springframework.context.annotation.Import;
4648
import org.springframework.core.env.Environment;
49+
import org.springframework.util.StringUtils;
4750

4851
/**
4952
* {@link EnableAutoConfiguration Auto-configuration} for Wavefront tracing.
@@ -65,7 +68,7 @@ public class WavefrontTracingAutoConfiguration {
6568
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
6669
* Application Tags</a>
6770
*/
68-
private static final String DEFAULT_WAVEFRONT_APPLICATION_NAME = "unnamed_application";
71+
private static final String DEFAULT_APPLICATION_NAME = "unnamed_application";
6972

7073
/**
7174
* Default value for the Wavefront Service name if {@code spring.application.name} is
@@ -74,28 +77,26 @@ public class WavefrontTracingAutoConfiguration {
7477
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
7578
* Application Tags</a>
7679
*/
77-
private static final String DEFAULT_WAVEFRONT_SERVICE_NAME = "unnamed_service";
80+
private static final String DEFAULT_SERVICE_NAME = "unnamed_service";
7881

7982
@Bean
8083
@ConditionalOnMissingBean
81-
public ApplicationTags applicationTags(Environment environment, WavefrontProperties properties) {
82-
String fallbackWavefrontServiceName = environment.getProperty("spring.application.name",
83-
DEFAULT_WAVEFRONT_SERVICE_NAME);
84+
public ApplicationTags wavefrontApplicationTags(Environment environment, WavefrontProperties properties) {
8485
Tracing tracing = properties.getTracing();
85-
String wavefrontServiceName = (tracing.getServiceName() != null) ? tracing.getServiceName()
86-
: fallbackWavefrontServiceName;
87-
String wavefrontApplicationName = (tracing.getApplicationName() != null) ? tracing.getApplicationName()
88-
: DEFAULT_WAVEFRONT_APPLICATION_NAME;
86+
String wavefrontServiceName = getName(tracing.getServiceName(),
87+
() -> environment.getProperty("spring.application.name", DEFAULT_SERVICE_NAME));
88+
String wavefrontApplicationName = getName(tracing.getApplicationName(), () -> DEFAULT_APPLICATION_NAME);
89+
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
8990
ApplicationTags.Builder builder = new ApplicationTags.Builder(wavefrontApplicationName, wavefrontServiceName);
90-
if (tracing.getClusterName() != null) {
91-
builder.cluster(tracing.getClusterName());
92-
}
93-
if (tracing.getShardName() != null) {
94-
builder.shard(tracing.getShardName());
95-
}
91+
map.from(tracing::getClusterName).to(builder::cluster);
92+
map.from(tracing::getShardName).to(builder::shard);
9693
return builder.build();
9794
}
9895

96+
private String getName(String value, Supplier<String> fallback) {
97+
return (StringUtils.hasText(value)) ? value : fallback.get();
98+
}
99+
99100
@Configuration(proxyBeanMethods = false)
100101
@ConditionalOnClass(WavefrontSpanHandler.class)
101102
static class WavefrontMicrometer {

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontProperties.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import java.net.UnknownHostException;
2222
import java.time.Duration;
2323

24-
import com.wavefront.sdk.common.application.ApplicationTags;
25-
2624
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PushRegistryProperties;
2725
import org.springframework.boot.context.properties.ConfigurationProperties;
2826
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
@@ -264,37 +262,24 @@ public void setBatchSize(Integer batchSize) {
264262
public static class Tracing {
265263

266264
/**
267-
* Wavefront Application name used in {@link ApplicationTags}. Defaults to
265+
* Wavefront Application name used in ApplicationTags. Defaults to
268266
* 'unnamed_application'.
269-
* @see <a href=
270-
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
271-
* Application Tags</a>
272267
*/
273268
private String applicationName;
274269

275270
/**
276-
* Wavefront Service name used in {@link ApplicationTags}, falling back to
277-
* {@code spring.application.name}. If both are unset it defaults to
278-
* 'unnamed_service'.
279-
* @see <a href=
280-
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
281-
* Application Tags</a>
271+
* Wavefront Service name used in ApplicationTags, falling back to
272+
* 'spring.application.name'. If both are unset it defaults to 'unnamed_service'.
282273
*/
283274
private String serviceName;
284275

285276
/**
286-
* Optional Wavefront Cluster name used in {@link ApplicationTags}.
287-
* @see <a href=
288-
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
289-
* Application Tags</a>
277+
* Optional Wavefront Cluster name used in ApplicationTags.
290278
*/
291279
private String clusterName;
292280

293281
/**
294-
* Optional Wavefront Shard name used in {@link ApplicationTags}.
295-
* @see <a href=
296-
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
297-
* Application Tags</a>
282+
* Optional Wavefront Shard name used in ApplicationTags.
298283
*/
299284
private String shardName;
300285

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/wavefront/WavefrontMetricsExportAutoConfigurationTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,12 @@ void allowsRegistryToBeCustomized() {
8888

8989
@Test
9090
void exportsApplicationTagsInWavefrontRegistry() {
91-
ApplicationTags.Builder appTagsBuilder = new ApplicationTags.Builder("super-application", "super-service");
92-
appTagsBuilder.cluster("super-cluster");
93-
appTagsBuilder.shard("super-shard");
94-
appTagsBuilder.customTags(Map.of("custom-key", "custom-val"));
95-
91+
ApplicationTags.Builder builder = new ApplicationTags.Builder("super-application", "super-service");
92+
builder.cluster("super-cluster");
93+
builder.shard("super-shard");
94+
builder.customTags(Map.of("custom-key", "custom-val"));
9695
this.contextRunner.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class))
97-
.withUserConfiguration(BaseConfiguration.class).withBean(ApplicationTags.class, appTagsBuilder::build)
96+
.withUserConfiguration(BaseConfiguration.class).withBean(ApplicationTags.class, builder::build)
9897
.run((context) -> {
9998
WavefrontMeterRegistry registry = context.getBean(WavefrontMeterRegistry.class);
10099
registry.counter("my.counter", "env", "qa");

0 commit comments

Comments
 (0)