/**
 * Copyright 2017 Pivotal Software, Inc.
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 io.micrometer.core.instrument.binder.db;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.binder.MeterBinder;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Jon Schneider
 */
public class DatabaseTableMetrics implements MeterBinder {
    /**
     * Record the row count for an individual database table.
     *
     * @param registry  The registry to bind metrics to.
     * @param ds        The data source to use to run the row count query.
     * @param tableName The name of the table to report table size for.
     * @param name      The name prefix of the metrics.
     * @param tags      Tags to apply to all recorded metrics.
     */
    public static void monitor(MeterRegistry registry, DataSource ds, String tableName, String name, String... tags) {
        monitor(registry, ds, tableName, name, Tags.zip(tags));
    }

    /**
     * Record the row count for an individual database table.
     *
     * @param registry  The registry to bind metrics to.
     * @param ds        The data source to use to run the row count query.
     * @param tableName The name of the table to report table size for.
     * @param name      The name prefix of the metrics.
     * @param tags      Tags to apply to all recorded metrics.
     */
    public static void monitor(MeterRegistry registry, DataSource ds, String tableName, String name, Iterable<Tag> tags) {
        new DatabaseTableMetrics(ds, tableName, name, tags).bindTo(registry);
    }

    private final String tableName;
    private final String name;
    private final Iterable<Tag> tags;
    private final DataSource dataSource;

    public DatabaseTableMetrics(DataSource dataSource, String tableName, String name, Iterable<Tag> tags) {
        this.tableName = tableName;
        this.name = name;
        this.tags = tags;
        this.dataSource = dataSource;
    }

    @Override
    public void bindTo(MeterRegistry registry) {
        registry.gauge(name, Tags.concat(tags,"table", tableName), dataSource, ds -> {
            try (Connection conn = ds.getConnection();
                 PreparedStatement ps = conn.prepareStatement("SELECT COUNT(1) FROM " + tableName);
                 ResultSet rs = ps.executeQuery()) {
                rs.next();
                return rs.getInt(1);
            } catch(SQLException ignored) {
                return 0;
            }
        });
    }
}

@@ -2305,24 +2305,56 @@
 ry);%0A    %7D%0A%0A
+    private final String query;%0A
     private 
@@ -2373,24 +2373,24 @@
  tableName;%0A
-
     private 
@@ -2588,32 +2588,313 @@
 le%3CTag%3E tags) %7B%0A
+        this(dataSource, %22SELECT COUNT(1) FROM %22 + tableName, tableName, name, tags);%0A    %7D%0A%0A    public DatabaseTableMetrics(DataSource dataSource, String query, String tableName, String name, Iterable%3CTag%3E tags) %7B%0A        this.dataSource = dataSource;%0A        this.query = query;%0A
         this.tab
@@ -2969,46 +2969,8 @@
 gs;%0A
-        this.dataSource = dataSource;%0A
     
@@ -3243,43 +3243,13 @@
 ent(
-%22SELECT COUNT(1) FROM %22 + tableName
+query
 );%0A 

Allow customization of query for DatabaseTableMetrics