Commit e73ae626 authored by Neil Powell's avatar Neil Powell Committed by Phillip Webb

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
parent 3c7d3f94
......@@ -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;
}
......
......@@ -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<NewRelicProperties>
......@@ -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);
......
......@@ -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)
......
......@@ -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");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment