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 String baseMetricName, final MetricRegistry metricRegistry, final EntryConsumer next)
    {
        timer = metricRegistry.register(String.format("%s: entryConsumption.timeTaken", baseMetricName), new Timer(new SlidingWindowReservoir(MAX_SAMPLES)));
        errorMeter = metricRegistry.meter(String.format("%s: entryConsumption.errors", baseMetricName));
        successMeter = metricRegistry.meter(String.format("%s: entryConsumption.success", baseMetricName));

        this.next = next;
    }

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

        try
        {
            final boolean success = next.consume(trackedEntry);

            (success ? successMeter : errorMeter).mark();

            return success;
        }
        catch (Exception e)
        {
            errorMeter.mark();

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy