Skip to content

Commit 4cfbe7f

Browse files
committed
Merge pull request #18472 from neiljpowell
* pr/18472: Polish 'Support 'New Relic' eventType properties' Support 'New Relic' eventType properties Closes gh-18472
2 parents 3c7d3f9 + 946202b commit 4cfbe7f

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicProperties.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,26 @@
2626
* @author Jon Schneider
2727
* @author Andy Wilkinson
2828
* @author Stephane Nicoll
29+
* @author Neil Powell
2930
* @since 2.0.0
3031
*/
3132
@ConfigurationProperties(prefix = "management.metrics.export.newrelic")
3233
public class NewRelicProperties extends StepRegistryProperties {
3334

35+
/**
36+
* Whether to send the meter name as the event type instead of using the 'event-type'
37+
* configuration property value. Can be set to 'true' if New Relic guidelines are not
38+
* being followed or event types consistent with previous Spring Boot releases are
39+
* required.
40+
*/
41+
private boolean meterNameEventTypeEnabled;
42+
43+
/**
44+
* The event type that should be published. This property will be ignored if
45+
* 'meter-name-event-type-enabled' is set to 'true'.
46+
*/
47+
private String eventType = "SpringBootSample";
48+
3449
/**
3550
* New Relic API key.
3651
*/
@@ -46,6 +61,22 @@ public class NewRelicProperties extends StepRegistryProperties {
4661
*/
4762
private String uri = "https://insights-collector.newrelic.com";
4863

64+
public boolean isMeterNameEventTypeEnabled() {
65+
return this.meterNameEventTypeEnabled;
66+
}
67+
68+
public void setMeterNameEventTypeEnabled(boolean meterNameEventTypeEnabled) {
69+
this.meterNameEventTypeEnabled = meterNameEventTypeEnabled;
70+
}
71+
72+
public String getEventType() {
73+
return this.eventType;
74+
}
75+
76+
public void setEventType(String eventType) {
77+
this.eventType = eventType;
78+
}
79+
4980
public String getApiKey() {
5081
return this.apiKey;
5182
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicPropertiesConfigAdapter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* Adapter to convert {@link NewRelicProperties} to a {@link NewRelicConfig}.
2525
*
2626
* @author Jon Schneider
27+
* @author Neil Powell
2728
* @since 2.0.0
2829
*/
2930
public class NewRelicPropertiesConfigAdapter extends StepRegistryPropertiesConfigAdapter<NewRelicProperties>
@@ -33,6 +34,16 @@ public NewRelicPropertiesConfigAdapter(NewRelicProperties properties) {
3334
super(properties);
3435
}
3536

37+
@Override
38+
public boolean meterNameEventTypeEnabled() {
39+
return get(NewRelicProperties::isMeterNameEventTypeEnabled, NewRelicConfig.super::meterNameEventTypeEnabled);
40+
}
41+
42+
@Override
43+
public String eventType() {
44+
return get(NewRelicProperties::getEventType, NewRelicConfig.super::eventType);
45+
}
46+
3647
@Override
3748
public String apiKey() {
3849
return get(NewRelicProperties::getApiKey, NewRelicConfig.super::apiKey);

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicMetricsExportAutoConfigurationTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,36 @@ void failsWithoutAnAccountId() {
5959
.run((context) -> assertThat(context).hasFailed());
6060
}
6161

62+
@Test
63+
void failsToAutoConfigureWithoutEventType() {
64+
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
65+
.withPropertyValues("management.metrics.export.newrelic.api-key=abcde",
66+
"management.metrics.export.newrelic.account-id=12345",
67+
"management.metrics.export.newrelic.event-type=")
68+
.run((context) -> assertThat(context).hasFailed());
69+
}
70+
71+
@Test
72+
void autoConfiguresWithEventTypeOverriden() {
73+
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
74+
.withPropertyValues("management.metrics.export.newrelic.api-key=abcde",
75+
"management.metrics.export.newrelic.account-id=12345",
76+
"management.metrics.export.newrelic.event-type=wxyz")
77+
.run((context) -> assertThat(context).hasSingleBean(NewRelicMeterRegistry.class)
78+
.hasSingleBean(Clock.class).hasSingleBean(NewRelicConfig.class));
79+
}
80+
81+
@Test
82+
void autoConfiguresWithMeterNameEventTypeEnabledAndWithoutEventType() {
83+
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
84+
.withPropertyValues("management.metrics.export.newrelic.api-key=abcde",
85+
"management.metrics.export.newrelic.account-id=12345",
86+
"management.metrics.export.newrelic.event-type=",
87+
"management.metrics.export.newrelic.meter-name-event-type-enabled=true")
88+
.run((context) -> assertThat(context).hasSingleBean(NewRelicMeterRegistry.class)
89+
.hasSingleBean(Clock.class).hasSingleBean(NewRelicConfig.class));
90+
}
91+
6292
@Test
6393
void autoConfiguresWithAccountIdAndApiKey() {
6494
this.contextRunner.withUserConfiguration(BaseConfiguration.class)

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicPropertiesTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ void defaultValuesAreConsistent() {
3737
assertStepRegistryDefaultValues(properties, config);
3838
// apiKey and account are mandatory
3939
assertThat(properties.getUri()).isEqualTo(config.uri());
40+
assertThat(properties.isMeterNameEventTypeEnabled()).isEqualTo(config.meterNameEventTypeEnabled());
41+
}
42+
43+
@Test
44+
void eventTypeDefaultValueIsOverriden() {
45+
NewRelicProperties properties = new NewRelicProperties();
46+
NewRelicConfig config = (key) -> null;
47+
assertThat(properties.getEventType()).isNotEqualTo(config.eventType());
48+
assertThat(properties.getEventType()).isEqualTo("SpringBootSample");
49+
assertThat(config.eventType()).isEqualTo("MicrometerSample");
4050
}
4151

4252
}

0 commit comments

Comments
 (0)