Line data Source code
1 : // Copyright (C) 2022 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.config; 16 : 17 : import com.google.gerrit.metrics.MetricsReservoirConfig; 18 : import com.google.gerrit.metrics.ReservoirType; 19 : import com.google.inject.Inject; 20 : import com.google.inject.Singleton; 21 : import java.time.Duration; 22 : import java.util.Optional; 23 : import java.util.concurrent.TimeUnit; 24 : import org.eclipse.jgit.lib.Config; 25 : 26 : /** Define metrics reservoir settings based on gerrit.config */ 27 : @Singleton 28 : public class MetricsReservoirConfigImpl implements MetricsReservoirConfig { 29 : private static final double RESERVOIR_ALPHA_DEFAULT = 0.015; 30 : private static final int METRICS_RESERVOIR_SIZE_DEFAULT = 1028; 31 : private static final long METRICS_RESERVOIR_WINDOW_MSEC_DEFAULT = 60000L; 32 : private static final String METRICS_SECTION = "metrics"; 33 : private static final String METRICS_RESERVOIR = "reservoir"; 34 : 35 : private final ReservoirType reservoirType; 36 : 37 : private final Duration reservoirWindow; 38 : private final int reservoirSize; 39 : private final double reservoirAlpha; 40 : 41 : @Inject 42 138 : MetricsReservoirConfigImpl(@GerritServerConfig Config gerritConfig) { 43 138 : this.reservoirType = 44 138 : gerritConfig.getEnum( 45 : METRICS_SECTION, null, METRICS_RESERVOIR, ReservoirType.ExponentiallyDecaying); 46 : 47 138 : reservoirWindow = 48 138 : Duration.ofMillis( 49 138 : ConfigUtil.getTimeUnit( 50 : gerritConfig, 51 : METRICS_SECTION, 52 138 : reservoirType.name(), 53 : "window", 54 : METRICS_RESERVOIR_WINDOW_MSEC_DEFAULT, 55 : TimeUnit.MILLISECONDS)); 56 138 : reservoirSize = 57 138 : gerritConfig.getInt( 58 138 : METRICS_SECTION, reservoirType.name(), "size", METRICS_RESERVOIR_SIZE_DEFAULT); 59 138 : reservoirAlpha = 60 138 : Optional.ofNullable(gerritConfig.getString(METRICS_SECTION, reservoirType.name(), "alpha")) 61 138 : .map(Double::parseDouble) 62 138 : .orElse(RESERVOIR_ALPHA_DEFAULT); 63 138 : } 64 : 65 : /* (non-Javadoc) 66 : * @see com.google.gerrit.server.config.MetricsConfig#reservoirType() 67 : */ 68 : @Override 69 : public ReservoirType reservoirType() { 70 15 : return reservoirType; 71 : } 72 : 73 : /* (non-Javadoc) 74 : * @see com.google.gerrit.server.config.MetricsConfig#reservoirWindow() 75 : */ 76 : @Override 77 : public Duration reservoirWindow() { 78 0 : return reservoirWindow; 79 : } 80 : 81 : /* (non-Javadoc) 82 : * @see com.google.gerrit.server.config.MetricsConfig#reservoirSize() 83 : */ 84 : @Override 85 : public int reservoirSize() { 86 15 : return reservoirSize; 87 : } 88 : 89 : /* (non-Javadoc) 90 : * @see com.google.gerrit.server.config.MetricsConfig#reservoirAlpha() 91 : */ 92 : @Override 93 : public double reservoirAlpha() { 94 15 : return reservoirAlpha; 95 : } 96 : }