Skip to content

Commit

Permalink
Export Application Tags in WavefrontMeterRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
oppegard committed Oct 21, 2022
1 parent 1c29409 commit 3c5e30d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 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,17 @@ public WavefrontMeterRegistry wavefrontMeterRegistry(WavefrontConfig wavefrontCo
return WavefrontMeterRegistry.builder(wavefrontConfig).clock(clock).wavefrontSender(wavefrontSender).build();
}

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

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

}
Expand Up @@ -16,12 +16,17 @@

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 +41,7 @@
*
* @author Jon Schneider
* @author Stephane Nicoll
* @author Glenn Oppegard
*/
class WavefrontMetricsExportAutoConfigurationTests {

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

@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"));

this.contextRunner.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class))
.withUserConfiguration(BaseConfiguration.class)
.withBean(ApplicationTags.class, appTagsBuilder::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

0 comments on commit 3c5e30d

Please sign in to comment.