Search code examples
javadropwizardmetrics

How to use timer in drop wizard?


I need to measure method execution duration statistic.
I decided to use drop wizard.

I rty to follow following article:
https://dzone.com/articles/dropwizard-part-3-measuring-your-application-metri

Looks like this appropriate for my situation:

final Timer.Context context = responses.time();//1
        try {
            String userOneName = userOne.get();
            String userTwoName = userTwo.get();
            return new ChatroomView(userOneName, userTwoName, chats.chatBetween(userOneName, userTwoName),
                chats.belongingTo(userOneName)
                    .stream()
                    .map(c -> new ChatView(userOneName, c))
                    .collect(toList()));
        } finally {
            context.stop();//2
        }

But In article I could not find what is responses object;

Plaese clarify


Solution

  • static final MetricRegistry metrics = new MetricRegistry();
    static final Timer timer = metrics.timer("MyTimer");
    
    static void startReport() {
        ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .build();
        reporter.start(1, TimeUnit.MINUTES);
    }
    
    @BeforeClass
    public static void init() {
        startReport();
    }
    

    and then:

    final Timer.Context context = timer.time();//1
    try {
        String userOneName = userOne.get();
        String userTwoName = userTwo.get();
        return new ChatroomView(userOneName, userTwoName, chats.chatBetween(userOneName, userTwoName),
            chats.belongingTo(userOneName)
                .stream()
                .map(c -> new ChatView(userOneName, c))
                .collect(toList()));
    } finally {
        context.stop();//2
    }