Skip to content

Commit 42bb4c0

Browse files
oppegardphilwebb
authored andcommitted
Align Wavefront application tags support with Spring Boot 2.x
Update `application-name` and `service-name` `management.wavefront` properties to align with Spring Boot 2.x support. The properties now use the wavefront terminology where `application` refers to an a collection of (micro)`services`. This commit also adds `cluster-name` and `shard-name` properties that can be used to add additional items to the `ApplicationTags`. See gh-32844
1 parent a50d1f0 commit 42bb4c0

File tree

5 files changed

+140
-13
lines changed

5 files changed

+140
-13
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront;
1818

19+
import java.util.Map;
20+
1921
import com.wavefront.sdk.common.WavefrontSender;
22+
import com.wavefront.sdk.common.application.ApplicationTags;
2023
import io.micrometer.core.instrument.Clock;
24+
import io.micrometer.core.instrument.Tag;
25+
import io.micrometer.core.instrument.Tags;
2126
import io.micrometer.wavefront.WavefrontConfig;
2227
import io.micrometer.wavefront.WavefrontMeterRegistry;
2328

2429
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
30+
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
2531
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
2632
import org.springframework.boot.actuate.autoconfigure.metrics.export.ConditionalOnEnabledMetricsExport;
2733
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
@@ -42,6 +48,7 @@
4248
* @author Jon Schneider
4349
* @author Artsiom Yudovin
4450
* @author Stephane Nicoll
51+
* @author Glenn Oppegard
4552
* @since 2.0.0
4653
*/
4754
@AutoConfiguration(
@@ -68,4 +75,16 @@ public WavefrontMeterRegistry wavefrontMeterRegistry(WavefrontConfig wavefrontCo
6875
return WavefrontMeterRegistry.builder(wavefrontConfig).clock(clock).wavefrontSender(wavefrontSender).build();
6976
}
7077

78+
@Bean
79+
@ConditionalOnBean(ApplicationTags.class)
80+
MeterRegistryCustomizer<WavefrontMeterRegistry> applicationTagsCustomizer(ApplicationTags applicationTags) {
81+
Tags commonTags = Tags.of(applicationTags.toPointTags().entrySet().stream()
82+
.map(WavefrontMetricsExportAutoConfiguration::asTag).toList());
83+
return (registry) -> registry.config().commonTags(commonTags);
84+
}
85+
86+
private static Tag asTag(Map.Entry<String, String> entry) {
87+
return Tag.of(entry.getKey(), entry.getValue());
88+
}
89+
7190
}

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* {@link EnableAutoConfiguration Auto-configuration} for Wavefront tracing.
5050
*
5151
* @author Moritz Halbritter
52+
* @author Glenn Oppegard
5253
* @since 3.0.0
5354
*/
5455
@AutoConfiguration(after = { MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class })
@@ -59,19 +60,40 @@
5960
public class WavefrontTracingAutoConfiguration {
6061

6162
/**
62-
* Default value for application name if {@code spring.application.name} is not set.
63+
* Default value for the Wavefront Application name.
64+
* @see <a href=
65+
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
66+
* Application Tags</a>
6367
*/
64-
private static final String DEFAULT_APPLICATION_NAME = "application";
68+
private static final String DEFAULT_WAVEFRONT_APPLICATION_NAME = "unnamed_application";
69+
70+
/**
71+
* Default value for the Wavefront Service name if {@code spring.application.name} is
72+
* not set.
73+
* @see <a href=
74+
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
75+
* Application Tags</a>
76+
*/
77+
private static final String DEFAULT_WAVEFRONT_SERVICE_NAME = "unnamed_service";
6578

6679
@Bean
6780
@ConditionalOnMissingBean
6881
public ApplicationTags applicationTags(Environment environment, WavefrontProperties properties) {
69-
String springApplicationName = environment.getProperty("spring.application.name", DEFAULT_APPLICATION_NAME);
82+
String fallbackWavefrontServiceName = environment.getProperty("spring.application.name",
83+
DEFAULT_WAVEFRONT_SERVICE_NAME);
7084
Tracing tracing = properties.getTracing();
71-
String applicationName = (tracing.getApplicationName() != null) ? tracing.getApplicationName()
72-
: springApplicationName;
73-
String serviceName = (tracing.getServiceName() != null) ? tracing.getServiceName() : springApplicationName;
74-
return new ApplicationTags.Builder(applicationName, serviceName).build();
85+
String wavefrontServiceName = (tracing.getServiceName() != null) ? tracing.getServiceName()
86+
: fallbackWavefrontServiceName;
87+
String wavefrontApplicationName = (tracing.getApplicationName() != null) ? tracing.getApplicationName()
88+
: DEFAULT_WAVEFRONT_APPLICATION_NAME;
89+
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+
}
96+
return builder.build();
7597
}
7698

7799
@Configuration(proxyBeanMethods = false)

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

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

24+
import com.wavefront.sdk.common.application.ApplicationTags;
25+
2426
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PushRegistryProperties;
2527
import org.springframework.boot.context.properties.ConfigurationProperties;
2628
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
@@ -30,6 +32,7 @@
3032
* Configuration properties to configure Wavefront.
3133
*
3234
* @author Moritz Halbritter
35+
* @author Glenn Oppegard
3336
* @since 3.0.0
3437
*/
3538
@ConfigurationProperties(prefix = "management.wavefront")
@@ -261,15 +264,40 @@ public void setBatchSize(Integer batchSize) {
261264
public static class Tracing {
262265

263266
/**
264-
* Application name. Defaults to 'spring.application.name'.
267+
* Wavefront Application name used in {@link ApplicationTags}. Defaults to
268+
* 'unnamed_application'.
269+
* @see <a href=
270+
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
271+
* Application Tags</a>
265272
*/
266273
private String applicationName;
267274

268275
/**
269-
* Service name. Defaults to 'spring.application.name'.
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>
270282
*/
271283
private String serviceName;
272284

285+
/**
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>
290+
*/
291+
private String clusterName;
292+
293+
/**
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>
298+
*/
299+
private String shardName;
300+
273301
public String getServiceName() {
274302
return this.serviceName;
275303
}
@@ -286,6 +314,22 @@ public void setApplicationName(String applicationName) {
286314
this.applicationName = applicationName;
287315
}
288316

317+
public String getClusterName() {
318+
return this.clusterName;
319+
}
320+
321+
public void setClusterName(String clusterName) {
322+
this.clusterName = clusterName;
323+
}
324+
325+
public String getShardName() {
326+
return this.shardName;
327+
}
328+
329+
public void setShardName(String shardName) {
330+
this.shardName = shardName;
331+
}
332+
289333
}
290334

291335
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront;
1818

19+
import java.util.Map;
20+
1921
import com.wavefront.sdk.common.WavefrontSender;
22+
import com.wavefront.sdk.common.application.ApplicationTags;
2023
import io.micrometer.core.instrument.Clock;
2124
import io.micrometer.wavefront.WavefrontConfig;
2225
import io.micrometer.wavefront.WavefrontMeterRegistry;
2326
import org.junit.jupiter.api.Test;
2427

28+
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
2529
import org.springframework.boot.autoconfigure.AutoConfigurations;
2630
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2731
import org.springframework.context.annotation.Bean;
@@ -36,6 +40,7 @@
3640
*
3741
* @author Jon Schneider
3842
* @author Stephane Nicoll
43+
* @author Glenn Oppegard
3944
*/
4045
class WavefrontMetricsExportAutoConfigurationTests {
4146

@@ -81,6 +86,24 @@ void allowsRegistryToBeCustomized() {
8186
.hasSingleBean(WavefrontMeterRegistry.class).hasBean("customRegistry"));
8287
}
8388

89+
@Test
90+
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+
96+
this.contextRunner.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class))
97+
.withUserConfiguration(BaseConfiguration.class).withBean(ApplicationTags.class, appTagsBuilder::build)
98+
.run((context) -> {
99+
WavefrontMeterRegistry registry = context.getBean(WavefrontMeterRegistry.class);
100+
registry.counter("my.counter", "env", "qa");
101+
assertThat(registry.find("my.counter").tags("env", "qa").tags("application", "super-application")
102+
.tags("service", "super-service").tags("cluster", "super-cluster")
103+
.tags("shard", "super-shard").tags("custom-key", "custom-val").counter()).isNotNull();
104+
});
105+
}
106+
84107
@Test
85108
void stopsMeterRegistryWhenContextIsClosed() {
86109
this.contextRunner.withUserConfiguration(BaseConfiguration.class)

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/wavefront/WavefrontTracingAutoConfigurationTests.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* Tests for {@link WavefrontTracingAutoConfiguration}.
4040
*
4141
* @author Moritz Halbritter
42+
* @author Glenn Oppegard
4243
*/
4344
class WavefrontTracingAutoConfigurationTests {
4445

@@ -114,22 +115,40 @@ void shouldNotSupplyWavefrontOtelSpanExporterIfOtelIsMissing() {
114115
}
115116

116117
@Test
117-
void shouldHaveADefaultApplicationName() {
118+
void shouldHaveADefaultApplicationNameAndServiceName() {
118119
this.contextRunner.withUserConfiguration(WavefrontSenderConfiguration.class).run((context) -> {
119120
ApplicationTags applicationTags = context.getBean(ApplicationTags.class);
120-
assertThat(applicationTags.getApplication()).isEqualTo("application");
121+
assertThat(applicationTags.getApplication()).isEqualTo("unnamed_application");
122+
assertThat(applicationTags.getService()).isEqualTo("unnamed_service");
123+
assertThat(applicationTags.getCluster()).isNull();
124+
assertThat(applicationTags.getShard()).isNull();
121125
});
122126
}
123127

128+
@Test
129+
void shouldUseSpringApplicationNameForServiceName() {
130+
this.contextRunner.withUserConfiguration(WavefrontSenderConfiguration.class)
131+
.withPropertyValues("spring.application.name=super-service").run((context) -> {
132+
ApplicationTags applicationTags = context.getBean(ApplicationTags.class);
133+
assertThat(applicationTags.getApplication()).isEqualTo("unnamed_application");
134+
assertThat(applicationTags.getService()).isEqualTo("super-service");
135+
});
136+
}
137+
124138
@Test
125139
void shouldHonorConfigProperties() {
126140
this.contextRunner.withUserConfiguration(WavefrontSenderConfiguration.class)
127-
.withPropertyValues("spring.application.name=super-application",
128-
"management.wavefront.tracing.service-name=super-service")
141+
.withPropertyValues("spring.application.name=ignored",
142+
"management.wavefront.tracing.application-name=super-application",
143+
"management.wavefront.tracing.service-name=super-service",
144+
"management.wavefront.tracing.cluster-name=super-cluster",
145+
"management.wavefront.tracing.shard-name=super-shard")
129146
.run((context) -> {
130147
ApplicationTags applicationTags = context.getBean(ApplicationTags.class);
131148
assertThat(applicationTags.getApplication()).isEqualTo("super-application");
132149
assertThat(applicationTags.getService()).isEqualTo("super-service");
150+
assertThat(applicationTags.getCluster()).isEqualTo("super-cluster");
151+
assertThat(applicationTags.getShard()).isEqualTo("super-shard");
133152
});
134153
}
135154

0 commit comments

Comments
 (0)