001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.fcrepo.persistence.ocfl.impl;
020
021import io.micrometer.core.instrument.Metrics;
022import io.micrometer.core.instrument.Timer;
023import org.fcrepo.common.metrics.MetricsHelper;
024import org.fcrepo.kernel.api.Transaction;
025import org.fcrepo.kernel.api.identifiers.FedoraId;
026import org.fcrepo.persistence.ocfl.api.FedoraOcflMappingNotFoundException;
027import org.fcrepo.persistence.ocfl.api.FedoraToOcflObjectIndex;
028import org.springframework.beans.factory.annotation.Autowired;
029import org.springframework.stereotype.Component;
030
031/**
032 * Wrapper for FedoraToOcflObjectIndex that adds metrics
033 *
034 * @author pwinckles
035 */
036@Component("ocflIndex")
037public class FedoraToOcflObjectIndexMetrics implements FedoraToOcflObjectIndex {
038
039    private static final String METRIC_NAME = "fcrepo.db";
040    private static final String DB = "db";
041    private static final String OCFL = "ocfl";
042    private static final String OPERATION = "operation";
043
044    private static final Timer getMappingTimer = Metrics.timer(METRIC_NAME,
045            DB, OCFL, OPERATION, "getMapping");
046    private static final Timer addMappingTimer = Metrics.timer(METRIC_NAME,
047            DB, OCFL, OPERATION, "addMapping");
048    private static final Timer removeMappingTimer = Metrics.timer(METRIC_NAME,
049            DB, OCFL, OPERATION, "removeMapping");
050    private static final Timer resetTimer = Metrics.timer(METRIC_NAME,
051            DB, OCFL, OPERATION, "reset");
052    private static final Timer commitTimer = Metrics.timer(METRIC_NAME,
053            DB, OCFL, OPERATION, "commit");
054    private static final Timer rollbackTimer = Metrics.timer(METRIC_NAME,
055            DB, OCFL, OPERATION, "rollback");
056
057    @Autowired
058    private FedoraToOcflObjectIndex ocflIndexImpl;
059
060    @Override
061    public FedoraOcflMapping getMapping(final Transaction session, final FedoraId fedoraResourceIdentifier)
062            throws FedoraOcflMappingNotFoundException {
063        final var stopwatch = Timer.start();
064        try  {
065            return ocflIndexImpl.getMapping(session, fedoraResourceIdentifier);
066        } finally {
067            stopwatch.stop(getMappingTimer);
068        }
069    }
070
071    @Override
072    public FedoraOcflMapping addMapping(final Transaction session,
073                                        final FedoraId fedoraResourceIdentifier,
074                                        final FedoraId fedoraRootObjectIdentifier,
075                                        final String ocflObjectId) {
076        return MetricsHelper.time(addMappingTimer, () -> {
077            return ocflIndexImpl.addMapping(session, fedoraResourceIdentifier,
078                    fedoraRootObjectIdentifier, ocflObjectId);
079        });
080    }
081
082    @Override
083    public void removeMapping(final Transaction session, final FedoraId fedoraResourceIdentifier) {
084        removeMappingTimer.record(() -> {
085            ocflIndexImpl.removeMapping(session, fedoraResourceIdentifier);
086        });
087    }
088
089    @Override
090    public void reset() {
091        resetTimer.record(() -> {
092            ocflIndexImpl.reset();
093        });
094    }
095
096    @Override
097    public void commit(final Transaction session) {
098        commitTimer.record(() -> {
099            ocflIndexImpl.commit(session);
100        });
101    }
102
103    @Override
104    public void rollback(final Transaction session) {
105        rollbackTimer.record(() -> {
106            ocflIndexImpl.rollback(session);
107        });
108    }
109
110}