All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.nlighten.prometheus.tomcat.TomcatDbcp2PoolExports Maven / Gradle / Ivy

There is a newer version: 0.0.17
Show newest version
package nl.nlighten.prometheus.tomcat;

import io.prometheus.client.Collector;
import io.prometheus.client.GaugeMetricFamily;
import javax.management.*;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * Exports Tomcat DBCP2-pool metrics.
 * 

* Example usage: *

 * {@code
 *   new TomcatDbcp2PoolExports().register();
 * }
 * 
* Example metrics being exported: *
 *    tomcat_dpcp2_connections_max{pool="jdbc/mypool"} 20.0
 *    tomcat_dbcp2_connections_active_total{pool="jdbc/mypool"} 2.0
 *    tomcat_dbcp2_connections_idle_total{pool="jdbc/mypool"} 6.0
 * 
*/ public class TomcatDbcp2PoolExports extends Collector { public List collect() { List mfs = new ArrayList(); try { final MBeanServer server = ManagementFactory.getPlatformMBeanServer(); ObjectName filterName = new ObjectName("Tomcat:class=javax.sql.DataSource,type=DataSource,*"); Set mBeans = server.queryMBeans(filterName, null); if (mBeans.size() > 0) { List labelList = Collections.singletonList("pool"); GaugeMetricFamily maxActiveConnectionsGauge = new GaugeMetricFamily( "tomcat_dbcp2_connections_max", "Maximum number of active connections that can be allocated from this pool at the same time", labelList); GaugeMetricFamily activeConnectionsGauge = new GaugeMetricFamily( "tomcat_dbcp2_connections_active_total", "Number of active connections allocated from this pool", labelList); GaugeMetricFamily idleConnectionsGauge = new GaugeMetricFamily( "tomcat_dbcp2_connections_idle_total", "Number of idle connections in this pool", labelList); for (final ObjectInstance mBean : mBeans) { if (mBean.getObjectName().getKeyProperty("connectionpool") == null){ List labelValueList = Collections.singletonList(mBean.getObjectName().getKeyProperty("name").replaceAll("[\"\\\\]", "")); maxActiveConnectionsGauge.addMetric( labelValueList, ((Integer) server.getAttribute(mBean.getObjectName(), "maxTotal")).doubleValue()); activeConnectionsGauge.addMetric( labelValueList, ((Integer) server.getAttribute(mBean.getObjectName(), "numActive")).doubleValue()); idleConnectionsGauge.addMetric( labelValueList, ((Integer) server.getAttribute(mBean.getObjectName(), "numIdle")).doubleValue()); } mfs.add(maxActiveConnectionsGauge); mfs.add(activeConnectionsGauge); mfs.add(idleConnectionsGauge); } } } catch (javax.management.AttributeNotFoundException e) { // Can happen in exception cases where TomcatDbcp2PoolExports is configured with a TomcatJdcpPool configured } catch (Exception e) { System.out.println ("####### " + e.getLocalizedMessage()); e.printStackTrace(); } return mfs; } public static boolean isDbcp2Used() { try { final MBeanServer server = ManagementFactory.getPlatformMBeanServer(); ObjectName filterName = new ObjectName("tomcat.jdbc:class=org.apache.tomcat.jdbc.pool.DataSource,type=ConnectionPool,*"); Set mBeans = server.queryMBeans(filterName, null); return !mBeans.isEmpty(); } catch (Exception e) { e.printStackTrace(); } return false; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy