SHARE
TWEET

Untitled

a guest Jun 25th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.vlkan;
  2.  
  3. import com.zaxxer.hikari.HikariConfig;
  4. import com.zaxxer.hikari.HikariDataSource;
  5. import com.zaxxer.hikari.metrics.prometheus.PrometheusMetricsTrackerFactory;
  6. import io.micrometer.core.instrument.Clock;
  7. import io.micrometer.core.instrument.MeterRegistry;
  8. import io.micrometer.prometheus.PrometheusConfig;
  9. import io.micrometer.prometheus.PrometheusMeterRegistry;
  10. import io.prometheus.client.CollectorRegistry;
  11.  
  12. import java.sql.Connection;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. import java.util.Random;
  17.  
  18. public enum HikaricpMicrometerBug {;
  19.  
  20.     public static void main(String[] args) throws SQLException {
  21.  
  22.         // Tickle HikariCP and feed statistics to a MeterRegistry.
  23.         PrometheusMeterRegistry meterRegistry1 = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
  24.         tickleStatistics(meterRegistry1);
  25.         scrapeStatistics(meterRegistry1);
  26.  
  27.         /*
  28.          * hitCount: 1825
  29.          * hikaricp_connections_acquire_seconds_count{pool="test",} 1.0
  30.          * hikaricp_connections_acquire_seconds_sum{pool="test",} 8.0015E-5
  31.          * hikaricp_connections_acquire_seconds_max{pool="test",} 8.0015E-5
  32.          * hikaricp_connections{pool="test",} 0.0
  33.          * hikaricp_connections_usage_seconds_count{pool="test",} 1.0
  34.          * hikaricp_connections_usage_seconds_sum{pool="test",} 1.083
  35.          * hikaricp_connections_usage_seconds_max{pool="test",} 1.083
  36.          * hikaricp_connections_creation_seconds_max{pool="test",} 0.0
  37.          * hikaricp_connections_creation_seconds_count{pool="test",} 0.0
  38.          * hikaricp_connections_creation_seconds_sum{pool="test",} 0.0
  39.          * hikaricp_connections_active{pool="test",} 0.0
  40.          * hikaricp_connections_timeout_total{pool="test",} 0.0
  41.          * hikaricp_connections_idle{pool="test",} 0.0
  42.          * hikaricp_connections_pending{pool="test",} 0.0
  43.          * hikaricp_connections_min{pool="test",} 1.0
  44.          * hikaricp_connections_max{pool="test",} 1.0
  45.          */
  46.  
  47.         // Tickle HikariCP and feed statistics to a CollectorRegistry.
  48.         CollectorRegistry collectorRegistry = new CollectorRegistry();
  49.         PrometheusMeterRegistry meterRegistry2 = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT, collectorRegistry, Clock.SYSTEM);
  50.         tickleStatistics(collectorRegistry);
  51.         scrapeStatistics(meterRegistry2);
  52.  
  53.         /*
  54.          * hitCount: 1825
  55.          * hikaricp_active_connections{pool="test",} 0.0
  56.          * hikaricp_idle_connections{pool="test",} 0.0
  57.          * hikaricp_pending_threads{pool="test",} 0.0
  58.          * hikaricp_connections{pool="test",} 0.0
  59.          * hikaricp_max_connections{pool="test",} 1.0
  60.          * hikaricp_min_connections{pool="test",} 1.0
  61.          * hikaricp_connection_creation_millis{pool="test",quantile="0.5",} NaN
  62.          * hikaricp_connection_creation_millis{pool="test",quantile="0.95",} NaN
  63.          * hikaricp_connection_creation_millis{pool="test",quantile="0.99",} NaN
  64.          * hikaricp_connection_creation_millis_count{pool="test",} 0.0
  65.          * hikaricp_connection_creation_millis_sum{pool="test",} 0.0
  66.          * hikaricp_connection_acquired_nanos{pool="test",quantile="0.5",} 17965.0
  67.          * hikaricp_connection_acquired_nanos{pool="test",quantile="0.95",} 17965.0
  68.          * hikaricp_connection_acquired_nanos{pool="test",quantile="0.99",} 17965.0
  69.          * hikaricp_connection_acquired_nanos_count{pool="test",} 1.0
  70.          * hikaricp_connection_acquired_nanos_sum{pool="test",} 17965.0
  71.          * hikaricp_connection_usage_millis{pool="test",quantile="0.5",} 678.0
  72.          * hikaricp_connection_usage_millis{pool="test",quantile="0.95",} 678.0
  73.          * hikaricp_connection_usage_millis{pool="test",quantile="0.99",} 678.0
  74.          * hikaricp_connection_usage_millis_count{pool="test",} 1.0
  75.          * hikaricp_connection_usage_millis_sum{pool="test",} 678.0
  76.          * hikaricp_connection_timeout_total{pool="test",} 0.0
  77.          */
  78.  
  79.     }
  80.  
  81.     private static void tickleStatistics(Object meterOrCollectorRegistry) throws SQLException {
  82.         try (HikariDataSource dataSource = createHikariDataSource(meterOrCollectorRegistry)) {
  83.             try (Connection connection = dataSource.getConnection()) {
  84.  
  85.                 // Create table.
  86.                 try (PreparedStatement statement = connection.prepareStatement("CREATE TABLE number (value INTEGER)")) {
  87.                     statement.execute();
  88.                 }
  89.  
  90.                 // Populate numbers.
  91.                 Random random = new Random(0);
  92.                 int maxNumber = 5_000;
  93.                 int numberCount = 1_000;
  94.                 try (PreparedStatement statement = connection.prepareStatement("INSERT INTO number (value) VALUES (?)")) {
  95.                     for (int numberIndex = 0; numberIndex < numberCount; numberIndex++) {
  96.                         int number = random.nextInt(maxNumber);
  97.                         statement.setInt(1, number);
  98.                         statement.addBatch();
  99.                     }
  100.                     statement.executeBatch();
  101.                 }
  102.  
  103.                 // Query numbers.
  104.                 int queryCount = 10_000;
  105.                 int hitCount = 0;
  106.                 for (int queryIndex = 0; queryIndex < queryCount; queryIndex++) {
  107.                     int number = random.nextInt(maxNumber);
  108.                     try (PreparedStatement statement = connection.prepareStatement("SELECT 1 FROM number WHERE value = ? LIMIT 1")) {
  109.                         statement.setInt(1, number);
  110.                         try (ResultSet resultSet = statement.executeQuery()) {
  111.                             if (resultSet.next()) {
  112.                                 hitCount++;
  113.                             }
  114.                         }
  115.                     }
  116.                 }
  117.                 System.out.format("hitCount: %d%n", hitCount);
  118.  
  119.                 // Commit changes.
  120.                 connection.commit();
  121.  
  122.             }
  123.         }
  124.     }
  125.  
  126.     private static HikariDataSource createHikariDataSource(Object meterOrCollectorRegistry) {
  127.         HikariConfig hikariConfig = createHikariConfig(meterOrCollectorRegistry);
  128.         return new HikariDataSource(hikariConfig);
  129.     }
  130.  
  131.     private static HikariConfig createHikariConfig(Object meterOrCollectorRegistry) {
  132.  
  133.         // Create base HikariCP configuration.
  134.         HikariConfig hikariConfig = new HikariConfig();
  135.         hikariConfig.setPoolName("test");
  136.         hikariConfig.setDriverClassName("org.h2.Driver");
  137.         hikariConfig.setJdbcUrl("jdbc:h2:mem:test");
  138.         hikariConfig.setUsername("sa");
  139.         hikariConfig.setPassword("");
  140.         hikariConfig.setAutoCommit(false);
  141.         hikariConfig.setMinimumIdle(1);
  142.         hikariConfig.setMaximumPoolSize(1);
  143.         hikariConfig.setConnectionTimeout(1_000L);
  144.         hikariConfig.setMaxLifetime(30_000L);
  145.         hikariConfig.setInitializationFailTimeout(1_000L);
  146.  
  147.         // Set metrics.
  148.         if (meterOrCollectorRegistry instanceof MeterRegistry) {
  149.             MeterRegistry meterRegistry = (MeterRegistry) meterOrCollectorRegistry;
  150.             hikariConfig.setMetricRegistry(meterRegistry);
  151.         } else {
  152.             CollectorRegistry collectorRegistry = (CollectorRegistry) meterOrCollectorRegistry;
  153.             PrometheusMetricsTrackerFactory metricsTrackerFactory = new PrometheusMetricsTrackerFactory(collectorRegistry);
  154.             hikariConfig.setMetricsTrackerFactory(metricsTrackerFactory);
  155.         }
  156.         return hikariConfig;
  157.  
  158.     }
  159.  
  160.     private static void scrapeStatistics(PrometheusMeterRegistry meterRegistry) {
  161.         meterRegistry
  162.                 .scrape()
  163.                 .lines()
  164.                 .filter(line -> !line.startsWith("#"))
  165.                 .forEach(System.out::println);
  166.         System.out.println();
  167.     }
  168.  
  169. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top