LCOV - code coverage report
Current view: top level - metrics/dropwizard - ListMetrics.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 0 29 0.0 %
Date: 2022-11-19 15:00:39 Functions: 0 4 0.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.gerrit.extensions.restapi.AuthException;
      19             : import com.google.gerrit.extensions.restapi.Response;
      20             : import com.google.gerrit.extensions.restapi.RestReadView;
      21             : import com.google.gerrit.server.config.ConfigResource;
      22             : import com.google.gerrit.server.permissions.GlobalPermission;
      23             : import com.google.gerrit.server.permissions.PermissionBackend;
      24             : import com.google.gerrit.server.permissions.PermissionBackendException;
      25             : import com.google.inject.Inject;
      26             : import java.util.ArrayList;
      27             : import java.util.List;
      28             : import java.util.Map;
      29             : import java.util.NavigableMap;
      30             : import java.util.TreeMap;
      31             : import org.kohsuke.args4j.Option;
      32             : 
      33             : class ListMetrics implements RestReadView<ConfigResource> {
      34             :   private final PermissionBackend permissionBackend;
      35             :   private final DropWizardMetricMaker metrics;
      36             : 
      37             :   @Option(name = "--data-only", usage = "return only values")
      38             :   boolean dataOnly;
      39             : 
      40           0 :   @Option(
      41             :       name = "--prefix",
      42             :       aliases = {"-p"},
      43             :       metaVar = "PREFIX",
      44             :       usage = "match metric by exact match or prefix")
      45             :   List<String> query = new ArrayList<>();
      46             : 
      47             :   @Inject
      48           0 :   ListMetrics(PermissionBackend permissionBackend, DropWizardMetricMaker metrics) {
      49           0 :     this.permissionBackend = permissionBackend;
      50           0 :     this.metrics = metrics;
      51           0 :   }
      52             : 
      53             :   @Override
      54             :   public Response<Map<String, MetricJson>> apply(ConfigResource resource)
      55             :       throws AuthException, PermissionBackendException {
      56           0 :     permissionBackend.currentUser().check(GlobalPermission.VIEW_CACHES);
      57             : 
      58           0 :     NavigableMap<String, MetricJson> out = new TreeMap<>();
      59           0 :     List<String> prefixes = new ArrayList<>(query.size());
      60           0 :     for (String q : query) {
      61           0 :       if (q.endsWith("/")) {
      62           0 :         prefixes.add(q);
      63             :       } else {
      64           0 :         Metric m = metrics.getMetric(q);
      65           0 :         if (m != null) {
      66           0 :           out.put(q, toJson(q, m));
      67             :         }
      68             :       }
      69           0 :     }
      70             : 
      71           0 :     if (query.isEmpty() || !prefixes.isEmpty()) {
      72           0 :       for (String name : metrics.getMetricNames()) {
      73           0 :         if (include(prefixes, name)) {
      74           0 :           out.put(name, toJson(name, metrics.getMetric(name)));
      75             :         }
      76           0 :       }
      77             :     }
      78             : 
      79           0 :     return Response.ok(out);
      80             :   }
      81             : 
      82             :   private MetricJson toJson(String q, Metric m) {
      83           0 :     return new MetricJson(m, metrics.getAnnotations(q), dataOnly);
      84             :   }
      85             : 
      86             :   private static boolean include(List<String> prefixes, String name) {
      87           0 :     if (prefixes.isEmpty()) {
      88           0 :       return true;
      89             :     }
      90           0 :     for (String p : prefixes) {
      91           0 :       if (name.startsWith(p)) {
      92           0 :         return true;
      93             :       }
      94           0 :     }
      95           0 :     return false;
      96             :   }
      97             : }

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