Skip to content

Commit

Permalink
Fixed database connection pool metrics logging
Browse files Browse the repository at this point in the history
  • Loading branch information
enricovianello committed Aug 2, 2021
1 parent ae257f7 commit b90a476
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
*
* Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2006-2010.
*
* 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 it.grid.storm.metrics;

import static com.codahale.metrics.MetricRegistry.name;
Expand All @@ -6,6 +23,8 @@
import java.sql.SQLException;

import org.apache.commons.dbcp2.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.JmxReporter;
Expand All @@ -14,8 +33,10 @@

public class InstrumentedBasicDataSource extends BasicDataSource {

private Timer getConnectionTimer = null;
private JmxReporter reporter = null;
private static final Logger LOG = LoggerFactory.getLogger(InstrumentedBasicDataSource.class);

private final Timer getConnectionTimer;
private final JmxReporter reporter;

public InstrumentedBasicDataSource(String prefix, MetricRegistry registry) {
instrument(prefix, registry, this);
Expand All @@ -30,6 +51,8 @@ public InstrumentedBasicDataSource(String prefix, MetricRegistry registry) {
*/
public static void instrument(String prefix, MetricRegistry registry, final BasicDataSource datasource) {

LOG.debug("Instrument {} with prefix {}: name value is {}", datasource.getClass().getName(), prefix, name(prefix, "initialsize"));

registry.register(name(prefix, "initialsize"), new Gauge<Integer>() {
public Integer getValue() {
return datasource.getInitialSize();
Expand Down Expand Up @@ -65,6 +88,11 @@ public Integer getValue() {
return datasource.getNumActive();
}
});
registry.register(name(prefix, "maxtotal"), new Gauge<Integer>() {
public Integer getValue() {
return datasource.getMaxTotal();
}
});
registry.register(name(prefix, "numidle"), new Gauge<Integer>() {
public Integer getValue() {
return datasource.getNumIdle();
Expand Down
49 changes: 37 additions & 12 deletions src/main/java/it/grid/storm/metrics/StormMetricsReporter.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
/*
*
* Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2006-2010.
*
* 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 it.grid.storm.metrics;

import java.util.Map;
import java.util.SortedMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -20,8 +35,6 @@

import it.grid.storm.common.OperationType;
import it.grid.storm.filesystem.MetricsFilesystemAdapter.FilesystemMetric;
import it.grid.storm.persistence.pool.StormBeIsamConnectionPool;
import it.grid.storm.persistence.pool.StormDbConnectionPool;

public class StormMetricsReporter extends ScheduledReporter {

Expand Down Expand Up @@ -115,17 +128,29 @@ public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> c
reportJettyHandlerMetrics("xmlrpc-handler", meters);
reportJettyHandlerMetrics("rest-handler", meters);

reportDbPoolMetrics("storm-db", StormDbConnectionPool.getInstance().getMetrics());
reportDbPoolMetrics("storm-be-isam", StormBeIsamConnectionPool.getInstance().getMetrics());
reportDbPoolMetrics("storm_db", gauges, timers);
reportDbPoolMetrics("storm_be_isam", gauges, timers);
}

private void reportDbPoolMetrics(String tpName, Map<String, String> metrics) {
@SuppressWarnings("rawtypes")
private void reportDbPoolMetrics(String tpName, SortedMap<String, Gauge> gauges,
SortedMap<String, Timer> timers) {

String timerName = tpName + ".getconnection";
Timer t = timers.get(timerName);
if (t != null) {
reportMetric(timerName, t);
} else {
LOG.error("Invalid metric name: {}", timerName);
}

int numActive = getIntValue(gauges.get(tpName + ".numactive"));
int numIdle = getIntValue(gauges.get(tpName + ".numidle"));
int maxTotal = getIntValue(gauges.get(tpName + ".maxtotal"));
int maxIdle = getIntValue(gauges.get(tpName + ".maxidle"));

String result = metrics.entrySet()
.stream()
.map(e -> e.getKey() + "=" + e.getValue())
.collect(Collectors.joining(", "));
LOG.info("{} [{}]", tpName, result);
LOG.info("{} [active-connections={}, idle-connections={}, percent-active={}, percent-idle={}]",
tpName, numActive, numIdle, numActive / maxTotal, numIdle / maxIdle);
}

private void reportMetric(String name, Timer timer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
*
* Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2006-2010.
*
* 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 it.grid.storm.persistence.pool;

public interface DatabaseConnectionPool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@

package it.grid.storm.persistence.pool;

import static java.lang.String.valueOf;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.Maps;

import it.grid.storm.metrics.InstrumentedBasicDataSource;
import it.grid.storm.metrics.StormMetricRegistry;

Expand Down Expand Up @@ -86,20 +80,6 @@ public Connection getConnection() throws SQLException {
return bds.getConnection();
}

public Map<String, String> getMetrics() {

Map<String, String> metrics = Maps.newHashMap();
metrics.put("max-total", valueOf(bds.getMaxTotal()));
metrics.put("min-idle", valueOf(bds.getMinIdle()));
metrics.put("test-on-borrow", valueOf(bds.getTestOnBorrow()));
metrics.put("test-while-idle", valueOf(bds.getTestWhileIdle()));
metrics.put("num-active", valueOf(bds.getNumActive()));
metrics.put("num-idle", valueOf(bds.getNumIdle()));
metrics.put("max-conn-lifetime-millis", valueOf(bds.getMaxConnLifetimeMillis()));
metrics.put("max-idle", valueOf(bds.getMaxIdle()));
return metrics;
}

@Override
public int getMaxTotal() {
return maxTotal;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
*
* Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2006-2010.
*
* 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 it.grid.storm.persistence.pool;

import static java.lang.String.format;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
*
* Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2006-2010.
*
* 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 it.grid.storm.persistence.pool;

import it.grid.storm.config.Configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
*
* Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2006-2010.
*
* 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 it.grid.storm.persistence.pool;

import it.grid.storm.config.Configuration;
Expand Down

0 comments on commit b90a476

Please sign in to comment.