Skip to content

Commit 5bb2f53

Browse files
authored
Register OciMetricsSupport service only when enable flag is set to true (helidon-io#6053)
Changes include: 1. Add a getter enabled method in OciMetricsSupport.Builder to provide boolean value indicating whether OciMetricsSupport service will be activated or not. 2. In OciMetricsBean, use the new enabled method to determine whether OciMetricsSupport service will be registered in the default routing or not. 3. Refactor routing registration of OciMetricsSupport, so it can be properly unit tested to verify if it is skipped when disabled or invoked when enabled. 4. Add unit test validation when enable flag is set to true, false, or not set (which defaults to true).
1 parent d49dab7 commit 5bb2f53

4 files changed

Lines changed: 82 additions & 26 deletions

File tree

integrations/oci/metrics/cdi/src/main/java/io/helidon/integrations/oci/metrics/cdi/OciMetricsBean.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ public class OciMetricsBean {
3939
// Make Priority higher than MetricsCdiExtension so this will only start after MetricsCdiExtension has completed.
4040
void registerOciMetrics(@Observes @Priority(LIBRARY_BEFORE + 20) @Initialized(ApplicationScoped.class) Object ignore,
4141
Config config, Monitoring monitoringClient) {
42-
Config helidonConfig = config.get("ocimetrics");
43-
if (helidonConfig.exists()) {
44-
OciMetricsSupport.Builder builder = OciMetricsSupport.builder()
45-
.config(helidonConfig)
46-
.monitoringClient(monitoringClient);
47-
48-
RoutingBuilders.create(helidonConfig)
49-
.routingBuilder()
50-
.register(builder.build());
42+
Config ocimetrics = config.get("ocimetrics");
43+
OciMetricsSupport.Builder builder = OciMetricsSupport.builder()
44+
.config(ocimetrics)
45+
.monitoringClient(monitoringClient);
46+
if (builder.enabled()) {
47+
activateOciMetricsSupport(ocimetrics, builder);
5148
}
5249
}
50+
51+
void activateOciMetricsSupport(Config ocimetrics, OciMetricsSupport.Builder builder) {
52+
RoutingBuilders.create(ocimetrics)
53+
.routingBuilder()
54+
.register(builder.build());
55+
}
5356
}

integrations/oci/metrics/cdi/src/test/java/io/helidon/integrations/oci/metrics/cdi/OciMetricsCdiExtensionTest.java

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
*/
1616
package io.helidon.integrations.oci.metrics.cdi;
1717

18-
import jakarta.annotation.Priority;
19-
import jakarta.enterprise.inject.Alternative;
20-
import static jakarta.interceptor.Interceptor.Priority.APPLICATION;
21-
2218
import com.oracle.bmc.Region;
2319
import com.oracle.bmc.monitoring.Monitoring;
2420
import com.oracle.bmc.monitoring.MonitoringPaginators;
@@ -28,6 +24,8 @@
2824
import com.oracle.bmc.monitoring.requests.*;
2925
import com.oracle.bmc.monitoring.responses.*;
3026

27+
import io.helidon.config.Config;
28+
import io.helidon.integrations.oci.metrics.OciMetricsSupport;
3129
import io.helidon.metrics.api.RegistryFactory;
3230
import io.helidon.microprofile.config.ConfigCdiExtension;
3331
import io.helidon.microprofile.server.ServerCdiExtension;
@@ -40,6 +38,7 @@
4038

4139
import org.eclipse.microprofile.metrics.MetricRegistry;
4240

41+
import org.junit.jupiter.api.AfterEach;
4342
import org.junit.jupiter.api.Test;
4443

4544
import java.util.concurrent.CountDownLatch;
@@ -51,6 +50,7 @@
5150

5251
@HelidonTest(resetPerTest = true)
5352
@AddBean(OciMetricsCdiExtensionTest.MockMonitoring.class)
53+
@AddBean(OciMetricsCdiExtensionTest.MockOciMetricsBean.class)
5454
@DisableDiscovery
5555
// Helidon MP Extensions
5656
@AddExtension(ServerCdiExtension.class)
@@ -76,26 +76,56 @@ public class OciMetricsCdiExtensionTest {
7676
// Use countDownLatch1 to signal the test that results to be asserted has been retrieved
7777
private static CountDownLatch countDownLatch1 = new CountDownLatch(1);
7878
private static PostMetricDataDetails postMetricDataDetails;
79+
private static boolean activateOciMetricsSupportIsInvoked;
80+
81+
@AfterEach
82+
private void resetState() {
83+
postMetricDataDetails = null;
84+
activateOciMetricsSupportIsInvoked = false;
85+
countDownLatch1 = new CountDownLatch(1);
86+
}
87+
88+
@Test
89+
@AddConfig(key = "ocimetrics.enabled", value = "true")
90+
public void testEnableOciMetrics() throws InterruptedException {
91+
validateOciMetricsSupport(true);
92+
}
93+
94+
@Test
95+
public void testEnableOciMetricsWithoutConfig() throws InterruptedException {
96+
validateOciMetricsSupport(true);
97+
}
7998

8099
@Test
81-
public void testRegisterOciMetrics() throws InterruptedException {
100+
@AddConfig(key = "ocimetrics.enabled", value = "false")
101+
public void testDisableOciMetrics() throws InterruptedException {
102+
validateOciMetricsSupport(false);
103+
}
104+
105+
private void validateOciMetricsSupport(boolean enabled) throws InterruptedException {
82106
baseMetricRegistry.counter("baseDummyCounter").inc();
83107
vendorMetricRegistry.counter("vendorDummyCounter").inc();
84108
appMetricRegistry.counter("appDummyCounter").inc();
85109
// Wait for signal from metric update that testMetricCount has been retrieved
86-
countDownLatch1.await(10, TimeUnit.SECONDS);
87-
88-
assertThat(testMetricCount, is(equalTo(3)));
89-
90-
MetricDataDetails metricDataDetails = postMetricDataDetails.getMetricData().get(0);
91-
assertThat(metricDataDetails.getCompartmentId(),
92-
is(equalTo(OciMetricsCdiExtensionTest.MetricDataDetailsOCIParams.compartmentId)));
93-
assertThat(metricDataDetails.getNamespace(), is(equalTo(MetricDataDetailsOCIParams.namespace)));
94-
assertThat(metricDataDetails.getResourceGroup(), is(equalTo(MetricDataDetailsOCIParams.resourceGroup)));
110+
countDownLatch1.await(3, TimeUnit.SECONDS);
111+
112+
if (enabled) {
113+
assertThat(activateOciMetricsSupportIsInvoked, is(true));
114+
assertThat(testMetricCount, is(3));
115+
116+
MetricDataDetails metricDataDetails = postMetricDataDetails.getMetricData().get(0);
117+
assertThat(metricDataDetails.getCompartmentId(),
118+
is(MetricDataDetailsOCIParams.compartmentId));
119+
assertThat(metricDataDetails.getNamespace(), is(MetricDataDetailsOCIParams.namespace));
120+
assertThat(metricDataDetails.getResourceGroup(), is(MetricDataDetailsOCIParams.resourceGroup));
121+
} else {
122+
assertThat(activateOciMetricsSupportIsInvoked, is(false));
123+
assertThat(testMetricCount, is(0));
124+
// validate that OCI post metric is never called
125+
assertThat(postMetricDataDetails, is(equalTo(null)));
126+
}
95127
}
96128

97-
@Alternative
98-
@Priority(APPLICATION + 1)
99129
static class MockMonitoring implements Monitoring {
100130
@Override
101131
public void setEndpoint(String s) {}
@@ -179,6 +209,15 @@ public SummarizeMetricsDataResponse summarizeMetricsData(SummarizeMetricsDataReq
179209
public void close() throws Exception {}
180210
}
181211

212+
static class MockOciMetricsBean extends OciMetricsBean {
213+
// Override so we can test if this is invoked when enabled or skipped when disabled
214+
@Override
215+
void activateOciMetricsSupport(Config config, OciMetricsSupport.Builder builder) {
216+
activateOciMetricsSupportIsInvoked = true;
217+
super.activateOciMetricsSupport(config, builder);
218+
}
219+
}
220+
182221
public interface MetricDataDetailsOCIParams {
183222
String compartmentId = "dummy.compartmentId";
184223
String namespace = "dummy-namespace";

integrations/oci/metrics/metrics/src/main/java/io/helidon/integrations/oci/metrics/OciMetricsSupport.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,16 @@ public Builder monitoringClient(Monitoring monitoringClient) {
525525
return this;
526526
}
527527

528+
/**
529+
* Returns boolean value to indicate whether OciMetricsSupport service will be activated or not.
530+
*
531+
* @return {@code true} if OciMetricsSupport service will be activated or
532+
* {@code false} if it not
533+
*/
534+
public boolean enabled() {
535+
return this.enabled;
536+
}
537+
528538
private static Type[] getAllMetricScopes() {
529539
return new ArrayList<>(SCOPE_TYPES.values()).toArray(new Type[SCOPE_TYPES.size()]);
530540
}

integrations/oci/metrics/metrics/src/test/java/io/helidon/integrations/oci/metrics/OciMetricsSupportTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ public void testMetricUpdate() throws InterruptedException {
132132
.initialDelay(1L)
133133
.delay(2L)
134134
.descriptionEnabled(false)
135-
.monitoringClient(monitoringClient);
135+
.monitoringClient(monitoringClient)
136+
.enabled(true);
136137

137138
HttpRouting routing = createRouting(ociMetricsSupportBuilder);
138139

@@ -143,6 +144,7 @@ public void testMetricUpdate() throws InterruptedException {
143144
countDownLatch1.await(10, java.util.concurrent.TimeUnit.SECONDS);
144145

145146
// Test the 1st and 2nd metric counter updates
147+
assertThat(ociMetricsSupportBuilder.enabled(), is(true));
146148
assertThat(testMetricUpdateCounterValue[0].intValue(), is(equalTo(1)));
147149
assertThat(testMetricUpdateCounterValue[1].intValue(), is(equalTo(2)));
148150

@@ -187,6 +189,7 @@ public void testEndpoint() throws InterruptedException {
187189
// Wait for metrics to be posted
188190
countDownLatch1.await(10, java.util.concurrent.TimeUnit.SECONDS);
189191

192+
assertThat(ociMetricsSupportBuilder.enabled(), is(true));
190193
// Verify that telemetry-ingestion endpoint is properly set during postin
191194
assertThat(postingEndPoint, startsWith("https://telemetry-ingestion."));
192195
// Verify that original endpoint is restored after metric posting
@@ -349,6 +352,7 @@ public void testDisableMetrics() {
349352
delay(1000L);
350353

351354
webServer.stop();
355+
assertThat(ociMetricsSupportBuilder.enabled(), is(false));
352356
// metric count should remain 0 as metrics is disabled
353357
assertThat(testMetricCount, is(equalTo(0)));
354358
}

0 commit comments

Comments
 (0)