Skip to content

HikariCP

Collect HikariCP metrics

Configuration

Collect with the DataKit collector

  • Enable the statsd collector

Go to conf.d/samples under the DataKit installation directory and copy statsd.conf.sample to statsd.conf.

mkdir -p ../statsd
cp statsd.conf.sample ../statsd/statsd.conf

Confirm that statsd.conf sets protocol = "udp" and service_address = ":8125", and enable parse_data_dog_tags = true to preserve the connection pool's pool tag.

  • If you also collect application traces, enable the ddtrace collector

Go to conf.d/ddtrace under the DataKit installation directory and copy ddtrace.conf.sample to ddtrace.conf.

cp ddtrace.conf.sample ddtrace.conf

  • After modifying the collector configuration, run the following command to restart DataKit
datakit service -R

Client configuration

Enable MBeans according to the data source type, then configure JMXFetch and the startup options.

Spring Boot with a single data source

For a single Spring Boot data source, use register-mbeans=true:

spring.datasource.url=jdbc:mysql://xx.xxx.xx.xxx:3306/test?useSSL=false&useLegacyDatetimeCode=false&allowPublicKeyRetrieval=true
spring.datasource.username=***
spring.datasource.password=*****
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.idle-timeout=300000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.pool-name=HikariCP
spring.datasource.hikari.register-mbeans=true

Multiple data sources with dynamic-datasource

When using dynamic-datasource (version 4.3.1 in this example), merge the following fields into your existing configuration and replace the data source names with your actual names:

spring:
  datasource:
    dynamic:
      datasource:
        master:
          hikari:
            is-register-mbeans: true
        slave:
          hikari:
            is-register-mbeans: true

You can also set spring.datasource.dynamic.hikari.is-register-mbeans=true globally. Settings for individual data sources take precedence.

Note that the parameter name is is-register-mbeans. Restart the application after changing it. A custom pool-name must be at the same level as the data source's url, and each pool name must be unique.

Configure JMXFetch

  • Download dd-java-agent

SDK

  • Create a hikaricp.yaml file with the following content:
init_config:

instances:
  - jvm_direct: true
    name: hikari-slick-monitoring
    collect_default_jvm_metrics: false
    collect_default_metrics: false
    refresh_beans: 60
    conf:
      - include:
          bean_regex: "com.zaxxer.hikari:type=Pool \\((.*)\\)"
          tags:
            pool: $1
          attribute:
            ActiveConnections:
              metric_type: gauge
              alias: hikaricp.connections.active
            IdleConnections:
              metric_type: gauge
              alias: hikaricp.connections.idle
            TotalConnections:
              metric_type: gauge
              alias: hikaricp.connections.total
            ThreadsAwaitingConnection:
              metric_type: gauge
              alias: hikaricp.connections.pending
      - include:
          bean_regex: "com.zaxxer.hikari:type=PoolConfig \\((.*)\\)"
          tags:
            pool: $1
          attribute:
            MaximumPoolSize:
              metric_type: gauge
              alias: hikaricp.pool.size.max
            IdleTimeout:
              metric_type: gauge
              alias: hikaricp.idle.timeout
            MaxLifetime:
              metric_type: gauge
              alias: hikaricp.max.lifetime
            MinimumIdle:
              metric_type: gauge
              alias: hikaricp.min.idle
            ConnectionTimeout:
              metric_type: gauge
              alias: hikaricp.connections.timeout
            ValidationTimeout:
              metric_type: gauge
              alias: hikaricp.validation.timeout
            LeakDetectionThreshold:
              metric_type: gauge
              alias: hikaricp.leak.detection.threshold

Configure startup options

The following command assumes that the application and DataKit run on the same host:

java \
-javaagent:/xxx/dd-java-agent.jar \
-Ddd.agent.port=9529 \
-Ddd.service=demo \
-Ddd.jmxfetch.check-period=1000 \
-Ddd.jmxfetch.enabled=true \
-Ddd.jmxfetch.config.dir=/xxx/ \
-Ddd.jmxfetch.config=hikaricp.yaml \
-Ddd.jmxfetch.statsd.host=127.0.0.1 \
-Ddd.jmxfetch.statsd.port=8125 \
-jar xxxx.jar

config.dir is the JMX configuration directory that the application process can read. The file name must match the config parameter. Place all -D options before -jar.

If the application and DataKit run in different Pods, change statsd.host to the actual Service address, such as datakit-service.datakit.svc.cluster.local, and ensure that UDP port 8125 is reachable.

Verify collection results

After startup, confirm that each pool's Hikari MBeans are registered and the DataKit StatsD receive count keeps increasing. Then query the hikaricp measurement in Guance, using the pool tag to distinguish connection pools.

Metrics

hikaricp measurement

HikariCP metrics are stored in the hikaricp measurement. The following table describes these metrics.

Metrics Description Purpose Unit
connections_active Current number of active database connections Number of connections currently used by the application count
connections_idle Current number of idle database connections Number of unused connections in the idle state count
connections_pending Current number of requests waiting for a connection A high value may indicate that the connection pool is too small to meet current concurrency demands count
connections_total Total number of database connections in the current pool Includes active and idle connections count
idle_timeout Maximum idle time for a connection Configuration setting: the pool closes a connection when its idle time exceeds this value ms
leak_detection_threshold Connection leak detection threshold Configuration setting: if a connection is in use longer than this threshold, the pool suspects a connection leak and issues a warning ms
max_lifetime Maximum connection lifetime Configuration setting: the total lifetime of a connection from creation to closure cannot exceed this value ms
min_idle Minimum number of idle connections in the pool Configuration setting: the pool maintains at least this many idle connections to respond quickly to new requests count
pool_size_max Maximum number of connections allowed in the pool Configuration setting: limits the pool size to prevent excessive resource consumption by too many connections count
validation_timeout Connection validation timeout Configuration setting: the pool validates a connection when it is obtained; validation fails if it takes longer than this value ms
connections_timeout Connection timeout Configuration setting: maximum wait time to establish a connection ms

Feedback

Is this page helpful?