Skip to content

Commit

Permalink
Merge pull request #32844 from oppegard
Browse files Browse the repository at this point in the history
* pr/32844:
  Polish 'Align Wavefront application tags support with Spring Boot 2.x'
  Align Wavefront application tags support with Spring Boot 2.x
  Switch to use BeanUtils.getPropertyDescriptors

Closes gh-32844
  • Loading branch information
philwebb committed Nov 17, 2022
2 parents 2e8d766 + a0f39d6 commit 7525c0f
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 38 deletions.
Expand Up @@ -16,12 +16,18 @@

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

import java.util.Map;

import com.wavefront.sdk.common.WavefrontSender;
import com.wavefront.sdk.common.application.ApplicationTags;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.wavefront.WavefrontConfig;
import io.micrometer.wavefront.WavefrontMeterRegistry;

import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.ConditionalOnEnabledMetricsExport;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
Expand All @@ -42,6 +48,7 @@
* @author Jon Schneider
* @author Artsiom Yudovin
* @author Stephane Nicoll
* @author Glenn Oppegard
* @since 2.0.0
*/
@AutoConfiguration(
Expand All @@ -68,4 +75,16 @@ public WavefrontMeterRegistry wavefrontMeterRegistry(WavefrontConfig wavefrontCo
return WavefrontMeterRegistry.builder(wavefrontConfig).clock(clock).wavefrontSender(wavefrontSender).build();
}

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

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

}
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,15 +41,18 @@
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.
*
* @author Moritz Halbritter
* @author Glenn Oppegard
* @since 3.0.0
*/
@AutoConfiguration(after = { MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class })
Expand All @@ -59,19 +63,38 @@
public class WavefrontTracingAutoConfiguration {

/**
* Default value for application name if {@code spring.application.name} is not set.
* Default value for the Wavefront Application name.
* @see <a href=
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
* Application Tags</a>
*/
private static final String DEFAULT_APPLICATION_NAME = "application";
private static final String DEFAULT_APPLICATION_NAME = "unnamed_application";

/**
* Default value for the Wavefront Service name if {@code spring.application.name} is
* not set.
* @see <a href=
* "https://docs.wavefront.com/trace_data_details.html#application-tags">Wavefront
* Application Tags</a>
*/
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);
public ApplicationTags wavefrontApplicationTags(Environment environment, WavefrontProperties properties) {
Tracing tracing = properties.getTracing();
String applicationName = (tracing.getApplicationName() != null) ? tracing.getApplicationName()
: springApplicationName;
String serviceName = (tracing.getServiceName() != null) ? tracing.getServiceName() : springApplicationName;
return new ApplicationTags.Builder(applicationName, serviceName).build();
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);
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)
Expand Down
Expand Up @@ -30,6 +30,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 +262,27 @@ public void setBatchSize(Integer batchSize) {
public static class Tracing {

/**
* Application name. Defaults to 'spring.application.name'.
* Wavefront Application name used in ApplicationTags. Defaults to
* 'unnamed_application'.
*/
private String applicationName;

/**
* Service name. Defaults to 'spring.application.name'.
* 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 ApplicationTags.
*/
private String clusterName;

/**
* Optional Wavefront Shard name used in ApplicationTags.
*/
private String shardName;

public String getServiceName() {
return this.serviceName;
}
Expand All @@ -286,6 +299,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 @@ -16,12 +16,16 @@

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

import java.util.Map;

import com.wavefront.sdk.common.WavefrontSender;
import com.wavefront.sdk.common.application.ApplicationTags;
import io.micrometer.core.instrument.Clock;
import io.micrometer.wavefront.WavefrontConfig;
import io.micrometer.wavefront.WavefrontMeterRegistry;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
Expand All @@ -36,6 +40,7 @@
*
* @author Jon Schneider
* @author Stephane Nicoll
* @author Glenn Oppegard
*/
class WavefrontMetricsExportAutoConfigurationTests {

Expand Down Expand Up @@ -81,6 +86,23 @@ void allowsRegistryToBeCustomized() {
.hasSingleBean(WavefrontMeterRegistry.class).hasBean("customRegistry"));
}

@Test
void exportsApplicationTagsInWavefrontRegistry() {
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, builder::build)
.run((context) -> {
WavefrontMeterRegistry registry = context.getBean(WavefrontMeterRegistry.class);
registry.counter("my.counter", "env", "qa");
assertThat(registry.find("my.counter").tags("env", "qa").tags("application", "super-application")
.tags("service", "super-service").tags("cluster", "super-cluster")
.tags("shard", "super-shard").tags("custom-key", "custom-val").counter()).isNotNull();
});
}

@Test
void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
Expand Down
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 shouldNotSupplyWavefrontOtelSpanExporterIfOtelIsMissing() {
}

@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
Expand Up @@ -16,9 +16,6 @@

package org.springframework.boot.context.properties.bind;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
Expand All @@ -34,14 +31,14 @@
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.beans.BeanInfoFactory;
import org.springframework.beans.ExtendedBeanInfoFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.ResolvableType;
import org.springframework.core.StandardReflectionParameterNameDiscoverer;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;

/**
Expand All @@ -59,8 +56,6 @@
*/
public class BindableRuntimeHintsRegistrar implements RuntimeHintsRegistrar {

private static final BeanInfoFactory beanInfoFactory = new ExtendedBeanInfoFactory();

private final Class<?>[] types;

/**
Expand Down Expand Up @@ -120,7 +115,7 @@ private final class Processor {

private final Constructor<?> bindConstructor;

private final BeanInfo beanInfo;
private final PropertyDescriptor[] propertyDescriptors;

private final Set<Class<?>> seen;

Expand All @@ -134,24 +129,11 @@ private Processor(Class<?> type, boolean nestedType, Set<Class<?>> seen,
Set<Class<?>> compiledWithoutParameters) {
this.type = type;
this.bindConstructor = BindConstructorProvider.DEFAULT.getBindConstructor(Bindable.of(type), nestedType);
this.beanInfo = getBeanInfo(type);
this.propertyDescriptors = BeanUtils.getPropertyDescriptors(type);
this.seen = seen;
this.compiledWithoutParameters = compiledWithoutParameters;
}

private static BeanInfo getBeanInfo(Class<?> beanType) {
try {
BeanInfo beanInfo = beanInfoFactory.getBeanInfo(beanType);
if (beanInfo != null) {
return beanInfo;
}
return Introspector.getBeanInfo(beanType, Introspector.IGNORE_ALL_BEANINFO);
}
catch (IntrospectionException ex) {
return null;
}
}

void process(ReflectionHints hints) {
if (this.seen.contains(this.type)) {
return;
Expand All @@ -161,7 +143,7 @@ void process(ReflectionHints hints) {
if (this.bindConstructor != null) {
handleValueObjectProperties(hints);
}
else if (this.beanInfo != null) {
else if (!ObjectUtils.isEmpty(this.propertyDescriptors)) {
handleJavaBeanProperties(hints);
}
}
Expand Down Expand Up @@ -196,7 +178,7 @@ private void handleValueObjectProperties(ReflectionHints hints) {
}

private void handleJavaBeanProperties(ReflectionHints hints) {
for (PropertyDescriptor propertyDescriptor : this.beanInfo.getPropertyDescriptors()) {
for (PropertyDescriptor propertyDescriptor : this.propertyDescriptors) {
Method writeMethod = propertyDescriptor.getWriteMethod();
if (writeMethod != null) {
hints.registerMethod(writeMethod, ExecutableMode.INVOKE);
Expand Down

0 comments on commit 7525c0f

Please sign in to comment.