LCOV - code coverage report
Current view: top level - server/git - UploadPackMetricsHook.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 38 38 100.0 %
Date: 2022-11-19 15:00:39 Functions: 3 3 100.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.server.git;
      16             : 
      17             : import static java.util.concurrent.TimeUnit.MILLISECONDS;
      18             : 
      19             : import com.google.gerrit.metrics.Counter1;
      20             : import com.google.gerrit.metrics.Description;
      21             : import com.google.gerrit.metrics.Description.Units;
      22             : import com.google.gerrit.metrics.Field;
      23             : import com.google.gerrit.metrics.Histogram1;
      24             : import com.google.gerrit.metrics.MetricMaker;
      25             : import com.google.gerrit.metrics.Timer1;
      26             : import com.google.gerrit.server.logging.Metadata;
      27             : import com.google.inject.Inject;
      28             : import com.google.inject.Singleton;
      29             : import org.eclipse.jgit.storage.pack.PackStatistics;
      30             : import org.eclipse.jgit.transport.PostUploadHook;
      31             : 
      32             : @Singleton
      33             : public class UploadPackMetricsHook implements PostUploadHook {
      34          10 :   enum Operation {
      35          10 :     CLONE,
      36          10 :     FETCH;
      37             :   }
      38             : 
      39             :   private final Counter1<Operation> requestCount;
      40             :   private final Timer1<Operation> counting;
      41             :   private final Timer1<Operation> compressing;
      42             :   private final Timer1<Operation> writing;
      43             :   private final Histogram1<Operation> packBytes;
      44             : 
      45             :   @Inject
      46         138 :   UploadPackMetricsHook(MetricMaker metricMaker) {
      47         138 :     Field<Operation> operationField =
      48         138 :         Field.ofEnum(Operation.class, "operation", Metadata.Builder::gitOperation)
      49         138 :             .description("The name of the operation (CLONE, FETCH).")
      50         138 :             .build();
      51         138 :     requestCount =
      52         138 :         metricMaker.newCounter(
      53             :             "git/upload-pack/request_count",
      54             :             new Description("Total number of git-upload-pack requests")
      55         138 :                 .setRate()
      56         138 :                 .setUnit("requests"),
      57             :             operationField);
      58             : 
      59         138 :     counting =
      60         138 :         metricMaker.newTimer(
      61             :             "git/upload-pack/phase_counting",
      62             :             new Description("Time spent in the 'Counting...' phase")
      63         138 :                 .setCumulative()
      64         138 :                 .setUnit(Units.MILLISECONDS),
      65             :             operationField);
      66             : 
      67         138 :     compressing =
      68         138 :         metricMaker.newTimer(
      69             :             "git/upload-pack/phase_compressing",
      70             :             new Description("Time spent in the 'Compressing...' phase")
      71         138 :                 .setCumulative()
      72         138 :                 .setUnit(Units.MILLISECONDS),
      73             :             operationField);
      74             : 
      75         138 :     writing =
      76         138 :         metricMaker.newTimer(
      77             :             "git/upload-pack/phase_writing",
      78             :             new Description("Time spent transferring bytes to client")
      79         138 :                 .setCumulative()
      80         138 :                 .setUnit(Units.MILLISECONDS),
      81             :             operationField);
      82             : 
      83         138 :     packBytes =
      84         138 :         metricMaker.newHistogram(
      85             :             "git/upload-pack/pack_bytes",
      86             :             new Description("Distribution of sizes of packs sent to clients")
      87         138 :                 .setCumulative()
      88         138 :                 .setUnit(Units.BYTES),
      89             :             operationField);
      90         138 :   }
      91             : 
      92             :   @Override
      93             :   public void onPostUpload(PackStatistics stats) {
      94          10 :     Operation op = Operation.FETCH;
      95          10 :     if (stats.getUninterestingObjects() == null || stats.getUninterestingObjects().isEmpty()) {
      96          10 :       op = Operation.CLONE;
      97             :     }
      98             : 
      99          10 :     requestCount.increment(op);
     100          10 :     counting.record(op, stats.getTimeCounting(), MILLISECONDS);
     101          10 :     compressing.record(op, stats.getTimeCompressing(), MILLISECONDS);
     102          10 :     writing.record(op, stats.getTimeWriting(), MILLISECONDS);
     103          10 :     packBytes.record(op, stats.getTotalBytes());
     104          10 :   }
     105             : }

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