Line data Source code
1 : // Copyright (C) 2015 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.metrics.dropwizard; 16 : 17 : import com.google.common.base.Joiner; 18 : import com.google.common.collect.ImmutableList; 19 : import com.google.gerrit.metrics.Description; 20 : import com.google.gerrit.metrics.Field; 21 : import com.google.gerrit.metrics.Timer2; 22 : import com.google.gerrit.metrics.Timer3; 23 : import java.util.concurrent.TimeUnit; 24 : import java.util.function.Function; 25 : 26 : /** Generalized implementation of N-dimensional timer metrics. */ 27 : class TimerImplN extends BucketedTimer implements BucketedMetric { 28 : TimerImplN(DropWizardMetricMaker metrics, String name, Description desc, Field<?>... fields) { 29 15 : super(metrics, name, desc, fields); 30 15 : } 31 : 32 : @SuppressWarnings("unchecked") 33 : <F1, F2> Timer2<F1, F2> timer2() { 34 15 : return new Timer2<>(name, (Field<F1>) fields[0], (Field<F2>) fields[1]) { 35 : @Override 36 : protected void doRecord(F1 field1, F2 field2, long value, TimeUnit unit) { 37 15 : total.record(value, unit); 38 15 : forceCreate(field1, field2).record(value, unit); 39 15 : } 40 : 41 : @Override 42 : public void remove() { 43 0 : doRemove(); 44 0 : } 45 : }; 46 : } 47 : 48 : @SuppressWarnings("unchecked") 49 : <F1, F2, F3> Timer3<F1, F2, F3> timer3() { 50 15 : return new Timer3<>(name, (Field<F1>) fields[0], (Field<F2>) fields[1], (Field<F3>) fields[2]) { 51 : @Override 52 : protected void doRecord(F1 field1, F2 field2, F3 field3, long value, TimeUnit unit) { 53 15 : total.record(value, unit); 54 15 : forceCreate(field1, field2, field3).record(value, unit); 55 15 : } 56 : 57 : @Override 58 : public void remove() { 59 0 : doRemove(); 60 0 : } 61 : }; 62 : } 63 : 64 : @SuppressWarnings("unchecked") 65 : @Override 66 : String name(Object key) { 67 15 : ImmutableList<Object> keyList = (ImmutableList<Object>) key; 68 15 : String[] parts = new String[fields.length]; 69 15 : for (int i = 0; i < fields.length; i++) { 70 15 : Function<Object, String> fmt = (Function<Object, String>) fields[i].formatter(); 71 : 72 15 : parts[i] = fmt.apply(keyList.get(i)).replace('/', '-'); 73 : } 74 15 : return Joiner.on('/').join(parts); 75 : } 76 : }