From d571fb311f4dcfa91de2ab965104410dabe64771 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 10 Apr 2020 11:34:19 +0200 Subject: [PATCH] Add support for NewRelicClientProvider Closes gh-20908 --- ...ewRelicMetricsExportAutoConfiguration.java | 16 +++++----- .../export/newrelic/NewRelicProperties.java | 17 ++++++++++- .../NewRelicPropertiesConfigAdapter.java | 8 ++++- ...icMetricsExportAutoConfigurationTests.java | 30 +++++++++++++++++-- .../newrelic/NewRelicPropertiesTests.java | 5 ++-- .../asciidoc/production-ready-features.adoc | 9 ++++++ 6 files changed, 72 insertions(+), 13 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicMetricsExportAutoConfiguration.java index a666810ef2..138b05ad79 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/newrelic/NewRelicMetricsExportAutoConfiguration.java @@ -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) { + Builder builder = NewRelicMeterRegistry.builder(newRelicConfig).clock(clock); + newRelicClientProvider.ifUnique(builder::clientProvider); + return builder.build(); } } 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 de7370e6c8..62d0075e72 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 @@ -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; } 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 d684df089f..b57f9b9f9e 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 @@ -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); 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 8df7196c21..a7f5bae47b 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 @@ -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); + } + + } + } 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 b2c416f986..1175a46dfb 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 @@ -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()); diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc index 97772f0bb8..cb58b24ad0 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc @@ -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]]