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.Counter2; 20 : import com.google.gerrit.metrics.Counter3; 21 : import com.google.gerrit.metrics.Description; 22 : import com.google.gerrit.metrics.Field; 23 : import java.util.function.Function; 24 : 25 : /** Generalized implementation of N-dimensional counter metrics. */ 26 : class CounterImplN extends BucketedCounter implements BucketedMetric { 27 : CounterImplN(DropWizardMetricMaker metrics, String name, Description desc, Field<?>... fields) { 28 15 : super(metrics, name, desc, fields); 29 15 : } 30 : 31 : <F1, F2> Counter2<F1, F2> counter2() { 32 15 : return new Counter2<>() { 33 : @Override 34 : public void incrementBy(F1 field1, F2 field2, long value) { 35 15 : total.incrementBy(value); 36 15 : forceCreate(field1, field2).incrementBy(value); 37 15 : } 38 : 39 : @Override 40 : public void remove() { 41 0 : doRemove(); 42 0 : } 43 : }; 44 : } 45 : 46 : <F1, F2, F3> Counter3<F1, F2, F3> counter3() { 47 15 : return new Counter3<>() { 48 : @Override 49 : public void incrementBy(F1 field1, F2 field2, F3 field3, long value) { 50 6 : total.incrementBy(value); 51 6 : forceCreate(field1, field2, field3).incrementBy(value); 52 6 : } 53 : 54 : @Override 55 : public void remove() { 56 0 : doRemove(); 57 0 : } 58 : }; 59 : } 60 : 61 : @SuppressWarnings("unchecked") 62 : @Override 63 : String name(Object key) { 64 15 : ImmutableList<Object> keyList = (ImmutableList<Object>) key; 65 15 : String[] parts = new String[fields.length]; 66 15 : for (int i = 0; i < fields.length; i++) { 67 15 : Function<Object, String> fmt = (Function<Object, String>) fields[i].formatter(); 68 : 69 15 : parts[i] = fmt.apply(keyList.get(i)).replace('/', '-'); 70 : } 71 15 : return Joiner.on('/').join(parts); 72 : } 73 : }