diff options
Diffstat (limited to 'kamon-jdbc/src')
3 files changed, 36 insertions, 23 deletions
diff --git a/kamon-jdbc/src/main/scala/kamon/jdbc/instrumentation/StatementInstrumentation.scala b/kamon-jdbc/src/main/scala/kamon/jdbc/instrumentation/StatementInstrumentation.scala index f0a83007..600a79b9 100644 --- a/kamon-jdbc/src/main/scala/kamon/jdbc/instrumentation/StatementInstrumentation.scala +++ b/kamon-jdbc/src/main/scala/kamon/jdbc/instrumentation/StatementInstrumentation.scala @@ -17,6 +17,7 @@ package kamon.jdbc.instrumentation import java.sql.SQLException import java.util.concurrent.TimeUnit.{ NANOSECONDS ⇒ nanos } + import akka.actor.ActorSystem import kamon.Kamon import kamon.jdbc.Jdbc @@ -59,8 +60,8 @@ class StatementInstrumentation { log.debug(s"Unable to parse sql [$sql]") pjp.proceed() } - } getOrElse pjp.proceed() - } + } + } getOrElse pjp.proceed() @AfterThrowing(pointcut = "onExecuteStatement(sql) || onExecutePreparedStatement(sql) || onExecutePreparedCall(sql)", throwing = "ex") def onError(sql: String, ex: SQLException): Unit = { @@ -94,7 +95,7 @@ class StatementInstrumentation { } object StatementInstrumentation { - val log = LoggerFactory.getLogger("StatementInstrumentation") + val log = LoggerFactory.getLogger(classOf[StatementInstrumentation]) val SelectStatement = "(?i)^\\s*select.*?\\sfrom[\\s\\[]+([^\\]\\s,)(;]*).*".r val InsertStatement = "(?i)^\\s*insert(?:\\s+ignore)?\\s+into\\s+([^\\s(,;]*).*".r diff --git a/kamon-jdbc/src/test/resources/logback.xml b/kamon-jdbc/src/test/resources/logback.xml new file mode 100644 index 00000000..c336bbfe --- /dev/null +++ b/kamon-jdbc/src/test/resources/logback.xml @@ -0,0 +1,12 @@ +<configuration> + <statusListener class="ch.qos.logback.core.status.NopStatusListener"/> + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> + </encoder> + </appender> + + <root level="OFF"> + <appender-ref ref="STDOUT"/> + </root> +</configuration>
\ No newline at end of file diff --git a/kamon-jdbc/src/test/scala/kamon/jdbc/instrumentation/StatementInstrumentationSpec.scala b/kamon-jdbc/src/test/scala/kamon/jdbc/instrumentation/StatementInstrumentationSpec.scala index ee68234d..ffd5a867 100644 --- a/kamon-jdbc/src/test/scala/kamon/jdbc/instrumentation/StatementInstrumentationSpec.scala +++ b/kamon-jdbc/src/test/scala/kamon/jdbc/instrumentation/StatementInstrumentationSpec.scala @@ -15,10 +15,10 @@ package kamon.jdbc.instrumentation -import java.sql.{DriverManager, SQLException} +import java.sql.{ DriverManager, SQLException } import akka.actor.ActorSystem -import akka.testkit.{TestKitBase, TestProbe} +import akka.testkit.{ TestKitBase, TestProbe } import com.typesafe.config.ConfigFactory import kamon.Kamon import kamon.jdbc.SlowQueryProcessor @@ -27,7 +27,8 @@ import kamon.jdbc.metric.StatementsMetrics.StatementsMetricsSnapshot import kamon.metric.Metrics import kamon.metric.Subscriptions.TickMetricSnapshot import kamon.trace.TraceRecorder -import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike} +import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpecLike } + import scala.concurrent.duration._ class StatementInstrumentationSpec extends TestKitBase with WordSpecLike with Matchers with BeforeAndAfterAll { @@ -44,7 +45,7 @@ class StatementInstrumentationSpec extends TestKitBase with WordSpecLike with Ma |} """.stripMargin)) - val connection = DriverManager.getConnection("jdbc:h2:mem:jdbc-spec","SA", "") + val connection = DriverManager.getConnection("jdbc:h2:mem:jdbc-spec", "SA", "") override protected def beforeAll(): Unit = { connection should not be null @@ -59,12 +60,12 @@ class StatementInstrumentationSpec extends TestKitBase with WordSpecLike with Ma } "the StatementInstrumentation" should { - "record the execution time of the INSERT operation" in new StatementsMetricsListenerFixture { + "record the execution time of INSERT operation" in new StatementsMetricsListenerFixture { TraceRecorder.withNewTraceContext("jdbc-trace-insert") { val metricsListener = subscribeToMetrics() - for(id <- 1 to 100) { + for (id ← 1 to 100) { val insert = s"INSERT INTO Address (Nr, Name) VALUES($id, 'foo')" val insertStatement = connection.prepareStatement(insert) insertStatement.execute() @@ -80,7 +81,7 @@ class StatementInstrumentationSpec extends TestKitBase with WordSpecLike with Ma val metricsListener = subscribeToMetrics() - for(id <- 1 to 100) { + for (id ← 1 to 100) { val select = s"SELECT * FROM Address where Nr = $id" val selectStatement = connection.createStatement() selectStatement.execute(select) @@ -96,8 +97,8 @@ class StatementInstrumentationSpec extends TestKitBase with WordSpecLike with Ma val metricsListener = subscribeToMetrics() - for(id <- 1 to 100) { - val update = s"UPDATE Address SET Name = 'bar$id' where Nr = $id" + for (id ← 1 to 100) { + val update = s"UPDATE Address SET Name = 'bar$id' where Nr = $id" val updateStatement = connection.prepareStatement(update) updateStatement.execute() } @@ -108,12 +109,12 @@ class StatementInstrumentationSpec extends TestKitBase with WordSpecLike with Ma } "record the execution time of DELETE operation" in new StatementsMetricsListenerFixture { - TraceRecorder.withNewTraceContext("jdbc-trace-insert") { + TraceRecorder.withNewTraceContext("jdbc-trace-delete") { val metricsListener = subscribeToMetrics() - for(id <- 1 to 100) { - val delete = s"DELETE FROM Address where Nr = $id" + for (id ← 1 to 100) { + val delete = s"DELETE FROM Address where Nr = $id" val deleteStatement = connection.createStatement() deleteStatement.execute(delete) } @@ -128,7 +129,7 @@ class StatementInstrumentationSpec extends TestKitBase with WordSpecLike with Ma val metricsListener = subscribeToMetrics() - for(id <- 1 to 2) { + for (id ← 1 to 2) { val select = s"SELECT * FROM Address; CALL SLEEP(100)" val selectStatement = connection.createStatement() selectStatement.execute(select) @@ -139,14 +140,14 @@ class StatementInstrumentationSpec extends TestKitBase with WordSpecLike with Ma } } - "count the total ERRORS" in new StatementsMetricsListenerFixture { - TraceRecorder.withNewTraceContext("jdbc-trace-slow") { + "count all SQL ERRORS" in new StatementsMetricsListenerFixture { + TraceRecorder.withNewTraceContext("jdbc-trace-errors") { val metricsListener = subscribeToMetrics() - for(_ <- 1 to 10) { + for (_ ← 1 to 10) { intercept[SQLException] { - val error = "SELECT * FROM NON_EXIST_TABLE" + val error = "SELECT * FROM NO_EXISTENT_TABLE" val errorStatement = connection.createStatement() errorStatement.execute(error) } @@ -158,7 +159,7 @@ class StatementInstrumentationSpec extends TestKitBase with WordSpecLike with Ma } trait StatementsMetricsListenerFixture { - def subscribeToMetrics( ): TestProbe = { + def subscribeToMetrics(): TestProbe = { val metricsListener = TestProbe() Kamon(Metrics).subscribe(StatementsMetrics, "*", metricsListener.ref, permanently = true) // Wait for one empty snapshot before proceeding to the test. @@ -178,7 +179,6 @@ class StatementInstrumentationSpec extends TestKitBase with WordSpecLike with Ma } class NOPSlowQueryProcessor extends SlowQueryProcessor { - override def process(sql: String, executionTimeInMillis: Long, queryThresholdInMillis: Long): Unit = {/*do nothing!!!*/} + override def process(sql: String, executionTimeInMillis: Long, queryThresholdInMillis: Long): Unit = { /*do nothing!!!*/ } } - |