From e73ae62621a0d2e097a7c01c30b90c425a354558 Mon Sep 17 00:00:00 2001 From: Neil Powell <52715665+neiljpowell@users.noreply.github.com> Date: Tue, 1 Oct 2019 22:21:56 -0500 Subject: [PATCH 1/2] Support 'New Relic' eventType properties Update `NewRelicProperties` so that the event type sent with each metric can be configured. An additional `boolean` property has also been added if the previous behavior using the "meter-name" is required. NewRelic's own agents publish metrics to eventTypes aligned with broader categories. To align with their recommendation the default behavior is to publish metrics under a "SpringBootSample" category. When doing so, additional context is provided by including "metricName" and "metricType" attributes. See gh-18472 --- .../export/newrelic/NewRelicProperties.java | 31 +++++++++++++++++++ .../NewRelicPropertiesConfigAdapter.java | 11 +++++++ ...icMetricsExportAutoConfigurationTests.java | 30 ++++++++++++++++++ .../newrelic/NewRelicPropertiesTests.java | 10 ++++++ 4 files changed, 82 insertions(+) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicProperties.java index 54a248f070f..15b434060b2 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicProperties.java @@ -26,11 +26,26 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * @author Jon Schneider * @author Andy Wilkinson * @author Stephane Nicoll + * @author Neil Powell * @since 2.0.0 */ @ConfigurationProperties(prefix = "management.metrics.export.newrelic") public class NewRelicProperties extends StepRegistryProperties { + /** + * When this is {@code false}, the New Relic eventType value will be set to + * {@link #eventType()}. Otherwise, the meter name will be used. Defaults to + * {@code false}. + */ + private boolean meterNameEventTypeEnabled; + + /** + * This configuration property will only be used if + * {@link #meterNameEventTypeEnabled()} is {@code false}. Default value is + * {@code SpringBootSample}. + */ + private String eventType = "SpringBootSample"; + /** * New Relic API key. */ @@ -46,6 +61,22 @@ public class NewRelicProperties extends StepRegistryProperties { */ private String uri = "https://insights-collector.newrelic.com"; + public boolean isMeterNameEventTypeEnabled() { + return this.meterNameEventTypeEnabled; + } + + public void setMeterNameEventTypeEnabled(boolean meterNameEventTypeEnabled) { + this.meterNameEventTypeEnabled = meterNameEventTypeEnabled; + } + + public String getEventType() { + return this.eventType; + } + + public void setEventType(String eventType) { + this.eventType = eventType; + } + public String getApiKey() { return this.apiKey; } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicPropertiesConfigAdapter.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicPropertiesConfigAdapter.java index 55f03a12d16..d684df089f1 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicPropertiesConfigAdapter.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicPropertiesConfigAdapter.java @@ -24,6 +24,7 @@ import org.springframework.boot.actuate.autoconfigure.metrics.export.properties. * Adapter to convert {@link NewRelicProperties} to a {@link NewRelicConfig}. * * @author Jon Schneider + * @author Neil Powell * @since 2.0.0 */ public class NewRelicPropertiesConfigAdapter extends StepRegistryPropertiesConfigAdapter @@ -33,6 +34,16 @@ public class NewRelicPropertiesConfigAdapter extends StepRegistryPropertiesConfi super(properties); } + @Override + public boolean meterNameEventTypeEnabled() { + return get(NewRelicProperties::isMeterNameEventTypeEnabled, NewRelicConfig.super::meterNameEventTypeEnabled); + } + + @Override + public String eventType() { + return get(NewRelicProperties::getEventType, NewRelicConfig.super::eventType); + } + @Override public String apiKey() { return get(NewRelicProperties::getApiKey, NewRelicConfig.super::apiKey); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicMetricsExportAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicMetricsExportAutoConfigurationTests.java index 98da1b03ceb..8df7196c21e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicMetricsExportAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicMetricsExportAutoConfigurationTests.java @@ -59,6 +59,36 @@ class NewRelicMetricsExportAutoConfigurationTests { .run((context) -> assertThat(context).hasFailed()); } + @Test + void failsToAutoConfigureWithoutEventType() { + this.contextRunner.withUserConfiguration(BaseConfiguration.class) + .withPropertyValues("management.metrics.export.newrelic.api-key=abcde", + "management.metrics.export.newrelic.account-id=12345", + "management.metrics.export.newrelic.event-type=") + .run((context) -> assertThat(context).hasFailed()); + } + + @Test + void autoConfiguresWithEventTypeOverriden() { + this.contextRunner.withUserConfiguration(BaseConfiguration.class) + .withPropertyValues("management.metrics.export.newrelic.api-key=abcde", + "management.metrics.export.newrelic.account-id=12345", + "management.metrics.export.newrelic.event-type=wxyz") + .run((context) -> assertThat(context).hasSingleBean(NewRelicMeterRegistry.class) + .hasSingleBean(Clock.class).hasSingleBean(NewRelicConfig.class)); + } + + @Test + void autoConfiguresWithMeterNameEventTypeEnabledAndWithoutEventType() { + this.contextRunner.withUserConfiguration(BaseConfiguration.class) + .withPropertyValues("management.metrics.export.newrelic.api-key=abcde", + "management.metrics.export.newrelic.account-id=12345", + "management.metrics.export.newrelic.event-type=", + "management.metrics.export.newrelic.meter-name-event-type-enabled=true") + .run((context) -> assertThat(context).hasSingleBean(NewRelicMeterRegistry.class) + .hasSingleBean(Clock.class).hasSingleBean(NewRelicConfig.class)); + } + @Test void autoConfiguresWithAccountIdAndApiKey() { this.contextRunner.withUserConfiguration(BaseConfiguration.class) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicPropertiesTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicPropertiesTests.java index af63e34ee02..b2c416f9869 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicPropertiesTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicPropertiesTests.java @@ -37,6 +37,16 @@ class NewRelicPropertiesTests extends StepRegistryPropertiesTests { assertStepRegistryDefaultValues(properties, config); // apiKey and account are mandatory assertThat(properties.getUri()).isEqualTo(config.uri()); + assertThat(properties.isMeterNameEventTypeEnabled()).isEqualTo(config.meterNameEventTypeEnabled()); + } + + @Test + void eventTypeDefaultValueIsOverriden() { + NewRelicProperties properties = new NewRelicProperties(); + NewRelicConfig config = (key) -> null; + assertThat(properties.getEventType()).isNotEqualTo(config.eventType()); + assertThat(properties.getEventType()).isEqualTo("SpringBootSample"); + assertThat(config.eventType()).isEqualTo("MicrometerSample"); } } From 946202bd42f5a5419b167e74abb8f38caf2d0274 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 2 Oct 2019 22:08:00 -0700 Subject: [PATCH 2/2] Polish 'Support 'New Relic' eventType properties' See gh-18472 --- .../metrics/export/newrelic/NewRelicProperties.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicProperties.java index 15b434060b2..de7370e6c8b 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicProperties.java @@ -33,16 +33,16 @@ import org.springframework.boot.context.properties.ConfigurationProperties; public class NewRelicProperties extends StepRegistryProperties { /** - * When this is {@code false}, the New Relic eventType value will be set to - * {@link #eventType()}. Otherwise, the meter name will be used. Defaults to - * {@code false}. + * Whether to send the meter name as the event type instead of using the 'event-type' + * configuration property value. Can be set to 'true' if New Relic guidelines are not + * being followed or event types consistent with previous Spring Boot releases are + * required. */ private boolean meterNameEventTypeEnabled; /** - * This configuration property will only be used if - * {@link #meterNameEventTypeEnabled()} is {@code false}. Default value is - * {@code SpringBootSample}. + * The event type that should be published. This property will be ignored if + * 'meter-name-event-type-enabled' is set to 'true'. */ private String eventType = "SpringBootSample";