Skip to content

Commit

Permalink
Add Helidon Metrics integration with OCI (#5829)
Browse files Browse the repository at this point in the history
* Add Helidon Metrics integration with OCI

The change includes the following:
1. Port of PR #4003 that adds the Helidon Metrics to OCI integration
2. Port of PR #4897 that fixes race condition in the unit test
3. Adjust code to deal with MP metrics API changes
4. Change endpoint to the ingestion endpoint when posting the metrics as this is not handled anymore by the OCI SDK integration due to changes in the OCI Java SDK v3.
5. Change MonitoringClient.class to Monitoring.class for mocking using Mockito in the unit test as the OCI Java SDK v3 converted some of the methods in MonitoringClient as Final making them difficult to mock.
6. Trim the OCI Metadata value which contains the metric description if the value exceeds 256 characters, otherwise it will fail.
7. OCI Monitoring service only allows a maximum of 50 metrics per posting, hence additional configuration parameters were added to control sending metrics in batches. The configuration parameters are:
   a. batchSize - Maximum no. of metrics to send in a batch. Defaults to 50 which is what OCI allows
   b. batchDelay - Interval between batch posting
   For example if there are 51 metrics and batchSize is set to 25 and batchDelay to 5 seconds, the Helidon metric integration module will divide the posting to 3 batches sending 25 metrics on the 1st and 2nd batches and 1 metric on the 3rd batch with 5 seconds interval between batch posting.
8. Refactor OciMetricsCdiExtension to add a new bean (OciMetricsBean) to handle the Observer method which will inject Monitoring. Previous code of OciMetricsCdiExtension cannot independently handle instantiation of Monitoring client via CDI.
9. Add unit tests to verify batch posting feature and the use of ingestion endpoint.
10. Add io.helidon.config.Config as parameter in OCIMetricsBean's Observer method so it can be injected
11. Various changes based on review feedback to fix dependencies, remove use of stream in list, execute rule.onNewWebserver only if enabled, add default value on @ConfigProperty and validate builder methods' parameters are not null
  • Loading branch information
klustria authored Jan 13, 2023
1 parent a52116c commit 34842fa
Show file tree
Hide file tree
Showing 17 changed files with 2,028 additions and 2 deletions.
12 changes: 11 additions & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2017, 2022 Oracle and/or its affiliates.
Copyright (c) 2017, 2023 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -968,6 +968,16 @@
<artifactId>helidon-integrations-oci-sdk-cdi</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.integrations.oci.metrics</groupId>
<artifactId>helidon-integrations-oci-metrics</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.integrations.oci.metrics</groupId>
<artifactId>helidon-integrations-oci-metrics-cdi</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.integrations.vault</groupId>
<artifactId>helidon-integrations-vault</artifactId>
Expand Down
57 changes: 57 additions & 0 deletions integrations/oci/metrics/cdi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2021, 2023 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.integrations.oci.metrics</groupId>
<artifactId>helidon-integrations-oci-metrics-project</artifactId>
<version>3.1.1-SNAPSHOT</version>
</parent>
<artifactId>helidon-integrations-oci-metrics-cdi</artifactId>
<name>Helidon Integrations OCI Metrics CDI</name>
<description>Helidon Metrics to OCI via CDI</description>

<dependencies>
<dependency>
<groupId>io.helidon.integrations.oci.metrics</groupId>
<artifactId>helidon-integrations-oci-metrics</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.server</groupId>
<artifactId>helidon-microprofile-server</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.integrations.oci.metrics.cdi;

import io.helidon.config.Config;
import io.helidon.integrations.oci.metrics.OciMetricsSupport;
import io.helidon.microprofile.server.RoutingBuilders;

import com.oracle.bmc.monitoring.Monitoring;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Initialized;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Singleton;

import static jakarta.interceptor.Interceptor.Priority.LIBRARY_BEFORE;

/**
* OCI metrics integration CDI extension.
*/

// This bean is added to handle injection on the ObserverMethod as it does not work on an Extension class.
@Singleton
public class OciMetricsBean {

// Make Priority higher than MetricsCdiExtension so this will only start after MetricsCdiExtension has completed.
void registerOciMetrics(@Observes @Priority(LIBRARY_BEFORE + 20) @Initialized(ApplicationScoped.class) Object ignore,
Config config, Monitoring monitoringClient) {
Config helidonConfig = config.get("ocimetrics");
if (helidonConfig.exists()) {
OciMetricsSupport.Builder builder = OciMetricsSupport.builder()
.config(helidonConfig)
.monitoringClient(monitoringClient);

RoutingBuilders.create(helidonConfig)
.routingBuilder()
.register(builder.build());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.integrations.oci.metrics.cdi;

import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.spi.BeforeBeanDiscovery;
import jakarta.enterprise.inject.spi.Extension;

/**
* OCI metrics integration CDI extension.
*/
public class OciMetricsCdiExtension implements Extension {

// A new bean is added to handle the Observer Method as injection does not work here
void addOciMetricsBean(@Observes BeforeBeanDiscovery event) {
event.addAnnotatedType(OciMetricsBean.class, OciMetricsBean.class.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Integrating with OCI Metrics Using CDI.
*/
package io.helidon.integrations.oci.metrics.cdi;
34 changes: 34 additions & 0 deletions integrations/oci/metrics/cdi/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Integrating with OCI Metrics Using CDI.
*/
module io.helidon.integrations.oci.metrics.cdi {
requires io.helidon.config.mp;
requires io.helidon.integrations.oci.metrics;
requires io.helidon.microprofile.config;
requires io.helidon.microprofile.server;

requires oci.java.sdk.monitoring;

requires jakarta.interceptor.api;
provides jakarta.enterprise.inject.spi.Extension with io.helidon.integrations.oci.metrics.cdi.OciMetricsCdiExtension;

opens io.helidon.integrations.oci.metrics.cdi to weld.core.impl, io.helidon.microprofile.cdi;

exports io.helidon.integrations.oci.metrics.cdi;
}
Loading

0 comments on commit 34842fa

Please sign in to comment.