Add support for NewRelicClientProvider

Closes gh-20908
This commit is contained in:
Stephane Nicoll 2020-04-10 11:34:19 +02:00
parent 1342e4970a
commit d571fb311f
6 changed files with 72 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,10 +17,12 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.newrelic;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.ipc.http.HttpUrlConnectionSender;
import io.micrometer.newrelic.NewRelicClientProvider;
import io.micrometer.newrelic.NewRelicConfig;
import io.micrometer.newrelic.NewRelicMeterRegistry;
import io.micrometer.newrelic.NewRelicMeterRegistry.Builder;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
@ -67,11 +69,11 @@ public class NewRelicMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public NewRelicMeterRegistry newRelicMeterRegistry(NewRelicConfig newRelicConfig, Clock clock) {
return NewRelicMeterRegistry.builder(newRelicConfig).clock(clock).httpClient(
new HttpUrlConnectionSender(this.properties.getConnectTimeout(), this.properties.getReadTimeout()))
.build();
public NewRelicMeterRegistry newRelicMeterRegistry(NewRelicConfig newRelicConfig, Clock clock,
ObjectProvider<NewRelicClientProvider> newRelicClientProvider) {
Builder builder = NewRelicMeterRegistry.builder(newRelicConfig).clock(clock);
newRelicClientProvider.ifUnique(builder::clientProvider);
return builder.build();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.newrelic;
import io.micrometer.newrelic.ClientProviderType;
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ -46,6 +48,11 @@ public class NewRelicProperties extends StepRegistryProperties {
*/
private String eventType = "SpringBootSample";
/**
* Client provider type to use.
*/
private ClientProviderType clientProviderType = ClientProviderType.INSIGHTS_API;
/**
* New Relic API key.
*/
@ -77,6 +84,14 @@ public class NewRelicProperties extends StepRegistryProperties {
this.eventType = eventType;
}
public ClientProviderType getClientProviderType() {
return this.clientProviderType;
}
public void setClientProviderType(ClientProviderType clientProviderType) {
this.clientProviderType = clientProviderType;
}
public String getApiKey() {
return this.apiKey;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.newrelic;
import io.micrometer.newrelic.ClientProviderType;
import io.micrometer.newrelic.NewRelicConfig;
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapter;
@ -44,6 +45,11 @@ public class NewRelicPropertiesConfigAdapter extends StepRegistryPropertiesConfi
return get(NewRelicProperties::getEventType, NewRelicConfig.super::eventType);
}
@Override
public ClientProviderType clientProviderType() {
return get(NewRelicProperties::getClientProviderType, NewRelicConfig.super::clientProviderType);
}
@Override
public String apiKey() {
return get(NewRelicProperties::getApiKey, NewRelicConfig.super::apiKey);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.newrelic;
import io.micrometer.core.instrument.Clock;
import io.micrometer.newrelic.NewRelicClientProvider;
import io.micrometer.newrelic.NewRelicConfig;
import io.micrometer.newrelic.NewRelicMeterRegistry;
import org.junit.jupiter.api.Test;
@ -28,12 +29,14 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
*
* Tests for {@link NewRelicMetricsExportAutoConfiguration}.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
class NewRelicMetricsExportAutoConfigurationTests {
@ -69,7 +72,7 @@ class NewRelicMetricsExportAutoConfigurationTests {
}
@Test
void autoConfiguresWithEventTypeOverriden() {
void autoConfiguresWithEventTypeOverridden() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.withPropertyValues("management.metrics.export.newrelic.api-key=abcde",
"management.metrics.export.newrelic.account-id=12345",
@ -123,6 +126,18 @@ class NewRelicMetricsExportAutoConfigurationTests {
.hasBean("customRegistry"));
}
@Test
void allowsClientProviderToBeCustomized() {
this.contextRunner.withUserConfiguration(CustomClientProviderConfiguration.class)
.withPropertyValues("management.metrics.export.newrelic.api-key=abcde",
"management.metrics.export.newrelic.account-id=12345")
.run((context) -> {
assertThat(context).hasSingleBean(NewRelicMeterRegistry.class);
assertThat(context.getBean(NewRelicMeterRegistry.class))
.hasFieldOrPropertyWithValue("clientProvider", context.getBean("customClientProvider"));
});
}
@Test
void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner
@ -176,4 +191,15 @@ class NewRelicMetricsExportAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
@Import(BaseConfiguration.class)
static class CustomClientProviderConfiguration {
@Bean
NewRelicClientProvider customClientProvider() {
return mock(NewRelicClientProvider.class);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -35,13 +35,14 @@ class NewRelicPropertiesTests extends StepRegistryPropertiesTests {
NewRelicProperties properties = new NewRelicProperties();
NewRelicConfig config = (key) -> null;
assertStepRegistryDefaultValues(properties, config);
assertThat(properties.getClientProviderType()).isEqualTo(config.clientProviderType());
// apiKey and account are mandatory
assertThat(properties.getUri()).isEqualTo(config.uri());
assertThat(properties.isMeterNameEventTypeEnabled()).isEqualTo(config.meterNameEventTypeEnabled());
}
@Test
void eventTypeDefaultValueIsOverriden() {
void eventTypeDefaultValueIsOverridden() {
NewRelicProperties properties = new NewRelicProperties();
NewRelicConfig config = (key) -> null;
assertThat(properties.getEventType()).isNotEqualTo(config.eventType());

View File

@ -1640,6 +1640,15 @@ You can also change the interval at which metrics are sent to New Relic:
management.metrics.export.newrelic.step=30s
----
By default, metrics are published via REST calls but it also possible to use the Java Agent API if you have it on the classpath:
[source,properties,indent=0,configprops]
----
management.metrics.export.newrelic.client-provider-type=insights-agent
----
Finally, you can take full control by defining your own `NewRelicClientProvider` bean.
[[production-ready-metrics-export-prometheus]]