Skip to content

Commit

Permalink
Polish 'Align Wavefront application tags support with Spring Boot 2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed Nov 17, 2022
1 parent 42bb4c0 commit a0f39d6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 45 deletions.
Expand Up @@ -77,13 +77,13 @@ public WavefrontMeterRegistry wavefrontMeterRegistry(WavefrontConfig wavefrontCo

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

private static Tag asTag(Map.Entry<String, String> entry) {
private Tag asTag(Map.Entry<String, String> entry) {
return Tag.of(entry.getKey(), entry.getValue());
}

Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.actuate.autoconfigure.tracing.wavefront;

import java.util.Collections;
import java.util.function.Supplier;

import brave.handler.SpanHandler;
import com.wavefront.sdk.common.WavefrontSender;
Expand All @@ -40,10 +41,12 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

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

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

@Bean
@ConditionalOnMissingBean
public ApplicationTags applicationTags(Environment environment, WavefrontProperties properties) {
String fallbackWavefrontServiceName = environment.getProperty("spring.application.name",
DEFAULT_WAVEFRONT_SERVICE_NAME);
public ApplicationTags wavefrontApplicationTags(Environment environment, WavefrontProperties properties) {
Tracing tracing = properties.getTracing();
String wavefrontServiceName = (tracing.getServiceName() != null) ? tracing.getServiceName()
: fallbackWavefrontServiceName;
String wavefrontApplicationName = (tracing.getApplicationName() != null) ? tracing.getApplicationName()
: DEFAULT_WAVEFRONT_APPLICATION_NAME;
String wavefrontServiceName = getName(tracing.getServiceName(),
() -> environment.getProperty("spring.application.name", DEFAULT_SERVICE_NAME));
String wavefrontApplicationName = getName(tracing.getApplicationName(), () -> DEFAULT_APPLICATION_NAME);
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
ApplicationTags.Builder builder = new ApplicationTags.Builder(wavefrontApplicationName, wavefrontServiceName);
if (tracing.getClusterName() != null) {
builder.cluster(tracing.getClusterName());
}
if (tracing.getShardName() != null) {
builder.shard(tracing.getShardName());
}
map.from(tracing::getClusterName).to(builder::cluster);
map.from(tracing::getShardName).to(builder::shard);
return builder.build();
}

private String getName(String value, Supplier<String> fallback) {
return (StringUtils.hasText(value)) ? value : fallback.get();
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(WavefrontSpanHandler.class)
static class WavefrontMicrometer {
Expand Down
Expand Up @@ -21,8 +21,6 @@
import java.net.UnknownHostException;
import java.time.Duration;

import com.wavefront.sdk.common.application.ApplicationTags;

import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PushRegistryProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
Expand Down Expand Up @@ -264,37 +262,24 @@ public void setBatchSize(Integer batchSize) {
public static class Tracing {

/**
* Wavefront Application name used in {@link ApplicationTags}. Defaults to
* Wavefront Application name used in ApplicationTags. Defaults to
* 'unnamed_application'.
* @see <a href=
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
* Application Tags</a>
*/
private String applicationName;

/**
* Wavefront Service name used in {@link ApplicationTags}, falling back to
* {@code spring.application.name}. If both are unset it defaults to
* 'unnamed_service'.
* @see <a href=
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
* Application Tags</a>
* Wavefront Service name used in ApplicationTags, falling back to
* 'spring.application.name'. If both are unset it defaults to 'unnamed_service'.
*/
private String serviceName;

/**
* Optional Wavefront Cluster name used in {@link ApplicationTags}.
* @see <a href=
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
* Application Tags</a>
* Optional Wavefront Cluster name used in ApplicationTags.
*/
private String clusterName;

/**
* Optional Wavefront Shard name used in {@link ApplicationTags}.
* @see <a href=
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
* Application Tags</a>
* Optional Wavefront Shard name used in ApplicationTags.
*/
private String shardName;

Expand Down
Expand Up @@ -88,13 +88,12 @@ void allowsRegistryToBeCustomized() {

@Test
void exportsApplicationTagsInWavefrontRegistry() {
ApplicationTags.Builder appTagsBuilder = new ApplicationTags.Builder("super-application", "super-service");
appTagsBuilder.cluster("super-cluster");
appTagsBuilder.shard("super-shard");
appTagsBuilder.customTags(Map.of("custom-key", "custom-val"));

ApplicationTags.Builder builder = new ApplicationTags.Builder("super-application", "super-service");
builder.cluster("super-cluster");
builder.shard("super-shard");
builder.customTags(Map.of("custom-key", "custom-val"));
this.contextRunner.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class))
.withUserConfiguration(BaseConfiguration.class).withBean(ApplicationTags.class, appTagsBuilder::build)
.withUserConfiguration(BaseConfiguration.class).withBean(ApplicationTags.class, builder::build)
.run((context) -> {
WavefrontMeterRegistry registry = context.getBean(WavefrontMeterRegistry.class);
registry.counter("my.counter", "env", "qa");
Expand Down

0 comments on commit a0f39d6

Please sign in to comment.