From 0b084166ef57cf7e42d7d0040b29b96fc5267f1d Mon Sep 17 00:00:00 2001 From: Nemanja Mikic Date: Thu, 5 Dec 2024 19:12:54 +0100 Subject: [PATCH 1/4] fix metrics endpoint --- .../micronaut/oraclecloud/client/OciMonitoringSpec.groovy | 3 +-- .../oraclecloud/monitoring/MonitoringIngestionClient.java | 6 +++--- .../micrometer/OracleCloudMeterRawRegistrySpec.groovy | 1 - 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/oraclecloud-httpclient-netty/src/test/groovy/io/micronaut/oraclecloud/client/OciMonitoringSpec.groovy b/oraclecloud-httpclient-netty/src/test/groovy/io/micronaut/oraclecloud/client/OciMonitoringSpec.groovy index 098f91ad8..1e96b89df 100644 --- a/oraclecloud-httpclient-netty/src/test/groovy/io/micronaut/oraclecloud/client/OciMonitoringSpec.groovy +++ b/oraclecloud-httpclient-netty/src/test/groovy/io/micronaut/oraclecloud/client/OciMonitoringSpec.groovy @@ -92,8 +92,7 @@ class OciMonitoringSpec extends Specification { } MonitoringClient createTelemetryClient() { - String ingestionEndpoint = String.format("https://telemetry-ingestion.%s.oraclecloud.com", - regionProvider.getRegion().getRegionId()); + String ingestionEndpoint = regionProvider.getRegion().getEndpoint(MonitoringClient.SERVICE).orElseThrow() return MonitoringClient.builder().endpoint(ingestionEndpoint).build(authenticationDetailsProvider) } diff --git a/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/MonitoringIngestionClient.java b/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/MonitoringIngestionClient.java index 5566ff1b2..8732a3f33 100644 --- a/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/MonitoringIngestionClient.java +++ b/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/MonitoringIngestionClient.java @@ -30,7 +30,7 @@ import java.util.Objects; /** - * Oracle SDK provides {@link MonitoringClient} that is constructed with default {@code https://telemetry..oraclecloud.com} endpoint. + * Oracle SDK provides {@link MonitoringClient} that is constructed with default endpoint based on region provided by region provider. * For sending metrics to the Oracle Cloud Monitoring service the {@link MonitoringClient#postMetricData(PostMetricDataRequest)} is used * but the endpoint must be configured to {@code https://telemetry-ingestion..oraclecloud.com}. This bean encapsulates creation * and configuration of the {@link MonitoringClient} to use {@link MonitoringClient#postMetricData(PostMetricDataRequest)} @@ -105,8 +105,8 @@ public MonitoringClient getDelegate() { if (delegate == null) { synchronized (MonitoringIngestionClient.class) { if (delegate == null) { - String ingestionEndpoint = String.format("https://telemetry-ingestion.%s.oraclecloud.com", - regionProvider.getRegion().getRegionId()); + String ingestionEndpoint = regionProvider.getRegion().getEndpoint(MonitoringClient.SERVICE).orElseThrow(); + MonitoringClient.Builder builder = MonitoringClient.builder(). endpoint(ingestionEndpoint); diff --git a/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMeterRawRegistrySpec.groovy b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMeterRawRegistrySpec.groovy index 7bba5cb1c..2ebe611f4 100644 --- a/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMeterRawRegistrySpec.groovy +++ b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMeterRawRegistrySpec.groovy @@ -1,6 +1,5 @@ package io.micronaut.oraclecloud.monitoring.micrometer -import com.oracle.bmc.monitoring.MonitoringClient import com.oracle.bmc.monitoring.model.Datapoint import io.micrometer.core.instrument.* import io.micronaut.http.client.HttpClientRegistry From 4d422030d3545d4c4fdabbcd7209e650e6aba1b9 Mon Sep 17 00:00:00 2001 From: Nemanja Mikic Date: Thu, 5 Dec 2024 20:19:34 +0100 Subject: [PATCH 2/4] add test and fallback --- .../micrometer/OracleCloudMonitorClientEndpointSpec.groovy | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMonitorClientEndpointSpec.groovy diff --git a/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMonitorClientEndpointSpec.groovy b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMonitorClientEndpointSpec.groovy new file mode 100644 index 000000000..be91605c6 --- /dev/null +++ b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMonitorClientEndpointSpec.groovy @@ -0,0 +1,4 @@ +package io.micronaut.oraclecloud.monitoring.micrometer + +class OracleCloudMonitorClientEndpointSpec { +} From 78f57bd2a09fe47490a593054d62bf930bc46064 Mon Sep 17 00:00:00 2001 From: Nemanja Mikic Date: Thu, 5 Dec 2024 20:21:09 +0100 Subject: [PATCH 3/4] add test and fallback --- .../monitoring/MonitoringIngestionClient.java | 7 ++- ...racleCloudMonitorClientEndpointSpec.groovy | 47 ++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/MonitoringIngestionClient.java b/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/MonitoringIngestionClient.java index 8732a3f33..a0c4c3c98 100644 --- a/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/MonitoringIngestionClient.java +++ b/oraclecloud-micrometer/src/main/java/io/micronaut/oraclecloud/monitoring/MonitoringIngestionClient.java @@ -105,7 +105,12 @@ public MonitoringClient getDelegate() { if (delegate == null) { synchronized (MonitoringIngestionClient.class) { if (delegate == null) { - String ingestionEndpoint = regionProvider.getRegion().getEndpoint(MonitoringClient.SERVICE).orElseThrow(); + if (regionProvider.getRegion() == null) { + throw new IllegalArgumentException("Region is required for the Monitoring Ingestion client"); + } + + String ingestionEndpoint = regionProvider.getRegion().getEndpoint(MonitoringClient.SERVICE) + .orElse(String.format("https://telemetry-ingestion.%s.oraclecloud.com", regionProvider.getRegion().getRegionId())); MonitoringClient.Builder builder = MonitoringClient.builder(). endpoint(ingestionEndpoint); diff --git a/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMonitorClientEndpointSpec.groovy b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMonitorClientEndpointSpec.groovy index be91605c6..4385d400c 100644 --- a/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMonitorClientEndpointSpec.groovy +++ b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMonitorClientEndpointSpec.groovy @@ -1,4 +1,49 @@ package io.micronaut.oraclecloud.monitoring.micrometer -class OracleCloudMonitorClientEndpointSpec { +import com.oracle.bmc.Region +import com.oracle.bmc.auth.RegionProvider +import io.micronaut.context.ApplicationContext +import io.micronaut.context.annotation.BootstrapContextCompatible +import io.micronaut.context.annotation.Primary +import io.micronaut.context.annotation.Property +import io.micronaut.context.annotation.Replaces +import io.micronaut.context.annotation.Requires +import io.micronaut.oraclecloud.monitoring.MonitoringIngestionClient +import io.micronaut.test.extensions.spock.annotation.MicronautTest +import jakarta.inject.Inject +import jakarta.inject.Singleton +import spock.lang.Specification + +@MicronautTest +@Property(name="micronaut.metrics.export.oraclecloud.enabled", value = "false") +@Property(name="spec.name", value = "OracleCloudMonitorClientEndpointSpec") +class OracleCloudMonitorClientEndpointSpec extends Specification { + + @Inject + ApplicationContext context + + def "test oci sdk metrics client filter request returns exception" () { + when: + MonitoringIngestionClient monitoringIngestionClient = context.getBean(MonitoringIngestionClient) + monitoringIngestionClient.getDelegate() + + then: + final IllegalArgumentException exception = thrown() + exception.message == "Region is required for the Monitoring Ingestion client" + + } + + @Singleton + @BootstrapContextCompatible + @Primary + @Replaces(RegionProvider.class) + @Requires(property = "spec.name", value = "OracleCloudMonitorClientEndpointSpec") + static class RegionProviderReplacement implements RegionProvider { + + @Override + Region getRegion() { + return null + } + } + } From 7c0f4b9586d5ccd72d756ee8d4639dcb30d46d42 Mon Sep 17 00:00:00 2001 From: Nemanja Mikic Date: Thu, 5 Dec 2024 21:00:55 +0100 Subject: [PATCH 4/4] disable startApplication in test --- .../micrometer/OracleCloudMonitorClientEndpointSpec.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMonitorClientEndpointSpec.groovy b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMonitorClientEndpointSpec.groovy index 4385d400c..d647e4e01 100644 --- a/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMonitorClientEndpointSpec.groovy +++ b/oraclecloud-micrometer/src/test/groovy/io/micronaut/oraclecloud/monitoring/micrometer/OracleCloudMonitorClientEndpointSpec.groovy @@ -14,7 +14,7 @@ import jakarta.inject.Inject import jakarta.inject.Singleton import spock.lang.Specification -@MicronautTest +@MicronautTest(startApplication = false) @Property(name="micronaut.metrics.export.oraclecloud.enabled", value = "false") @Property(name="spec.name", value = "OracleCloudMonitorClientEndpointSpec") class OracleCloudMonitorClientEndpointSpec extends Specification {