Back to Repositories

Testing Prometheus Metrics Registration and Tracking in HikariCP

This test suite validates the Prometheus metrics integration with HikariCP, ensuring proper registration and tracking of connection pool metrics. It verifies both default and custom collector registry implementations while testing the complete set of HikariCP metrics exposure.

Test Coverage Overview

The test suite provides comprehensive coverage of Prometheus metrics tracking functionality in HikariCP.

Key areas tested include:
  • Custom collector registry registration
  • Default registry integration
  • Complete metrics set validation
  • Connection pool statistics tracking
Edge cases cover registry isolation and metric presence verification.

Implementation Analysis

The testing approach utilizes JUnit’s framework for systematic verification of metrics tracking behavior. The implementation employs mock objects (StubPoolStats) and follows a clear pattern of registry setup, metric creation, and validation.

Technical patterns include:
  • Registry isolation testing
  • Metric name verification
  • Mock statistics injection

Technical Details

Testing tools and components:
  • JUnit 4 testing framework
  • Prometheus Client Java library
  • CollectorRegistry for metrics management
  • Custom assertion methods for metric validation
  • Mock pool statistics implementation

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through clear organization and thorough validation.

Notable practices include:
  • Proper test cleanup using @After annotations
  • Dedicated helper methods for assertions
  • Comprehensive metric set verification
  • Clear separation of test scenarios
  • Effective use of mock objects

brettwooldridge/hikaricp

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

            
package com.zaxxer.hikari.metrics.prometheus;

import com.zaxxer.hikari.mocks.StubPoolStats;
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 PrometheusMetricsTrackerFactoryTest
{

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

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

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

   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;
   }
}