Skip to content

Commit

Permalink
Add metric for SQL commits (#6222)
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
rautenrieth-da authored Jun 8, 2020
1 parent 921266a commit 2a4e9dc
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ class DatabaseMetrics private[metrics] (
val waitTimer: Timer = registry.timer(dbPrefix :+ "wait")
val executionTimer: Timer = registry.timer(dbPrefix :+ "exec")
val translationTimer: Timer = registry.timer(dbPrefix :+ "translation")
val commitTimer: Timer = registry.timer(dbPrefix :+ "commit")
val queryTimer: Timer = registry.timer(dbPrefix :+ "query")
}
8 changes: 7 additions & 1 deletion ledger/metrics/src/main/scala/com/daml/metrics/Metrics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ class Metrics(val registry: MetricRegistry) {
registry.gauge(name, metricSupplier).asInstanceOf[Gauge[T]]
}

object test {
val prefix: MetricName = MetricName("test")

val db: DatabaseMetrics = new DatabaseMetrics(registry, prefix, "db")
}

object daml {
val prefix: MetricName = MetricName.DAML

object commands {
val prefix: MetricName = daml.prefix :+ "commands"

val submissions: Timer = registry.timer(prefix :+ "submissions")
val validation: Timer = registry.timer(prefix :+ "validation")
val submissions: Timer = registry.timer(prefix :+ "submissions")

val failedCommandInterpretations: Meter =
registry.meter(prefix :+ "failed_command_interpretations")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class DbDispatcher private (
val startExec = System.nanoTime()
try {
// Actual execution
val result = connectionProvider.runSQL(sql)
val result = connectionProvider.runSQL(databaseMetrics)(sql)
result
} catch {
case NonFatal(e) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import java.util.{Timer, TimerTask}
import com.codahale.metrics.MetricRegistry
import com.daml.ledger.api.health.{HealthStatus, Healthy, Unhealthy}
import com.daml.logging.{ContextualizedLogger, LoggingContext}
import com.daml.metrics.{DatabaseMetrics, Timed}
import com.daml.platform.configuration.ServerRole
import com.daml.platform.store.DbType
import com.daml.platform.store.dao.HikariJdbcConnectionProvider._
Expand Down Expand Up @@ -120,12 +121,18 @@ class HikariJdbcConnectionProvider(dataSource: HikariDataSource, healthPoller: T
else
Unhealthy

override def runSQL[T](block: Connection => T): T = {
override def runSQL[T](databaseMetrics: DatabaseMetrics)(block: Connection => T): T = {
val conn = dataSource.getConnection()
conn.setAutoCommit(false)
try {
val res = block(conn)
conn.commit()
val res = Timed.value(
databaseMetrics.queryTimer,
block(conn)
)
Timed.value(
databaseMetrics.commitTimer,
conn.commit()
)
res
} catch {
case e: SQLTransientConnectionException =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.daml.platform.store.dao
import java.sql.Connection

import com.daml.ledger.api.health.ReportsHealth
import com.daml.metrics.DatabaseMetrics

/** A helper to run JDBC queries using a pool of managed connections */
trait JdbcConnectionProvider extends ReportsHealth {
Expand All @@ -14,5 +15,5 @@ trait JdbcConnectionProvider extends ReportsHealth {
* is returned to the pool.
* The block must not recursively call [[runSQL]], as this could result in a deadlock
* waiting for a free connection from the same pool. */
def runSQL[T](block: Connection => T): T
def runSQL[T](databaseMetrics: DatabaseMetrics)(block: Connection => T): T
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

package com.daml.platform.sandbox.persistence

import com.codahale.metrics.MetricRegistry
import com.codahale.metrics.{MetricRegistry, SharedMetricRegistries}
import com.daml.dec.DirectExecutionContext
import com.daml.logging.LoggingContext.newLoggingContext
import com.daml.metrics.Metrics
import com.daml.platform.configuration.ServerRole
import com.daml.platform.store.FlywayMigrations
import com.daml.platform.store.dao.{HikariJdbcConnectionProvider, JdbcConnectionProvider}
Expand All @@ -20,6 +21,7 @@ class PostgresIT extends AsyncWordSpec with Matchers with PostgresAroundAll with

private var connectionProviderResource: Resource[JdbcConnectionProvider] = _
private var connectionProvider: JdbcConnectionProvider = _
private val metrics = new Metrics(SharedMetricRegistries.getOrCreate("PostgresIT"))

override def beforeAll(): Unit = {
super.beforeAll()
Expand All @@ -44,7 +46,7 @@ class PostgresIT extends AsyncWordSpec with Matchers with PostgresAroundAll with
"Postgres" when {
"running queries using Hikari" should {
"be accessible" in {
connectionProvider.runSQL { conn =>
connectionProvider.runSQL(metrics.test.db) { conn =>
val resultSet = conn.createStatement().executeQuery("SELECT 1")
resultSet.next()
val result = resultSet.getInt(1)
Expand All @@ -59,7 +61,7 @@ class PostgresIT extends AsyncWordSpec with Matchers with PostgresAroundAll with
newLoggingContext { implicit logCtx =>
new FlywayMigrations(postgresDatabase.url).migrate()(DirectExecutionContext)
}.map { _ =>
connectionProvider.runSQL { conn =>
connectionProvider.runSQL(metrics.test.db) { conn =>
def checkTableExists(table: String) = {
val resultSet = conn.createStatement().executeQuery(s"SELECT * from $table")
resultSet.next shouldEqual false
Expand Down

0 comments on commit 2a4e9dc

Please sign in to comment.