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.UsedAt; 18 : import com.google.gerrit.metrics.Counter1; 19 : import com.google.gerrit.metrics.Counter3; 20 : import com.google.gerrit.metrics.Description; 21 : import com.google.gerrit.metrics.Field; 22 : import com.google.gerrit.metrics.MetricMaker; 23 : import com.google.gerrit.server.cancellation.RequestStateProvider; 24 : import com.google.gerrit.server.logging.Metadata; 25 : import com.google.inject.Inject; 26 : import com.google.inject.Singleton; 27 : 28 : /** Metrics for request cancellations and deadlines. */ 29 : @Singleton 30 : public class CancellationMetrics { 31 : private final Counter3<String, String, String> advisoryDeadlineCount; 32 : private final Counter3<String, String, RequestStateProvider.Reason> cancelledRequestsCount; 33 : private final Counter1<String> receiveTimeoutCount; 34 : 35 : @Inject 36 125 : CancellationMetrics(MetricMaker metrics) { 37 125 : this.advisoryDeadlineCount = 38 125 : metrics.newCounter( 39 : "cancellation/advisory_deadline_count", 40 125 : new Description("Exceeded advisory deadlines by request").setRate(), 41 125 : Field.ofString("request_type", Metadata.Builder::requestType) 42 125 : .description("The type of the request to which the advisory deadline applied.") 43 125 : .build(), 44 125 : Field.ofString("request_uri", Metadata.Builder::restViewName) 45 125 : .description( 46 : "The redacted URI of the request to which the advisory deadline applied" 47 : + " (only set for request_type = REST).") 48 125 : .build(), 49 125 : Field.ofString("deadline_id", (metadataBuilder, resolveAllUsers) -> {}) 50 125 : .description("The ID of the advisory deadline.") 51 125 : .build()); 52 : 53 125 : this.cancelledRequestsCount = 54 125 : metrics.newCounter( 55 : "cancellation/cancelled_requests_count", 56 125 : new Description("Number of request cancellations by request").setRate(), 57 125 : Field.ofString("request_type", Metadata.Builder::requestType) 58 125 : .description("The type of the request that was cancelled.") 59 125 : .build(), 60 125 : Field.ofString("request_uri", Metadata.Builder::restViewName) 61 125 : .description( 62 : "The redacted URI of the request that was cancelled" 63 : + " (only set for request_type = REST).") 64 125 : .build(), 65 125 : Field.ofEnum( 66 : RequestStateProvider.Reason.class, 67 : "cancellation_reason", 68 : Metadata.Builder::cancellationReason) 69 125 : .description("The reason why the request was cancelled.") 70 125 : .build()); 71 : 72 125 : this.receiveTimeoutCount = 73 125 : metrics.newCounter( 74 : "cancellation/receive_timeout_count", 75 : new Description( 76 : "Number of requests that are cancelled because receive.timout is exceeded") 77 125 : .setRate(), 78 125 : Field.ofString("cancellation_type", (metadataBuilder, resolveAllUsers) -> {}) 79 125 : .description("The cancellation type (graceful or forceful).") 80 125 : .build()); 81 125 : } 82 : 83 : public void countAdvisoryDeadline(RequestInfo requestInfo, String deadlineId) { 84 1 : advisoryDeadlineCount.increment( 85 1 : requestInfo.requestType(), requestInfo.redactedRequestUri().orElse(""), deadlineId); 86 1 : } 87 : 88 : public void countCancelledRequest( 89 : RequestInfo requestInfo, RequestStateProvider.Reason cancellationReason) { 90 2 : cancelledRequestsCount.increment( 91 2 : requestInfo.requestType(), requestInfo.redactedRequestUri().orElse(""), cancellationReason); 92 2 : } 93 : 94 : public void countCancelledRequest( 95 : RequestInfo.RequestType requestType, 96 : String requestUri, 97 : RequestStateProvider.Reason cancellationReason) { 98 1 : cancelledRequestsCount.increment( 99 1 : requestType.name(), RequestInfo.redactRequestUri(requestUri), cancellationReason); 100 1 : } 101 : 102 : @UsedAt(UsedAt.Project.GOOGLE) 103 : public void countCancelledRequest( 104 : String requestType, 105 : String redactedRequestUri, 106 : RequestStateProvider.Reason cancellationReason) { 107 0 : cancelledRequestsCount.increment(requestType, redactedRequestUri, cancellationReason); 108 0 : } 109 : 110 : public void countGracefulReceiveTimeout() { 111 1 : receiveTimeoutCount.increment("graceful"); 112 1 : } 113 : 114 : public void countForcefulReceiveTimeout() { 115 0 : receiveTimeoutCount.increment("forceful"); 116 0 : } 117 : }