Line data Source code
1 : // Copyright (C) 2019 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.logging; 16 : 17 : import com.google.auto.value.AutoValue; 18 : import java.util.Optional; 19 : 20 : /** 21 : * The record of an operation for which the execution time was measured. 22 : * 23 : * <p>Metadata to provide additional context can be included by providing a {@link Metadata} 24 : * instance. 25 : */ 26 : @AutoValue 27 3 : public abstract class PerformanceLogRecord { 28 : /** 29 : * Creates a performance log record without meta data. 30 : * 31 : * @param operation the name of operation the is was performed 32 : * @param durationMs the execution time in milliseconds 33 : * @return the performance log record 34 : */ 35 : public static PerformanceLogRecord create(String operation, long durationMs) { 36 3 : return new AutoValue_PerformanceLogRecord(operation, durationMs, Optional.empty()); 37 : } 38 : 39 : /** 40 : * Creates a performance log record with meta data. 41 : * 42 : * @param operation the name of operation the is was performed 43 : * @param durationMs the execution time in milliseconds 44 : * @param metadata metadata 45 : * @return the performance log record 46 : */ 47 : public static PerformanceLogRecord create(String operation, long durationMs, Metadata metadata) { 48 3 : return new AutoValue_PerformanceLogRecord(operation, durationMs, Optional.of(metadata)); 49 : } 50 : 51 : public abstract String operation(); 52 : 53 : public abstract long durationMs(); 54 : 55 : public abstract Optional<Metadata> metadata(); 56 : 57 : void writeTo(PerformanceLogger performanceLogger) { 58 3 : if (metadata().isPresent()) { 59 3 : performanceLogger.log(operation(), durationMs(), metadata().get()); 60 : } else { 61 3 : performanceLogger.log(operation(), durationMs()); 62 : } 63 3 : } 64 : }