Skip to content

Commit

Permalink
Add cluster and shard to WavefrontProperties
Browse files Browse the repository at this point in the history
- Also fix use of application and service tags to reflect their
  meaning in Wavefront
  • Loading branch information
oppegard committed Oct 21, 2022
1 parent b0b2818 commit d7157df
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
Expand Up @@ -49,6 +49,7 @@
* {@link EnableAutoConfiguration Auto-configuration} for Wavefront tracing.
*
* @author Moritz Halbritter
* @author Glenn Oppegard
* @since 3.0.0
*/
@AutoConfiguration(after = { MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class })
Expand All @@ -58,20 +59,29 @@
@ConditionalOnEnabledTracing
public class WavefrontTracingAutoConfiguration {

private static final String DEFAULT_APPLICATION_NAME = "unnamed_application";

/**
* Default value for application name if {@code spring.application.name} is not set.
* Default value for service name if {@code spring.application.name} is not set.
*/
private static final String DEFAULT_APPLICATION_NAME = "application";
private static final String DEFAULT_SERVICE_NAME = "unnamed_service";

@Bean
@ConditionalOnMissingBean
public ApplicationTags applicationTags(Environment environment, WavefrontProperties properties) {
String springApplicationName = environment.getProperty("spring.application.name", DEFAULT_APPLICATION_NAME);
String fallbackServiceName = environment.getProperty("spring.application.name", DEFAULT_SERVICE_NAME);
Tracing tracing = properties.getTracing();
String serviceName = (tracing.getServiceName() != null) ? tracing.getServiceName() : fallbackServiceName;
String applicationName = (tracing.getApplicationName() != null) ? tracing.getApplicationName()
: springApplicationName;
String serviceName = (tracing.getServiceName() != null) ? tracing.getServiceName() : springApplicationName;
return new ApplicationTags.Builder(applicationName, serviceName).build();
: DEFAULT_APPLICATION_NAME;
ApplicationTags.Builder builder = new ApplicationTags.Builder(applicationName, serviceName);
if (tracing.getClusterName() != null) {
builder.cluster(tracing.getClusterName());
}
if (tracing.getShardName() != null) {
builder.shard(tracing.getShardName());
}
return builder.build();
}

@Configuration(proxyBeanMethods = false)
Expand Down
Expand Up @@ -21,6 +21,8 @@
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 All @@ -30,6 +32,7 @@
* Configuration properties to configure Wavefront.
*
* @author Moritz Halbritter
* @author Glenn Oppegard
* @since 3.0.0
*/
@ConfigurationProperties(prefix = "management.wavefront")
Expand Down Expand Up @@ -261,15 +264,28 @@ public void setBatchSize(Integer batchSize) {
public static class Tracing {

/**
* Application name. Defaults to 'spring.application.name'.
* Application name used in {@link ApplicationTags}. Defaults to
* 'unnamed_application'.
*/
private String applicationName;

/**
* Service name. Defaults to 'spring.application.name'.
* Service name used in {@link ApplicationTags}, falling back to
* {@code spring.application.name}. If both are unset it defaults to
* 'unnamed_service'.
*/
private String serviceName;

/**
* Optional cluster name used in {@link ApplicationTags}.
*/
private String clusterName;

/**
* Optional shard name used in {@link ApplicationTags}.
*/
private String shardName;

public String getServiceName() {
return this.serviceName;
}
Expand All @@ -286,6 +302,22 @@ public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}

public String getClusterName() {
return this.clusterName;
}

public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}

public String getShardName() {
return this.shardName;
}

public void setShardName(String shardName) {
this.shardName = shardName;
}

}

}
Expand Up @@ -39,6 +39,7 @@
* Tests for {@link WavefrontTracingAutoConfiguration}.
*
* @author Moritz Halbritter
* @author Glenn Oppegard
*/
class WavefrontTracingAutoConfigurationTests {

Expand Down Expand Up @@ -114,22 +115,40 @@ void shouldNotSupplyWavefrontOtelSpanHandlerIfOtelIsMissing() {
}

@Test
void shouldHaveADefaultApplicationName() {
void shouldHaveADefaultApplicationNameAndServiceName() {
this.contextRunner.withUserConfiguration(WavefrontSenderConfiguration.class).run((context) -> {
ApplicationTags applicationTags = context.getBean(ApplicationTags.class);
assertThat(applicationTags.getApplication()).isEqualTo("application");
assertThat(applicationTags.getApplication()).isEqualTo("unnamed_application");
assertThat(applicationTags.getService()).isEqualTo("unnamed_service");
assertThat(applicationTags.getCluster()).isNull();
assertThat(applicationTags.getShard()).isNull();
});
}

@Test
void shouldUseSpringApplicationNameForServiceName() {
this.contextRunner.withUserConfiguration(WavefrontSenderConfiguration.class)
.withPropertyValues("spring.application.name=super-service").run((context) -> {
ApplicationTags applicationTags = context.getBean(ApplicationTags.class);
assertThat(applicationTags.getApplication()).isEqualTo("unnamed_application");
assertThat(applicationTags.getService()).isEqualTo("super-service");
});
}

@Test
void shouldHonorConfigProperties() {
this.contextRunner.withUserConfiguration(WavefrontSenderConfiguration.class)
.withPropertyValues("spring.application.name=super-application",
"management.wavefront.tracing.service-name=super-service")
.withPropertyValues("spring.application.name=ignored",
"management.wavefront.tracing.application-name=super-application",
"management.wavefront.tracing.service-name=super-service",
"management.wavefront.tracing.cluster-name=super-cluster",
"management.wavefront.tracing.shard-name=super-shard")
.run((context) -> {
ApplicationTags applicationTags = context.getBean(ApplicationTags.class);
assertThat(applicationTags.getApplication()).isEqualTo("super-application");
assertThat(applicationTags.getService()).isEqualTo("super-service");
assertThat(applicationTags.getCluster()).isEqualTo("super-cluster");
assertThat(applicationTags.getShard()).isEqualTo("super-shard");
});
}

Expand Down

0 comments on commit d7157df

Please sign in to comment.