Back to Repositories

Testing Prometheus Metrics Configuration Integration in Conductor OSS

This test suite validates the Prometheus metrics configuration in Conductor OSS, focusing on the integration between Micrometer and Spectator registries. It ensures proper setup and registration of metrics collectors for monitoring system performance.

Test Coverage Overview

The test suite provides comprehensive coverage of the Prometheus metrics configuration functionality.

Key areas tested include:
  • Verification of global registry configuration
  • Integration between Micrometer and Spectator registries
  • Proper initialization of metrics collectors
  • Configuration property handling for Prometheus metrics enablement

Implementation Analysis

The testing approach utilizes Spring Boot’s testing framework with JUnit4 integration. It employs reflection to access private registry fields and verify internal configurations.

Notable patterns include:
  • Use of @RunWith(SpringRunner.class) for Spring context testing
  • Custom TestConfiguration for registry bean injection
  • Property source configuration for metrics enablement

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Spring Boot Test context
  • Micrometer Core for metrics registry
  • SimpleMeterRegistry for testing
  • Spectator API for Netflix metrics
  • Custom TestPropertySource configuration

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper separation of test configuration using @TestConfiguration
  • Clear test method naming and purpose
  • Appropriate use of assertions for validation
  • Clean handling of reflection-based testing
  • Effective use of Spring testing annotations

conductor-oss/conductor

metrics/src/test/java/com/netflix/conductor/contribs/metrics/PrometheusMetricsConfigurationTest.java

            
/*
 * Copyright 2023 Conductor Authors.
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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.conductor.contribs.metrics;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Spectator;
import com.netflix.spectator.micrometer.MicrometerRegistry;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;

import static org.junit.Assert.assertTrue;

@RunWith(SpringRunner.class)
@Import({PrometheusMetricsConfiguration.class})
@TestPropertySource(properties = {"conductor.metrics-prometheus.enabled=true"})
public class PrometheusMetricsConfigurationTest {

    @SuppressWarnings("unchecked")
    @Test
    public void testCollector() throws IllegalAccessException {
        final Optional<Field> registries =
                Arrays.stream(Spectator.globalRegistry().getClass().getDeclaredFields())
                        .filter(f -> f.getName().equals("registries"))
                        .findFirst();
        assertTrue(registries.isPresent());
        registries.get().setAccessible(true);

        List<Registry> meters = (List<Registry>) registries.get().get(Spectator.globalRegistry());
        assertTrue(meters.size() > 0);
        Optional<Registry> microMeterReg =
                meters.stream()
                        .filter(r -> r.getClass().equals(MicrometerRegistry.class))
                        .findFirst();
        assertTrue(microMeterReg.isPresent());
    }

    @TestConfiguration
    public static class TestConfig {

        /**
         * This bean will be injected in PrometheusMetricsConfiguration, which wraps it with a
         * MicrometerRegistry, and appends it to the global registry.
         *
         * @return a Prometheus registry instance
         */
        @Bean
        @Primary
        public MeterRegistry meterRegistry() {
            return new SimpleMeterRegistry();
        }
    }
}