All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qmetric.feed.consumer.metrics.EntryConsumerWithMetrics Maven / Gradle / Ivy

Go to download

Java library for consuming HAL+JSON feeds (https://github.com/qmetric/hal-feed-server)

There is a newer version: 3.18
Show newest version
package com.qmetric.feed.consumer.metrics;

import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SlidingWindowReservoir;
import com.codahale.metrics.Timer;
import com.qmetric.feed.consumer.EntryConsumer;
import com.qmetric.feed.consumer.TrackedEntry;

public class EntryConsumerWithMetrics implements EntryConsumer
{
    private static final int MAX_SAMPLES = 100;

    private final Timer timer;

    private final Meter errorMeter;

    private final Meter successMeter;

    private final EntryConsumer next;

    public EntryConsumerWithMetrics(final MetricRegistry metricRegistry, final EntryConsumer next)
    {
        timer = metricRegistry.register("entryConsumption.timeTaken", new Timer(new SlidingWindowReservoir(MAX_SAMPLES)));
        errorMeter = metricRegistry.meter("entryConsumption.errors");
        successMeter = metricRegistry.meter("entryConsumption.success");

        this.next = next;
    }

    @Override public void consume(final TrackedEntry trackedEntry) throws Exception
    {
        final Timer.Context context = timer.time();

        try
        {
            next.consume(trackedEntry);

            successMeter.mark();
        }
        catch (Exception e)
        {
            errorMeter.mark();

            throw e;
        }
        finally
        {
            context.stop();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy