From d168010802e6fa06ab24701e97cd28fbd666479e Mon Sep 17 00:00:00 2001 From: Massimo Pacher Date: Mon, 28 Feb 2022 13:21:48 +0100 Subject: [PATCH] fix(telemetry): fix MetricsPostProcessor dependency injection (#4224) Allow registry implementations to be processed by other BeanPostProcessors Fix https://github.com/armory-plugins/armory-observability-plugin/issues/32 --- .../TaskSchedulerMetricsPostProcessor.java | 2 + .../ThreadPoolMetricsPostProcessor.java | 2 + .../RedisPoolMetricsPostProcessor.java | 2 + .../orca/MeterRegistryProcessorIntTest.java | 65 ++++++++++++++++++ ...terRegistryProcessorTestConfiguration.java | 68 +++++++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 orca-web/src/test/java/com/netflix/spinnaker/orca/MeterRegistryProcessorIntTest.java create mode 100644 orca-web/src/test/java/com/netflix/spinnaker/orca/MeterRegistryProcessorTestConfiguration.java diff --git a/orca-core/src/main/java/com/netflix/spinnaker/orca/telemetry/TaskSchedulerMetricsPostProcessor.java b/orca-core/src/main/java/com/netflix/spinnaker/orca/telemetry/TaskSchedulerMetricsPostProcessor.java index a8c0cc390b..7abfed75ed 100644 --- a/orca-core/src/main/java/com/netflix/spinnaker/orca/telemetry/TaskSchedulerMetricsPostProcessor.java +++ b/orca-core/src/main/java/com/netflix/spinnaker/orca/telemetry/TaskSchedulerMetricsPostProcessor.java @@ -23,6 +23,7 @@ import java.util.function.BiConsumer; import java.util.function.Function; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.stereotype.Component; @@ -31,6 +32,7 @@ public class TaskSchedulerMetricsPostProcessor extends AbstractMetricsPostProcessor { @Autowired + @Lazy public TaskSchedulerMetricsPostProcessor(Registry registry) { super(ThreadPoolTaskScheduler.class, registry); } diff --git a/orca-core/src/main/java/com/netflix/spinnaker/orca/telemetry/ThreadPoolMetricsPostProcessor.java b/orca-core/src/main/java/com/netflix/spinnaker/orca/telemetry/ThreadPoolMetricsPostProcessor.java index f430e3b6d1..810f0f4dcf 100644 --- a/orca-core/src/main/java/com/netflix/spinnaker/orca/telemetry/ThreadPoolMetricsPostProcessor.java +++ b/orca-core/src/main/java/com/netflix/spinnaker/orca/telemetry/ThreadPoolMetricsPostProcessor.java @@ -24,6 +24,7 @@ import java.util.function.BiConsumer; import java.util.function.Function; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component; @@ -32,6 +33,7 @@ public class ThreadPoolMetricsPostProcessor extends AbstractMetricsPostProcessor { @Autowired + @Lazy public ThreadPoolMetricsPostProcessor(Registry registry) { super(ThreadPoolTaskExecutor.class, registry); } diff --git a/orca-redis/src/main/java/com/netflix/spinnaker/orca/telemetry/RedisPoolMetricsPostProcessor.java b/orca-redis/src/main/java/com/netflix/spinnaker/orca/telemetry/RedisPoolMetricsPostProcessor.java index 34dbf60f7f..f13464b515 100644 --- a/orca-redis/src/main/java/com/netflix/spinnaker/orca/telemetry/RedisPoolMetricsPostProcessor.java +++ b/orca-redis/src/main/java/com/netflix/spinnaker/orca/telemetry/RedisPoolMetricsPostProcessor.java @@ -20,6 +20,7 @@ import java.lang.reflect.Field; import org.apache.commons.pool2.impl.GenericObjectPool; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; @@ -29,6 +30,7 @@ public class RedisPoolMetricsPostProcessor extends AbstractMetricsPostProcessor { @Autowired + @Lazy public RedisPoolMetricsPostProcessor(Registry registry) { super(JedisPool.class, registry); } diff --git a/orca-web/src/test/java/com/netflix/spinnaker/orca/MeterRegistryProcessorIntTest.java b/orca-web/src/test/java/com/netflix/spinnaker/orca/MeterRegistryProcessorIntTest.java new file mode 100644 index 0000000000..2d9479195e --- /dev/null +++ b/orca-web/src/test/java/com/netflix/spinnaker/orca/MeterRegistryProcessorIntTest.java @@ -0,0 +1,65 @@ +/* + * Copyright 2019 Google, Inc. + * + * 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 com.netflix.spinnaker.orca; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import io.micrometer.core.instrument.MeterRegistry; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = {Main.class}) +@ContextConfiguration(classes = {MeterRegistryProcessorTestConfiguration.class}) +@TestPropertySource( + properties = {"spring.config.location=classpath:orca-test.yml", "redis.enabled: false"}) +public class MeterRegistryProcessorIntTest { + + @Autowired TestBeanPostProcessor testBeanPostProcessor; + + @Test + public void test() { + assertEquals(true, testBeanPostProcessor.invoked); + } + + /* + Helper bean post processor used as spy + */ + public static class TestBeanPostProcessor implements BeanPostProcessor { + boolean invoked = false; + + public TestBeanPostProcessor() {} + + @Override + public final Object postProcessBeforeInitialization(Object bean, String beanName) + throws BeansException { + + if (bean instanceof MeterRegistry) { + invoked = true; + } + + return bean; + } + } +} diff --git a/orca-web/src/test/java/com/netflix/spinnaker/orca/MeterRegistryProcessorTestConfiguration.java b/orca-web/src/test/java/com/netflix/spinnaker/orca/MeterRegistryProcessorTestConfiguration.java new file mode 100644 index 0000000000..3197e40cf1 --- /dev/null +++ b/orca-web/src/test/java/com/netflix/spinnaker/orca/MeterRegistryProcessorTestConfiguration.java @@ -0,0 +1,68 @@ +/* + * Copyright 2022 Netflix, Inc. + * + * 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 com.netflix.spinnaker.orca; + +import com.netflix.spinnaker.orca.notifications.AlwaysUnlockedNotificationClusterLock; +import com.netflix.spinnaker.orca.notifications.NotificationClusterLock; +import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository; +import com.netflix.spinnaker.orca.pipeline.persistence.InMemoryExecutionRepository; +import com.netflix.spinnaker.q.memory.InMemoryQueue; +import com.netflix.spinnaker.q.metrics.EventPublisher; +import com.netflix.spinnaker.q.metrics.MonitorableQueue; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.composite.CompositeMeterRegistry; +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import java.time.Clock; +import java.time.Duration; +import java.util.Collections; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; + +@TestConfiguration +public class MeterRegistryProcessorTestConfiguration { + + @Bean + NotificationClusterLock notificationClusterLock() { + return new AlwaysUnlockedNotificationClusterLock(); + } + + @Bean + ExecutionRepository executionRepository() { + return new InMemoryExecutionRepository(); + } + + @Bean + @Primary + MonitorableQueue queue(Clock clock, EventPublisher publisher) { + return new InMemoryQueue( + clock, Duration.ofMinutes(1), Collections.emptyList(), false, publisher); + } + + @Bean + MeterRegistry meterRegistry() { + return new CompositeMeterRegistry( + io.micrometer.core.instrument.Clock.SYSTEM, + Collections.singleton(new SimpleMeterRegistry())); + } + + @Bean + BeanPostProcessor testBeanPostProcessor() { + return new MeterRegistryProcessorIntTest.TestBeanPostProcessor(); + } +}