001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.common.metrics;
007
008import io.micrometer.core.instrument.Timer;
009import org.fcrepo.common.lang.UncheckedCallable;
010
011/**
012 * Helper for recording metrics
013 *
014 * @author pwinckles
015 */
016public final class MetricsHelper {
017
018    private MetricsHelper() {
019        // static class
020    }
021
022    /**
023     * Records a timing metric around the code in the closure.
024     *
025     * @param timer the timer to record to
026     * @param callable the closure to time
027     * @param <T> the return type
028     * @return the result of the closure
029     */
030    public static <T> T time(final Timer timer, final UncheckedCallable<T> callable) {
031        final var stopwatch = Timer.start();
032        try {
033            return callable.call();
034        } finally {
035            stopwatch.stop(timer);
036        }
037    }
038
039}