Database Instrumentation

DataSource Observation Instrumentation

Through the Datasource Micrometer project you can instrument your Datasource to start producing Observations while interacting with the database. That means that depending on your Observation Handler setup you can plug in producing of metrics or distributed tracing.

You can read more about Datasource Micrometer reference documentation here.

DataSource Metrics Instrumentation

// Binding instrumentation through static method
DatabaseTableMetrics.monitor(registry, "foo", "mydb", ds);

// Usage example
try (Connection conn = ds.getConnection()) {
    conn.prepareStatement("CREATE TABLE foo (id int)").execute();
    conn.prepareStatement("INSERT INTO foo VALUES (1)").executeUpdate();
}
assertThat(registry.get("db.table.size").tag("table", "foo").tag("db", "mydb").gauge().value()).isEqualTo(1.0);

jOOQ Instrumentation

// Setting up instrumentation
Configuration configuration = new DefaultConfiguration().set(conn).set(SQLDialect.H2);

MetricsDSLContext jooq = MetricsDSLContext.withMetrics(DSL.using(configuration), meterRegistry, Tags.empty());

// Usage example
jooq.tag("name", "selectAllAuthors").execute("SELECT * FROM author");

assertThat(meterRegistry.get("jooq.query").tag("name", "selectAllAuthors").timer().count()).isEqualTo(1);

PostgresSQL Instrumentation

// Setting up instrumentation
new PostgreSQLDatabaseMetrics(dataSource, postgres.getDatabaseName()).bindTo(registry);

// Usage example
executeSql("CREATE TABLE gauge_test_table (val varchar(255))",
        "INSERT INTO gauge_test_table (val) VALUES ('foo')", "UPDATE gauge_test_table SET val = 'bar'",
        "SELECT * FROM gauge_test_table", "DELETE FROM gauge_test_table");
Thread.sleep(PGSTAT_STAT_INTERVAL);

final List<String> GAUGES = Arrays.asList(SIZE, CONNECTIONS, ROWS_DEAD, LOCKS);

for (String name : GAUGES) {
    assertThat(get(name).gauge().value()).withFailMessage("Gauge " + name + " is zero.").isGreaterThan(0);
}