LCOV - code coverage report
Current view: top level - server - PerformanceMetrics.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 0 31 0.0 %
Date: 2022-11-19 15:00:39 Functions: 0 6 0.0 %

          Line data    Source code
       1             : // Copyright (C) 2021 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;
      16             : 
      17             : import com.google.gerrit.common.Nullable;
      18             : import com.google.gerrit.metrics.Counter3;
      19             : import com.google.gerrit.metrics.Description;
      20             : import com.google.gerrit.metrics.Field;
      21             : import com.google.gerrit.metrics.MetricMaker;
      22             : import com.google.gerrit.metrics.Timer3;
      23             : import com.google.gerrit.server.logging.Metadata;
      24             : import com.google.gerrit.server.logging.PerformanceLogger;
      25             : import com.google.gerrit.server.logging.TraceContext;
      26             : import com.google.inject.Inject;
      27             : import com.google.inject.Singleton;
      28             : import java.util.concurrent.TimeUnit;
      29             : 
      30             : /** Performance logger that records the execution times as a metric. */
      31             : @Singleton
      32             : public class PerformanceMetrics implements PerformanceLogger {
      33             :   private static final String OPERATION_LATENCY_METRIC_NAME = "performance/operations";
      34             :   private static final String OPERATION_COUNT_METRIC_NAME = "performance/operations_count";
      35             : 
      36             :   public final Timer3<String, String, String> operationsLatency;
      37             :   public final Counter3<String, String, String> operationsCounter;
      38             : 
      39             :   @Inject
      40           0 :   PerformanceMetrics(MetricMaker metricMaker) {
      41           0 :     Field<String> operationNameField =
      42           0 :         Field.ofString(
      43             :                 "operation_name",
      44           0 :                 (metadataBuilder, fieldValue) -> metadataBuilder.operationName(fieldValue))
      45           0 :             .description("The operation that was performed.")
      46           0 :             .build();
      47           0 :     Field<String> requestField =
      48           0 :         Field.ofString("request", (metadataBuilder, fieldValue) -> {})
      49           0 :             .description(
      50             :                 "The request for which the operation was performed"
      51             :                     + " (format = '<request-type> <redacted-request-uri>').")
      52           0 :             .build();
      53           0 :     Field<String> pluginField =
      54           0 :         Field.ofString(
      55           0 :                 "plugin", (metadataBuilder, fieldValue) -> metadataBuilder.pluginName(fieldValue))
      56           0 :             .description("The name of the plugin that performed the operation.")
      57           0 :             .build();
      58             : 
      59           0 :     this.operationsLatency =
      60             :         metricMaker
      61           0 :             .newTimer(
      62             :                 OPERATION_LATENCY_METRIC_NAME,
      63             :                 new Description("Latency of performing operations")
      64           0 :                     .setCumulative()
      65           0 :                     .setUnit(Description.Units.MILLISECONDS),
      66             :                 operationNameField,
      67             :                 requestField,
      68             :                 pluginField)
      69           0 :             .suppressLogging();
      70           0 :     this.operationsCounter =
      71           0 :         metricMaker.newCounter(
      72             :             OPERATION_COUNT_METRIC_NAME,
      73           0 :             new Description("Number of performed operations").setRate(),
      74             :             operationNameField,
      75             :             requestField,
      76             :             pluginField);
      77           0 :   }
      78             : 
      79             :   @Override
      80             :   public void log(String operation, long durationMs) {
      81           0 :     log(operation, durationMs, /* metadata= */ null);
      82           0 :   }
      83             : 
      84             :   @Override
      85             :   public void log(String operation, long durationMs, @Nullable Metadata metadata) {
      86           0 :     String requestTag = TraceContext.getTag(TraceRequestListener.TAG_REQUEST).orElse("");
      87           0 :     String pluginTag = TraceContext.getPluginTag().orElse("");
      88           0 :     operationsLatency.record(operation, requestTag, pluginTag, durationMs, TimeUnit.MILLISECONDS);
      89           0 :     operationsCounter.increment(operation, requestTag, pluginTag);
      90           0 :   }
      91             : }

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