1 /* 2 * Copyright 2021 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc.xds; 18 19 import com.google.auto.value.AutoValue; 20 import com.google.common.collect.ImmutableList; 21 import javax.annotation.Nullable; 22 23 /** Represents client load stats. */ 24 final class Stats { Stats()25 private Stats() {} 26 27 /** Cluster-level load stats. */ 28 @AutoValue 29 abstract static class ClusterStats { clusterName()30 abstract String clusterName(); 31 32 @Nullable clusterServiceName()33 abstract String clusterServiceName(); 34 upstreamLocalityStatsList()35 abstract ImmutableList<UpstreamLocalityStats> upstreamLocalityStatsList(); 36 droppedRequestsList()37 abstract ImmutableList<DroppedRequests> droppedRequestsList(); 38 totalDroppedRequests()39 abstract long totalDroppedRequests(); 40 loadReportIntervalNano()41 abstract long loadReportIntervalNano(); 42 newBuilder()43 static Builder newBuilder() { 44 return new AutoValue_Stats_ClusterStats.Builder() 45 .totalDroppedRequests(0L) // default initialization 46 .loadReportIntervalNano(0L); 47 } 48 49 @AutoValue.Builder 50 abstract static class Builder { clusterName(String clusterName)51 abstract Builder clusterName(String clusterName); 52 clusterServiceName(String clusterServiceName)53 abstract Builder clusterServiceName(String clusterServiceName); 54 upstreamLocalityStatsListBuilder()55 abstract ImmutableList.Builder<UpstreamLocalityStats> upstreamLocalityStatsListBuilder(); 56 addUpstreamLocalityStats(UpstreamLocalityStats upstreamLocalityStats)57 Builder addUpstreamLocalityStats(UpstreamLocalityStats upstreamLocalityStats) { 58 upstreamLocalityStatsListBuilder().add(upstreamLocalityStats); 59 return this; 60 } 61 droppedRequestsListBuilder()62 abstract ImmutableList.Builder<DroppedRequests> droppedRequestsListBuilder(); 63 addDroppedRequests(DroppedRequests droppedRequests)64 Builder addDroppedRequests(DroppedRequests droppedRequests) { 65 droppedRequestsListBuilder().add(droppedRequests); 66 return this; 67 } 68 totalDroppedRequests(long totalDroppedRequests)69 abstract Builder totalDroppedRequests(long totalDroppedRequests); 70 loadReportIntervalNano(long loadReportIntervalNano)71 abstract Builder loadReportIntervalNano(long loadReportIntervalNano); 72 loadReportIntervalNano()73 abstract long loadReportIntervalNano(); 74 build()75 abstract ClusterStats build(); 76 } 77 } 78 79 /** Stats for dropped requests. */ 80 @AutoValue 81 abstract static class DroppedRequests { category()82 abstract String category(); 83 droppedCount()84 abstract long droppedCount(); 85 create(String category, long droppedCount)86 static DroppedRequests create(String category, long droppedCount) { 87 return new AutoValue_Stats_DroppedRequests(category, droppedCount); 88 } 89 } 90 91 /** Load stats aggregated in locality level. */ 92 @AutoValue 93 abstract static class UpstreamLocalityStats { locality()94 abstract Locality locality(); 95 totalIssuedRequests()96 abstract long totalIssuedRequests(); 97 totalSuccessfulRequests()98 abstract long totalSuccessfulRequests(); 99 totalErrorRequests()100 abstract long totalErrorRequests(); 101 totalRequestsInProgress()102 abstract long totalRequestsInProgress(); 103 create(Locality locality, long totalIssuedRequests, long totalSuccessfulRequests, long totalErrorRequests, long totalRequestsInProgress)104 static UpstreamLocalityStats create(Locality locality, long totalIssuedRequests, 105 long totalSuccessfulRequests, long totalErrorRequests, long totalRequestsInProgress) { 106 return new AutoValue_Stats_UpstreamLocalityStats(locality, totalIssuedRequests, 107 totalSuccessfulRequests, totalErrorRequests, totalRequestsInProgress); 108 } 109 } 110 } 111