Skip to content

Commit

Permalink
Merge branch 'helidon-2.x' into issue7613
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Feb 28, 2024
2 parents ed39ddf + f5683f4 commit 2274e8f
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
<version.lib.opentracing.tracerresolver>0.1.8</version.lib.opentracing.tracerresolver>
<version.lib.perfmark-api>0.25.0</version.lib.perfmark-api>
<version.lib.persistence-api>2.2.3</version.lib.persistence-api>
<version.lib.postgresql>42.4.3</version.lib.postgresql>
<version.lib.postgresql>42.4.4</version.lib.postgresql>
<version.lib.prometheus>0.9.0</version.lib.prometheus>
<version.lib.slf4j>2.0.9</version.lib.slf4j>
<version.lib.smallrye-openapi>2.0.26</version.lib.smallrye-openapi>
Expand Down
49 changes: 48 additions & 1 deletion integrations/oci/metrics/cdi/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2021, 2023 Oracle and/or its affiliates.
Copyright (c) 2021, 2024 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 @@ -53,4 +53,51 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<!--
Adding the Helidon MP metrics artifact to the test scope would make the CDI extension test susceptible to changes
in the number of automatic metrics registered. The auto-registration does not occur without adding the dep. on
the MP metrics artifact.
So, instead of that, we run surefire twice, once running just the observer priority test with the Helidon
MP metrics library added to just thats execution. Only surefire version 3.2 and later support this feature, so
use a more recent version than the Helidon default.
-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<executions>
<execution>
<id>default-test</id>
<configuration>
<excludes>
<exclude>**/TestObserverPriorities.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>observer-priority-check</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<additionalClasspathDependencies>
<dependency>
<groupId>io.helidon.microprofile.metrics</groupId>
<artifactId>helidon-microprofile-metrics</artifactId>
<version>${helidon.version}</version>
</dependency>
</additionalClasspathDependencies>
<includes>
<include>**/TestObserverPriorities.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2024 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 @@ -30,28 +30,89 @@
import static javax.interceptor.Interceptor.Priority.LIBRARY_BEFORE;

/**
* OCI metrics integration CDI extension.
* CDI bean for preparing OCI metrics integration.
* <p>
* This bean relies on the OCI SDK integration to manufacture a {@link com.oracle.bmc.monitoring.Monitoring} instance.
* It is also extensible in case an upstream library needs to fine-tune the way the OCI metrics integration is set up.
* </p>
*/

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

private Config ociMetricsConfig;
private OciMetricsSupport ociMetricsSupport;

/**
* For CDI use only.
*/
@Deprecated
public OciMetricsBean() {
}

/**
* Returns the config key to use for retrieving OCI metrics settings from the root config.
*
* @return config key for OCI metrics settings
*/
protected String configKey() {
return "ocimetrics";
}

/**
* Returns the builder for constructing a new {@link io.helidon.integrations.oci.metrics.OciMetricsSupport} instance,
* initialized using the config retrieved using the {@link #configKey()} return value and the provided
* {@link com.oracle.bmc.monitoring.Monitoring} instance.
*
* @param rootConfig root {@link io.helidon.config.Config} node
* @param ociMetricsConfig config node for the OCI metrics settings
* @param monitoring monitoring implementation to be used in preparing the {@code OciMetricsSupport.Builder}.
* @return resulting builder
*/
protected OciMetricsSupport.Builder ociMetricsSupportBuilder(Config rootConfig,
Config ociMetricsConfig,
Monitoring monitoring) {
return OciMetricsSupport.builder()
.config(ociMetricsConfig)
.monitoringClient(monitoring);
}

/**
* Returns the OCI metrics config settings previously retrieved from the config root.
*
* @return OCI metrics config node
*/
protected Config ociMetricsConfig() {
return ociMetricsConfig;
}

/**
* Activates OCI metrics support.
*
* @param rootConfig root config node
* @param ociMetricsConfig OCI metrics configuration
* @param builder builder for {@link io.helidon.integrations.oci.metrics.OciMetricsSupport}
*/
protected void activateOciMetricsSupport(Config rootConfig, Config ociMetricsConfig, OciMetricsSupport.Builder builder) {
ociMetricsSupport = builder.build();
RoutingBuilders.create(ociMetricsConfig)
.routingBuilder()
.register(ociMetricsSupport);
}

// 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 ocimetrics = config.get("ocimetrics");
OciMetricsSupport.Builder builder = OciMetricsSupport.builder()
.config(ocimetrics)
.monitoringClient(monitoringClient);
Config rootConfig, Monitoring monitoringClient) {
ociMetricsConfig = rootConfig.get(configKey());
OciMetricsSupport.Builder builder = ociMetricsSupportBuilder(rootConfig, ociMetricsConfig, monitoringClient);
if (builder.enabled()) {
activateOciMetricsSupport(ocimetrics, builder);
activateOciMetricsSupport(rootConfig, ociMetricsConfig, builder);
}
}

void activateOciMetricsSupport(Config ocimetrics, OciMetricsSupport.Builder builder) {
RoutingBuilders.create(ocimetrics)
.routingBuilder()
.register(builder.build());
// For testing
OciMetricsSupport ociMetricsSupport() {
return ociMetricsSupport;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 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 @@ -276,9 +276,9 @@ public void close() throws Exception {
static class MockOciMetricsBean extends OciMetricsBean {
// Override so we can test if this is invoked when enabled or skipped when disabled
@Override
void activateOciMetricsSupport(Config config, OciMetricsSupport.Builder builder) {
protected void activateOciMetricsSupport(Config rootConfig, Config ociMetricsConfig, OciMetricsSupport.Builder builder) {
activateOciMetricsSupportIsInvoked = true;
super.activateOciMetricsSupport(config, builder);
super.activateOciMetricsSupport(rootConfig, ociMetricsConfig, builder);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2024 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 javax.annotation.Priority;
import javax.enterprise.inject.Alternative;
import javax.inject.Singleton;
import javax.interceptor.Interceptor;

import io.helidon.config.Config;
import io.helidon.integrations.oci.metrics.OciMetricsSupport;

import com.oracle.bmc.monitoring.Monitoring;

/**
* Example of an overriding bean implementation to test that overrides work. (See
* {@link io.helidon.integrations.oci.metrics.cdi.TestOverridingBean}.)
*/
@Priority(Interceptor.Priority.LIBRARY_BEFORE)
@Alternative
@Singleton
class OverridingOciMetricsBean extends OciMetricsBean {

@Override
protected String configKey() {
return "oci.metrics";
}

@Override
protected OciMetricsSupport.Builder ociMetricsSupportBuilder(Config rootConfig,
Config ociMetricsConfig,
Monitoring monitoring) {
OciMetricsSupport.Builder result = super.ociMetricsSupportBuilder(rootConfig, rootConfig, monitoring);
// Example using synonyms for two of the config keys.
ociMetricsConfig.get("product").asString().ifPresent(result::namespace);
ociMetricsConfig.get("fleet").asString().ifPresent(result::resourceGroup);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2024 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 java.lang.reflect.Method;
import java.util.Arrays;

import javax.annotation.Priority;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.BeanManager;
import javax.ws.rs.Priorities;

import io.helidon.config.Config;
import io.helidon.microprofile.server.ServerCdiExtension;

import com.oracle.bmc.monitoring.Monitoring;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;

class TestObserverPriorities {

@Test
void compareObserverAgainstMetricsCdiExtensionObserver() throws NoSuchMethodException, ClassNotFoundException {
Method ourObserverMethod = OciMetricsBean.class.getDeclaredMethod("registerOciMetrics",
Object.class,
Config.class,
Monitoring.class);
Class<?> metricsCdiExtension = Class.forName("io.helidon.microprofile.metrics.MetricsCdiExtension");
Method metricsCdiExtensionRegisteringMethod = metricsCdiExtension.getDeclaredMethod("registerService",
Object.class,
BeanManager.class,
ServerCdiExtension.class);
assertThat("Observer priority compared to metrics CDI extension observer priority",
priority(ourObserverMethod),
is(greaterThan(priority(metricsCdiExtensionRegisteringMethod))));
}

private static int priority(Method method) {
return Arrays.stream(method.getParameters())
.filter(p -> p.isAnnotationPresent(Observes.class))
.filter(p -> p.isAnnotationPresent(Priority.class))
.map(p -> p.getAnnotation(Priority.class).value())
.findFirst()
.orElse(Priorities.USER);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2024 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 java.lang.reflect.Field;

import javax.inject.Inject;

import io.helidon.integrations.oci.metrics.OciMetricsSupport;
import io.helidon.microprofile.tests.junit5.AddBean;
import io.helidon.microprofile.tests.junit5.AddConfig;
import io.helidon.microprofile.tests.junit5.HelidonTest;

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;

@HelidonTest
@AddBean(OverridingOciMetricsBean.class)
@AddConfig(key = "oci.metrics.product", value = TestOverridingBean.PRODUCT)
@AddConfig(key = "oci.metrics.fleet", value = TestOverridingBean.FLEET)
@AddBean(OciMetricsCdiExtensionTest.MockMonitoring.class)
class TestOverridingBean {

static final String PRODUCT = "overriding-product-name";
static final String FLEET = "overriding-fleet";

// The injected bean should be the test one which overrides the default implementation.
@Inject
private OciMetricsBean bean;

@Test
void checkOverriding() throws NoSuchFieldException, IllegalAccessException {
assertThat("Config key", bean.configKey(), is(equalTo("oci.metrics")));
assertThat("Injected bean", bean, instanceOf(OverridingOciMetricsBean.class));
OciMetricsSupport ociMetricsSupport = bean.ociMetricsSupport();

String namespace = getStringField("namespace", ociMetricsSupport);
String resourceGroup = getStringField("resourceGroup", ociMetricsSupport);

assertThat("Effective namespace", namespace, is(equalTo(PRODUCT)));
assertThat("Effective namespace", resourceGroup, is(equalTo(FLEET)));
}

private String getStringField(String fieldName, OciMetricsSupport ociMetricsSupport)
throws NoSuchFieldException, IllegalAccessException {
// Except for testing, there is no need for OciMetricsSupport to expose methods returning the namespace and resource
// group so just use reflection rather than add those methods. It's only a test so using reflection is not so bad.
Field field = OciMetricsSupport.class.getDeclaredField(fieldName);
field.setAccessible(true);
return (String) field.get(ociMetricsSupport);
}
}

0 comments on commit 2274e8f

Please sign in to comment.