LCOV - code coverage report
Current view: top level - metrics/dropwizard - BucketedHistogram.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 19 30 63.3 %
Date: 2022-11-19 15:00:39 Functions: 3 10 30.0 %

          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.codahale.metrics.Metric;
      18             : import com.google.common.collect.ImmutableList;
      19             : import com.google.common.collect.Maps;
      20             : import com.google.gerrit.metrics.Description;
      21             : import com.google.gerrit.metrics.Field;
      22             : import com.google.gerrit.metrics.dropwizard.DropWizardMetricMaker.HistogramImpl;
      23             : import java.util.Map;
      24             : import java.util.concurrent.ConcurrentHashMap;
      25             : 
      26             : /** Abstract histogram broken down into buckets by {@link Field} values. */
      27             : abstract class BucketedHistogram implements BucketedMetric {
      28             :   private final DropWizardMetricMaker metrics;
      29             :   private final String name;
      30             :   private final Description.FieldOrdering ordering;
      31             :   protected final Field<?>[] fields;
      32             :   protected final HistogramImpl total;
      33             :   private final Map<Object, HistogramImpl> cells;
      34          15 :   private final Object lock = new Object();
      35             : 
      36             :   BucketedHistogram(
      37          15 :       DropWizardMetricMaker metrics, String name, Description desc, Field<?>... fields) {
      38          15 :     this.metrics = metrics;
      39          15 :     this.name = name;
      40          15 :     this.ordering = desc.getFieldOrdering();
      41          15 :     this.fields = fields;
      42          15 :     this.total = metrics.newHistogramImpl(name + "_total");
      43          15 :     this.cells = new ConcurrentHashMap<>();
      44          15 :   }
      45             : 
      46             :   void doRemove() {
      47           0 :     for (HistogramImpl c : cells.values()) {
      48           0 :       c.remove();
      49           0 :     }
      50           0 :     total.remove();
      51           0 :     metrics.remove(name);
      52           0 :   }
      53             : 
      54             :   HistogramImpl forceCreate(Object f1, Object f2) {
      55           0 :     return forceCreate(ImmutableList.of(f1, f2));
      56             :   }
      57             : 
      58             :   HistogramImpl forceCreate(Object f1, Object f2, Object f3) {
      59           0 :     return forceCreate(ImmutableList.of(f1, f2, f3));
      60             :   }
      61             : 
      62             :   HistogramImpl forceCreate(Object key) {
      63           7 :     HistogramImpl c = cells.get(key);
      64           7 :     if (c != null) {
      65           3 :       return c;
      66             :     }
      67             : 
      68           7 :     synchronized (lock) {
      69           7 :       c = cells.get(key);
      70           7 :       if (c == null) {
      71           7 :         c = metrics.newHistogramImpl(submetric(key));
      72           7 :         cells.put(key, c);
      73             :       }
      74           7 :       return c;
      75             :     }
      76             :   }
      77             : 
      78             :   private String submetric(Object key) {
      79           7 :     return DropWizardMetricMaker.name(ordering, name, name(key));
      80             :   }
      81             : 
      82             :   abstract String name(Object key);
      83             : 
      84             :   @Override
      85             :   public Metric getTotal() {
      86           0 :     return total.metric;
      87             :   }
      88             : 
      89             :   @Override
      90             :   public Field<?>[] getFields() {
      91           0 :     return fields;
      92             :   }
      93             : 
      94             :   @Override
      95             :   public Map<Object, Metric> getCells() {
      96           0 :     return Maps.transformValues(cells, h -> h.metric);
      97             :   }
      98             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750