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 java.io.InputStream;
022import java.time.Instant;
023import java.util.List;
024
025import org.fcrepo.common.metrics.MetricsHelper;
026import org.fcrepo.kernel.api.RdfStream;
027import org.fcrepo.kernel.api.identifiers.FedoraId;
028import org.fcrepo.kernel.api.models.ResourceHeaders;
029import org.fcrepo.kernel.api.operations.ResourceOperation;
030import org.fcrepo.persistence.api.PersistentStorageSession;
031import org.fcrepo.persistence.api.exceptions.PersistentStorageException;
032
033import io.micrometer.core.instrument.Metrics;
034import io.micrometer.core.instrument.Timer;
035
036/**
037 * PersistentStorageSession wrapper for collecting metrics
038 *
039 * @author pwinckles
040 */
041public class OcflPersistentStorageSessionMetrics implements PersistentStorageSession {
042
043    private static final String METRIC_NAME = "fcrepo.storage.ocfl.session";
044    private static final String OPERATION = "operation";
045    private static final Timer persistTimer = Metrics.timer(METRIC_NAME, OPERATION, "persist");
046    private static final Timer getHeadersTimer = Metrics.timer(METRIC_NAME, OPERATION, "getHeaders");
047    private static final Timer getTriplesTimer = Metrics.timer(METRIC_NAME, OPERATION, "getTriples");
048    private static final Timer listVersionsTimer = Metrics.timer(METRIC_NAME, OPERATION, "listVersions");
049    private static final Timer getContentTimer = Metrics.timer(METRIC_NAME, OPERATION, "getContent");
050    private static final Timer prepareTimer = Metrics.timer(METRIC_NAME, OPERATION, "prepare");
051    private static final Timer commitTimer = Metrics.timer(METRIC_NAME, OPERATION, "commit");
052    private static final Timer rollbackTimer = Metrics.timer(METRIC_NAME, OPERATION, "rollback");
053
054    private final PersistentStorageSession delegate;
055
056    public OcflPersistentStorageSessionMetrics(final PersistentStorageSession delegate) {
057        this.delegate = delegate;
058    }
059
060    @Override
061    public String getId() {
062        return delegate.getId();
063    }
064
065    @Override
066    public void persist(final ResourceOperation operation) throws PersistentStorageException {
067        persistTimer.record(() -> {
068            delegate.persist(operation);
069        });
070    }
071
072    @Override
073    public ResourceHeaders getHeaders(final FedoraId identifier, final Instant version)
074            throws PersistentStorageException {
075        return MetricsHelper.time(getHeadersTimer, () -> {
076            return delegate.getHeaders(identifier, version);
077        });
078    }
079
080    @Override
081    public RdfStream getTriples(final FedoraId identifier, final Instant version) throws PersistentStorageException {
082        return MetricsHelper.time(getTriplesTimer, () -> {
083            return delegate.getTriples(identifier, version);
084        });
085    }
086
087    @Override
088    public InputStream getBinaryContent(final FedoraId identifier, final Instant version)
089            throws PersistentStorageException {
090        return MetricsHelper.time(getContentTimer, () -> {
091            return delegate.getBinaryContent(identifier, version);
092        });
093    }
094
095    @Override
096    public List<Instant> listVersions(final FedoraId identifier) throws PersistentStorageException {
097        return MetricsHelper.time(listVersionsTimer, () -> {
098            return delegate.listVersions(identifier);
099        });
100    }
101
102    @Override
103    public void prepare() throws PersistentStorageException {
104        prepareTimer.record(delegate::prepare);
105    }
106
107    @Override
108    public void commit() throws PersistentStorageException {
109        commitTimer.record(delegate::commit);
110    }
111
112    @Override
113    public void rollback() throws PersistentStorageException {
114        rollbackTimer.record(delegate::rollback);
115    }
116
117}