package com.github.arteam.jdbi3;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.github.arteam.jdbi3.strategies.SmartNameStrategy;
import com.github.arteam.jdbi3.strategies.StatementNameStrategy;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.core.statement.TimingCollector;

import java.util.concurrent.TimeUnit;

/**
 * A {@link TimingCollector} implementation for JDBI which uses the SQL objects' class names and
 * method names for millisecond-precision timers.
 */
public class InstrumentedTimingCollector implements TimingCollector {

    private final MetricRegistry registry;
    private final StatementNameStrategy statementNameStrategy;

    public InstrumentedTimingCollector(MetricRegistry registry) {
        this(registry, new SmartNameStrategy());
    }

    public InstrumentedTimingCollector(MetricRegistry registry,
                                       StatementNameStrategy statementNameStrategy) {
        this.registry = registry;
        this.statementNameStrategy = statementNameStrategy;
    }

    @Override
    public void collect(long elapsedTime, StatementContext ctx) {
        final Timer timer = getTimer(ctx);
        timer.update(elapsedTime, TimeUnit.NANOSECONDS);
    }

    private Timer getTimer(StatementContext ctx) {
        return registry.timer(statementNameStrategy.getStatementName(ctx));
    }
}

@@ -75,43 +75,8 @@
 ry;%0A
-import com.codahale.metrics.Timer;%0A
 impo
@@ -1138,154 +1138,109 @@
     
-final Timer ti
+String statementNa
 me
-r
  = 
-getTimer(ctx);%0A        timer.update(elapsedTime, TimeUnit.NANOSECONDS
+statementNameStrategy.getStatementName(ctx
 );%0A    
-%7D%0A%0A
     
-private Timer getTimer(StatementContext ctx
+if (statementName != null
 ) %7B%0A
@@ -1251,14 +1251,11 @@
     
-return
+   
  reg
@@ -1283,40 +1283,62 @@
 Name
-Strategy.getStatementName(ctx));
+).update(elapsedTime, TimeUnit.NANOSECONDS);%0A        %7D
 %0A   

Support nullable timers