Back to Repositories

Testing Prometheus Histogram Metrics Registration in HikariCP

This test suite validates the Prometheus histogram metrics tracking functionality in HikariCP’s connection pool monitoring system. It ensures proper registration and tracking of connection pool metrics through Prometheus collectors while verifying both custom and default registry implementations.

Test Coverage Overview

The test suite provides comprehensive coverage of Prometheus metrics registration scenarios for HikariCP connection pools.

Key areas tested include:
  • Custom collector registry registration
  • Default registry implementation
  • Verification of essential connection pool metrics
  • Metric presence and absence validation

Implementation Analysis

The testing approach utilizes JUnit to validate Prometheus metric collection integration with HikariCP. It implements systematic verification of metric registration across different collector registries, using mock PoolStats for controlled testing environments.

Key patterns include:
  • Registry isolation testing
  • Metric presence verification
  • Clean test environment maintenance

Technical Details

Testing tools and components:
  • JUnit test framework
  • Prometheus CollectorRegistry
  • Custom PoolStats implementation
  • Metric validation utilities
  • Connection pool metric assertions

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, thorough metric verification, and clean test environment management.

Notable practices:
  • Proper test cleanup with @After annotations
  • Comprehensive metric verification
  • Clear test method naming
  • Reusable assertion methods

brettwooldridge/hikaricp

src/test/java/com/zaxxer/hikari/metrics/prometheus/PrometheusHistogramMetricsTrackerFactoryTest.java

            
package com.zaxxer.hikari.metrics.prometheus;

import com.zaxxer.hikari.metrics.PoolStats;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import org.junit.After;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class PrometheusHistogramMetricsTrackerFactoryTest {

   @Test
   public void registersToProvidedCollectorRegistry() {
      CollectorRegistry collectorRegistry = new CollectorRegistry();
      PrometheusHistogramMetricsTrackerFactory factory =
         new PrometheusHistogramMetricsTrackerFactory(collectorRegistry);
      factory.create("testpool-1", poolStats());
      assertHikariMetricsAreNotPresent(CollectorRegistry.defaultRegistry);
      assertHikariMetricsArePresent(collectorRegistry);
   }

   @Test
   public void registersToDefaultCollectorRegistry() {
      PrometheusHistogramMetricsTrackerFactory factory = new PrometheusHistogramMetricsTrackerFactory();
      factory.create("testpool-2", poolStats());
      assertHikariMetricsArePresent(CollectorRegistry.defaultRegistry);
   }

   @After
   public void clearCollectorRegistry(){
      CollectorRegistry.defaultRegistry.clear();
   }

   private void assertHikariMetricsArePresent(CollectorRegistry collectorRegistry) {
      List<String> registeredMetrics = toMetricNames(collectorRegistry.metricFamilySamples());
      assertTrue(registeredMetrics.contains("hikaricp_active_connections"));
      assertTrue(registeredMetrics.contains("hikaricp_idle_connections"));
      assertTrue(registeredMetrics.contains("hikaricp_pending_threads"));
      assertTrue(registeredMetrics.contains("hikaricp_connections"));
      assertTrue(registeredMetrics.contains("hikaricp_max_connections"));
      assertTrue(registeredMetrics.contains("hikaricp_min_connections"));
   }

   private void assertHikariMetricsAreNotPresent(CollectorRegistry collectorRegistry) {
      List<String> registeredMetrics = toMetricNames(collectorRegistry.metricFamilySamples());
      assertFalse(registeredMetrics.contains("hikaricp_active_connections"));
      assertFalse(registeredMetrics.contains("hikaricp_idle_connections"));
      assertFalse(registeredMetrics.contains("hikaricp_pending_threads"));
      assertFalse(registeredMetrics.contains("hikaricp_connections"));
      assertFalse(registeredMetrics.contains("hikaricp_max_connections"));
      assertFalse(registeredMetrics.contains("hikaricp_min_connections"));
   }

   private List<String> toMetricNames(Enumeration<Collector.MetricFamilySamples> enumeration) {
      List<String> list = new ArrayList<>();
      while (enumeration.hasMoreElements()) {
         list.add(enumeration.nextElement().name);
      }
      return list;
   }

   private PoolStats poolStats() {
      return new PoolStats(0) {
         @Override
         protected void update() {
            // do nothing
         }
      };
   }

}