Permalink
Browse files

MetricsTracker refactor.

  • Loading branch information...
1 parent 8bb6d1e commit b06eb02b6dfa180c618b935e40be7400fac205d2 @brettwooldridge committed Jan 11, 2017
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2017 Brett Wooldridge
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.zaxxer.hikari.metrics;
+
+/**
+ * @author Brett Wooldridge
+ */
+public interface IMetricsTracker extends AutoCloseable
+{
+ default void recordConnectionCreatedMillis(long connectionCreatedMillis) {}
+
+ default void recordConnectionAcquiredNanos(final long elapsedAcquiredNanos) {}
+
+ default void recordConnectionUsageMillis(final long elapsedBorrowedMillis) {}
+
+ default void recordConnectionTimeout() {}
+
+ @Override
+ default void close() {}
+}
@@ -21,30 +21,7 @@
*
* @author Brett Wooldridge
*/
-public class MetricsTracker implements AutoCloseable
+@Deprecated
+public class MetricsTracker implements IMetricsTracker
{
- public MetricsTracker()
- {
- }
-
- public void recordConnectionAcquiredNanos(final long elapsedAcquiredNanos)
- {
- }
-
- public void recordConnectionUsageMillis(final long elapsedBorrowedMillis)
- {
- }
-
- public void recordConnectionTimeout()
- {
- }
-
- public void recordConnectionCreatedMillis(long connectionCreatedMillis)
- {
- }
-
- @Override
- public void close()
- {
- }
}
@@ -19,11 +19,11 @@
public interface MetricsTrackerFactory
{
/**
- * Create an instance of a MetricsTracker.
+ * Create an instance of an IMetricsTracker.
*
* @param poolName the name of the pool
* @param poolStats a PoolStats instance to use
- * @return a MetricsTracker implementation instance
+ * @return a IMetricsTracker implementation instance
*/
- MetricsTracker create(String poolName, PoolStats poolStats);
+ IMetricsTracker create(String poolName, PoolStats poolStats);
}
@@ -23,10 +23,10 @@
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
-import com.zaxxer.hikari.metrics.MetricsTracker;
+import com.zaxxer.hikari.metrics.IMetricsTracker;
import com.zaxxer.hikari.metrics.PoolStats;
-public final class CodaHaleMetricsTracker extends MetricsTracker
+public final class CodaHaleMetricsTracker implements IMetricsTracker
{
private final String poolName;
private final Timer connectionObtainTimer;
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2013,2014 Brett Wooldridge
+ * Copyright (C) 2013 Brett Wooldridge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
package com.zaxxer.hikari.metrics.dropwizard;
import com.codahale.metrics.MetricRegistry;
-import com.zaxxer.hikari.metrics.MetricsTracker;
+import com.zaxxer.hikari.metrics.IMetricsTracker;
import com.zaxxer.hikari.metrics.MetricsTrackerFactory;
import com.zaxxer.hikari.metrics.PoolStats;
@@ -36,7 +36,7 @@ public MetricRegistry getRegistry()
}
@Override
- public MetricsTracker create(String poolName, PoolStats poolStats)
+ public IMetricsTracker create(String poolName, PoolStats poolStats)
{
return new CodaHaleMetricsTracker(poolName, poolStats, registry);
}
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2013, 2014 Brett Wooldridge
+ * Copyright (C) 2013 Brett Wooldridge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,14 +16,14 @@
package com.zaxxer.hikari.metrics.prometheus;
-import com.zaxxer.hikari.metrics.MetricsTracker;
+import com.zaxxer.hikari.metrics.IMetricsTracker;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import io.prometheus.client.Summary;
-class PrometheusMetricsTracker extends MetricsTracker
+class PrometheusMetricsTracker implements IMetricsTracker
{
private final Counter.Child connectionTimeoutCounter;
private final Summary.Child elapsedAcquiredSummary;
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2013, 2014 Brett Wooldridge
+ * Copyright (C) 2016 Brett Wooldridge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
package com.zaxxer.hikari.metrics.prometheus;
-import com.zaxxer.hikari.metrics.MetricsTracker;
+import com.zaxxer.hikari.metrics.IMetricsTracker;
import com.zaxxer.hikari.metrics.MetricsTrackerFactory;
import com.zaxxer.hikari.metrics.PoolStats;
@@ -28,9 +28,11 @@
* config.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory());
* }</pre>
*/
-public class PrometheusMetricsTrackerFactory implements MetricsTrackerFactory {
+public class PrometheusMetricsTrackerFactory implements MetricsTrackerFactory
+{
@Override
- public MetricsTracker create(String poolName, PoolStats poolStats) {
+ public IMetricsTracker create(String poolName, PoolStats poolStats)
+ {
Collector collector = new HikariCPCollector(poolName, poolStats).register();
return new PrometheusMetricsTracker(poolName, collector);
}
@@ -16,15 +16,6 @@
package com.zaxxer.hikari.pool;
-import static com.zaxxer.hikari.pool.ProxyConnection.DIRTY_BIT_AUTOCOMMIT;
-import static com.zaxxer.hikari.pool.ProxyConnection.DIRTY_BIT_CATALOG;
-import static com.zaxxer.hikari.pool.ProxyConnection.DIRTY_BIT_ISOLATION;
-import static com.zaxxer.hikari.pool.ProxyConnection.DIRTY_BIT_NETTIMEOUT;
-import static com.zaxxer.hikari.pool.ProxyConnection.DIRTY_BIT_READONLY;
-import static com.zaxxer.hikari.util.ClockSource.currentTime;
-import static com.zaxxer.hikari.util.ClockSource.elapsedMillis;
-import static com.zaxxer.hikari.util.ClockSource.elapsedNanos;
-import static com.zaxxer.hikari.util.UtilityElf.createInstance;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
@@ -48,12 +39,22 @@
import org.slf4j.LoggerFactory;
import com.zaxxer.hikari.HikariConfig;
-import com.zaxxer.hikari.metrics.MetricsTracker;
+import com.zaxxer.hikari.metrics.IMetricsTracker;
import com.zaxxer.hikari.util.DriverDataSource;
import com.zaxxer.hikari.util.PropertyElf;
import com.zaxxer.hikari.util.UtilityElf;
import com.zaxxer.hikari.util.UtilityElf.DefaultThreadFactory;
+import static com.zaxxer.hikari.pool.ProxyConnection.DIRTY_BIT_AUTOCOMMIT;
+import static com.zaxxer.hikari.pool.ProxyConnection.DIRTY_BIT_CATALOG;
+import static com.zaxxer.hikari.pool.ProxyConnection.DIRTY_BIT_ISOLATION;
+import static com.zaxxer.hikari.pool.ProxyConnection.DIRTY_BIT_NETTIMEOUT;
+import static com.zaxxer.hikari.pool.ProxyConnection.DIRTY_BIT_READONLY;
+import static com.zaxxer.hikari.util.ClockSource.currentTime;
+import static com.zaxxer.hikari.util.ClockSource.elapsedMillis;
+import static com.zaxxer.hikari.util.ClockSource.elapsedNanos;
+import static com.zaxxer.hikari.util.UtilityElf.createInstance;
+
abstract class PoolBase
{
private final Logger LOGGER = LoggerFactory.getLogger(PoolBase.class);
@@ -62,7 +63,7 @@
protected final String poolName;
protected long connectionTimeout;
protected long validationTimeout;
- protected MetricsTrackerDelegate metricsTracker;
+ protected IMetricsTrackerDelegate metricsTracker;
private static final String[] RESET_STATES = {"readOnly", "autoCommit", "isolation", "catalog", "netTimeout"};
private static final int UNINITIALIZED = -1;
@@ -639,91 +640,69 @@ public void execute(Runnable command)
}
}
+ static interface IMetricsTrackerDelegate extends AutoCloseable
+ {
+ default void recordConnectionUsage(PoolEntry poolEntry) {}
+
+ default void recordConnectionCreated(long connectionCreatedMillis) {}
+
+ default void recordBorrowStats(final PoolEntry poolEntry, final long startTime) {}
+
+ default void recordConnectionTimeout() {}
+
+ @Override
+ default void close() {}
+ }
+
/**
* A class that delegates to a MetricsTracker implementation. The use of a delegate
* allows us to use the NopMetricsTrackerDelegate when metrics are disabled, which in
* turn allows the JIT to completely optimize away to callsites to record metrics.
*/
- static class MetricsTrackerDelegate implements AutoCloseable
+ static class MetricsTrackerDelegate implements IMetricsTrackerDelegate
{
- final MetricsTracker tracker;
-
- protected MetricsTrackerDelegate()
- {
- this.tracker = null;
- }
+ final IMetricsTracker tracker;
- MetricsTrackerDelegate(MetricsTracker tracker)
+ MetricsTrackerDelegate(IMetricsTracker tracker)
{
this.tracker = tracker;
}
@Override
- public void close()
- {
- tracker.close();
- }
-
- void recordConnectionUsage(final PoolEntry poolEntry)
+ public void recordConnectionUsage(final PoolEntry poolEntry)
{
tracker.recordConnectionUsageMillis(poolEntry.getMillisSinceBorrowed());
}
- void recordConnectionCreated(long connectionCreatedMillis)
+ @Override
+ public void recordConnectionCreated(long connectionCreatedMillis)
{
tracker.recordConnectionCreatedMillis(connectionCreatedMillis);
}
- /**
- * @param poolEntry
- * @param now
- */
- void recordBorrowStats(final PoolEntry poolEntry, final long startTime)
+ @Override
+ public void recordBorrowStats(final PoolEntry poolEntry, final long startTime)
{
final long now = currentTime();
poolEntry.lastBorrowed = now;
tracker.recordConnectionAcquiredNanos(elapsedNanos(startTime, now));
}
- void recordConnectionTimeout() {
- tracker.recordConnectionTimeout();
- }
- }
-
- /**
- * A no-op implementation of the MetricsTrackerDelegate that is used when metrics capture is
- * disabled.
- */
- static final class NopMetricsTrackerDelegate extends MetricsTrackerDelegate
- {
@Override
- void recordConnectionUsage(final PoolEntry poolEntry)
- {
- // no-op
+ public void recordConnectionTimeout() {
+ tracker.recordConnectionTimeout();
}
@Override
public void close()
{
- // no-op
- }
-
- @Override
- void recordBorrowStats(final PoolEntry poolEntry, final long startTime)
- {
- // no-op
- }
-
- @Override
- void recordConnectionTimeout()
- {
- // no-op
- }
-
- @Override
- void recordConnectionCreated(long connectionCreatedMillis)
- {
- // no-op
+ tracker.close();
}
}
+
+ /**
+ * A no-op implementation of the IMetricsTrackerDelegate that is used when metrics capture is
+ * disabled.
+ */
+ static final class NopMetricsTrackerDelegate implements IMetricsTrackerDelegate {}
}

0 comments on commit b06eb02

Please sign in to comment.