Skip to content

Commit d571fb3

Browse files
committed
Add support for NewRelicClientProvider
Closes gh-20908
1 parent 1342e49 commit d571fb3

File tree

6 files changed

+72
-13
lines changed

6 files changed

+72
-13
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,10 +17,12 @@
1717
package org.springframework.boot.actuate.autoconfigure.metrics.export.newrelic;
1818

1919
import io.micrometer.core.instrument.Clock;
20-
import io.micrometer.core.ipc.http.HttpUrlConnectionSender;
20+
import io.micrometer.newrelic.NewRelicClientProvider;
2121
import io.micrometer.newrelic.NewRelicConfig;
2222
import io.micrometer.newrelic.NewRelicMeterRegistry;
23+
import io.micrometer.newrelic.NewRelicMeterRegistry.Builder;
2324

25+
import org.springframework.beans.factory.ObjectProvider;
2426
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
2527
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
2628
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
@@ -67,11 +69,11 @@ public NewRelicConfig newRelicConfig() {
6769

6870
@Bean
6971
@ConditionalOnMissingBean
70-
public NewRelicMeterRegistry newRelicMeterRegistry(NewRelicConfig newRelicConfig, Clock clock) {
71-
return NewRelicMeterRegistry.builder(newRelicConfig).clock(clock).httpClient(
72-
new HttpUrlConnectionSender(this.properties.getConnectTimeout(), this.properties.getReadTimeout()))
73-
.build();
74-
72+
public NewRelicMeterRegistry newRelicMeterRegistry(NewRelicConfig newRelicConfig, Clock clock,
73+
ObjectProvider<NewRelicClientProvider> newRelicClientProvider) {
74+
Builder builder = NewRelicMeterRegistry.builder(newRelicConfig).clock(clock);
75+
newRelicClientProvider.ifUnique(builder::clientProvider);
76+
return builder.build();
7577
}
7678

7779
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.metrics.export.newrelic;
1818

19+
import io.micrometer.newrelic.ClientProviderType;
20+
1921
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties;
2022
import org.springframework.boot.context.properties.ConfigurationProperties;
2123

@@ -46,6 +48,11 @@ public class NewRelicProperties extends StepRegistryProperties {
4648
*/
4749
private String eventType = "SpringBootSample";
4850

51+
/**
52+
* Client provider type to use.
53+
*/
54+
private ClientProviderType clientProviderType = ClientProviderType.INSIGHTS_API;
55+
4956
/**
5057
* New Relic API key.
5158
*/
@@ -77,6 +84,14 @@ public void setEventType(String eventType) {
7784
this.eventType = eventType;
7885
}
7986

87+
public ClientProviderType getClientProviderType() {
88+
return this.clientProviderType;
89+
}
90+
91+
public void setClientProviderType(ClientProviderType clientProviderType) {
92+
this.clientProviderType = clientProviderType;
93+
}
94+
8095
public String getApiKey() {
8196
return this.apiKey;
8297
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.metrics.export.newrelic;
1818

19+
import io.micrometer.newrelic.ClientProviderType;
1920
import io.micrometer.newrelic.NewRelicConfig;
2021

2122
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapter;
@@ -44,6 +45,11 @@ public String eventType() {
4445
return get(NewRelicProperties::getEventType, NewRelicConfig.super::eventType);
4546
}
4647

48+
@Override
49+
public ClientProviderType clientProviderType() {
50+
return get(NewRelicProperties::getClientProviderType, NewRelicConfig.super::clientProviderType);
51+
}
52+
4753
@Override
4854
public String apiKey() {
4955
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: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.actuate.autoconfigure.metrics.export.newrelic;
1818

1919
import io.micrometer.core.instrument.Clock;
20+
import io.micrometer.newrelic.NewRelicClientProvider;
2021
import io.micrometer.newrelic.NewRelicConfig;
2122
import io.micrometer.newrelic.NewRelicMeterRegistry;
2223
import org.junit.jupiter.api.Test;
@@ -28,12 +29,14 @@
2829
import org.springframework.context.annotation.Import;
2930

3031
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.Mockito.mock;
3133

3234
/**
3335
*
3436
* Tests for {@link NewRelicMetricsExportAutoConfiguration}.
3537
*
3638
* @author Andy Wilkinson
39+
* @author Stephane Nicoll
3740
*/
3841
class NewRelicMetricsExportAutoConfigurationTests {
3942

@@ -69,7 +72,7 @@ void failsToAutoConfigureWithoutEventType() {
6972
}
7073

7174
@Test
72-
void autoConfiguresWithEventTypeOverriden() {
75+
void autoConfiguresWithEventTypeOverridden() {
7376
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
7477
.withPropertyValues("management.metrics.export.newrelic.api-key=abcde",
7578
"management.metrics.export.newrelic.account-id=12345",
@@ -123,6 +126,18 @@ void allowsRegistryToBeCustomized() {
123126
.hasBean("customRegistry"));
124127
}
125128

129+
@Test
130+
void allowsClientProviderToBeCustomized() {
131+
this.contextRunner.withUserConfiguration(CustomClientProviderConfiguration.class)
132+
.withPropertyValues("management.metrics.export.newrelic.api-key=abcde",
133+
"management.metrics.export.newrelic.account-id=12345")
134+
.run((context) -> {
135+
assertThat(context).hasSingleBean(NewRelicMeterRegistry.class);
136+
assertThat(context.getBean(NewRelicMeterRegistry.class))
137+
.hasFieldOrPropertyWithValue("clientProvider", context.getBean("customClientProvider"));
138+
});
139+
}
140+
126141
@Test
127142
void stopsMeterRegistryWhenContextIsClosed() {
128143
this.contextRunner
@@ -176,4 +191,15 @@ NewRelicMeterRegistry customRegistry(NewRelicConfig config, Clock clock) {
176191

177192
}
178193

194+
@Configuration(proxyBeanMethods = false)
195+
@Import(BaseConfiguration.class)
196+
static class CustomClientProviderConfiguration {
197+
198+
@Bean
199+
NewRelicClientProvider customClientProvider() {
200+
return mock(NewRelicClientProvider.class);
201+
}
202+
203+
}
204+
179205
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,13 +35,14 @@ void defaultValuesAreConsistent() {
3535
NewRelicProperties properties = new NewRelicProperties();
3636
NewRelicConfig config = (key) -> null;
3737
assertStepRegistryDefaultValues(properties, config);
38+
assertThat(properties.getClientProviderType()).isEqualTo(config.clientProviderType());
3839
// apiKey and account are mandatory
3940
assertThat(properties.getUri()).isEqualTo(config.uri());
4041
assertThat(properties.isMeterNameEventTypeEnabled()).isEqualTo(config.meterNameEventTypeEnabled());
4142
}
4243

4344
@Test
44-
void eventTypeDefaultValueIsOverriden() {
45+
void eventTypeDefaultValueIsOverridden() {
4546
NewRelicProperties properties = new NewRelicProperties();
4647
NewRelicConfig config = (key) -> null;
4748
assertThat(properties.getEventType()).isNotEqualTo(config.eventType());

spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,15 @@ You can also change the interval at which metrics are sent to New Relic:
16401640
management.metrics.export.newrelic.step=30s
16411641
----
16421642

1643+
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:
1644+
1645+
[source,properties,indent=0,configprops]
1646+
----
1647+
management.metrics.export.newrelic.client-provider-type=insights-agent
1648+
----
1649+
1650+
Finally, you can take full control by defining your own `NewRelicClientProvider` bean.
1651+
16431652

16441653

16451654
[[production-ready-metrics-export-prometheus]]

0 commit comments

Comments
 (0)