Don't try to cast metric registry before it is checked for null (#2076)

* Don't try to cast metric registry before it is checked for null

* Simplify the condition check

* Use previously obtained object instead of calling the same getter one more time

---------

Co-authored-by: Alexander Sinuskin <Alexander.Sinuskin.ext@deutsche-boerse.com>
pull/1838/merge
alsin 1 year ago committed by GitHub
parent 5b2852e8bf
commit 2021f90444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,10 +16,6 @@
package com.zaxxer.hikari.metrics.dropwizard; package com.zaxxer.hikari.metrics.dropwizard;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer; import com.codahale.metrics.Timer;
import com.codahale.metrics.health.HealthCheck; import com.codahale.metrics.health.HealthCheck;
@ -27,6 +23,10 @@ import com.codahale.metrics.health.HealthCheckRegistry;
import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.pool.HikariPool; import com.zaxxer.hikari.pool.HikariPool;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
/** /**
* Provides Dropwizard HealthChecks. Two health checks are provided: * Provides Dropwizard HealthChecks. Two health checks are provided:
* <ul> * <ul>
@ -54,13 +54,16 @@ public final class CodahaleHealthChecker
public static void registerHealthChecks(final HikariPool pool, final HikariConfig hikariConfig, final HealthCheckRegistry registry) public static void registerHealthChecks(final HikariPool pool, final HikariConfig hikariConfig, final HealthCheckRegistry registry)
{ {
final var healthCheckProperties = hikariConfig.getHealthCheckProperties(); final var healthCheckProperties = hikariConfig.getHealthCheckProperties();
final var metricRegistry = (MetricRegistry) hikariConfig.getMetricRegistry();
final var checkTimeoutMs = Long.parseLong(healthCheckProperties.getProperty("connectivityCheckTimeoutMs", String.valueOf(hikariConfig.getConnectionTimeout()))); final var checkTimeoutMs = Long.parseLong(healthCheckProperties.getProperty("connectivityCheckTimeoutMs", String.valueOf(hikariConfig.getConnectionTimeout())));
registry.register(MetricRegistry.name(hikariConfig.getPoolName(), "pool", "ConnectivityCheck"), new ConnectivityHealthCheck(pool, checkTimeoutMs)); registry.register(MetricRegistry.name(hikariConfig.getPoolName(), "pool", "ConnectivityCheck"), new ConnectivityHealthCheck(pool, checkTimeoutMs));
final var expected99thPercentile = Long.parseLong(healthCheckProperties.getProperty("expected99thPercentileMs", "0")); final var expected99thPercentile = Long.parseLong(healthCheckProperties.getProperty("expected99thPercentileMs", "0"));
if (metricRegistry != null && expected99thPercentile > 0) {
final Object metricRegistryObj = hikariConfig.getMetricRegistry();
if (expected99thPercentile > 0 && metricRegistryObj instanceof MetricRegistry) {
final var metricRegistry = (MetricRegistry) metricRegistryObj;
var timers = metricRegistry.getTimers((name, metric) -> name.equals(MetricRegistry.name(hikariConfig.getPoolName(), "pool", "Wait"))); var timers = metricRegistry.getTimers((name, metric) -> name.equals(MetricRegistry.name(hikariConfig.getPoolName(), "pool", "Wait")));
if (!timers.isEmpty()) { if (!timers.isEmpty()) {

Loading…
Cancel
Save