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.kernel.impl.services;
020
021import io.micrometer.core.instrument.Metrics;
022import io.micrometer.core.instrument.Timer;
023import org.fcrepo.common.metrics.MetricsHelper;
024import org.fcrepo.kernel.api.RdfStream;
025import org.fcrepo.kernel.api.identifiers.FedoraId;
026import org.fcrepo.kernel.api.models.FedoraResource;
027import org.fcrepo.kernel.api.services.ReferenceService;
028import org.springframework.beans.factory.annotation.Autowired;
029import org.springframework.beans.factory.annotation.Qualifier;
030import org.springframework.stereotype.Component;
031
032/**
033 * ReferenceService wrapper for collecting metrics
034 *
035 * @author pwinckles
036 */
037@Component("referenceService")
038public class ReferenceServiceMetrics implements ReferenceService {
039
040    private static final String METRIC_NAME = "fcrepo.db";
041    private static final String DB = "db";
042    private static final String REFERENCE = "reference";
043    private static final String OPERATION = "operation";
044
045    private static final Timer getInboundReferences = Metrics.timer(METRIC_NAME,
046            DB, REFERENCE, OPERATION, "getInboundReferences");
047    private static final Timer deleteAllReferencesTimer = Metrics.timer(METRIC_NAME,
048            DB, REFERENCE, OPERATION, "deleteAllReferences");
049    private static final Timer updateReferencesTimer = Metrics.timer(METRIC_NAME,
050            DB, REFERENCE, OPERATION, "updateReferences");
051    private static final Timer commitTransactionTimer = Metrics.timer(METRIC_NAME,
052            DB, REFERENCE, OPERATION, "commitTransaction");
053    private static final Timer rollbackTransactionTimer = Metrics.timer(METRIC_NAME,
054            DB, REFERENCE, OPERATION, "rollbackTransaction");
055    private static final Timer resetTimer = Metrics.timer(METRIC_NAME,
056            DB, REFERENCE, OPERATION, "reset");
057
058    @Autowired
059    @Qualifier("referenceServiceImpl")
060    private ReferenceService referenceServiceImpl;
061
062    @Override
063    public RdfStream getInboundReferences(final String txId, final FedoraResource resource) {
064        return MetricsHelper.time(getInboundReferences, () -> {
065            return referenceServiceImpl.getInboundReferences(txId, resource);
066        });
067    }
068
069    @Override
070    public void deleteAllReferences(final String txId, final FedoraId resourceId) {
071        deleteAllReferencesTimer.record(() -> {
072            referenceServiceImpl.deleteAllReferences(txId, resourceId);
073        });
074    }
075
076    @Override
077    public void updateReferences(final String txId,
078                                 final FedoraId resourceId,
079                                 final String userPrincipal,
080                                 final RdfStream rdfStream) {
081        updateReferencesTimer.record(() -> {
082            referenceServiceImpl.updateReferences(txId, resourceId, userPrincipal, rdfStream);
083        });
084    }
085
086    @Override
087    public void commitTransaction(final String txId) {
088        commitTransactionTimer.record(() -> {
089            referenceServiceImpl.commitTransaction(txId);
090        });
091    }
092
093    @Override
094    public void rollbackTransaction(final String txId) {
095        rollbackTransactionTimer.record(() -> {
096            referenceServiceImpl.rollbackTransaction(txId);
097        });
098    }
099
100    @Override
101    public void reset() {
102        resetTimer.record(() -> {
103            referenceServiceImpl.reset();
104        });
105    }
106
107}