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 54a248f070..de7370e6c8 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 { + /** + * 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; + + /** + * 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"; + /** * 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 55f03a12d1..d684df089f 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 98da1b03ce..8df7196c21 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 af63e34ee0..b2c416f986 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"); } }