|
| 1 | +/* |
| 2 | + * Copyright 2025 LINE Corporation |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.linecorp.armeria.client.metric; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.concurrent.ConcurrentHashMap; |
| 22 | +import java.util.concurrent.ConcurrentMap; |
| 23 | +import java.util.concurrent.atomic.LongAdder; |
| 24 | + |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import com.linecorp.armeria.client.endpoint.EndpointGroup; |
| 29 | +import com.linecorp.armeria.common.SessionProtocol; |
| 30 | +import com.linecorp.armeria.common.annotation.Nullable; |
| 31 | + |
| 32 | +/** |
| 33 | + * Collects simple client-side metrics such as: |
| 34 | + * <ul> |
| 35 | + * <li>The number of pending requests per {@link SessionProtocol}</li> |
| 36 | + * <li>The number of active requests per {@link EndpointGroup}</li> |
| 37 | + * </ul> |
| 38 | + * This class is intended to be used as an in-memory counter. |
| 39 | + */ |
| 40 | +public class ClientMetrics { |
| 41 | + |
| 42 | + private static final Logger logger = LoggerFactory.getLogger(ClientMetrics.class); |
| 43 | + |
| 44 | + private final LongAdder httpsPendingRequest; |
| 45 | + private final LongAdder httpPendingRequest; |
| 46 | + private final LongAdder http1PendingRequest; |
| 47 | + private final LongAdder http2PendingRequest; |
| 48 | + private final LongAdder proxyPendingRequest; |
| 49 | + |
| 50 | + // EndpointGroup does not override equals() and hashCode(). |
| 51 | + // Call sites must use the same 'EndpointGroup' instance when invoking |
| 52 | + // 'incrementActiveRequest(...)' and 'decrementActiveRequest'. |
| 53 | + private final ConcurrentMap<EndpointGroup, LongAdder> activeRequestsPerEndpointGroup; |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a new instance with all counters initialized to zero. |
| 57 | + */ |
| 58 | + public ClientMetrics() { |
| 59 | + this.activeRequestsPerEndpointGroup = new ConcurrentHashMap<>(); |
| 60 | + this.httpsPendingRequest = new LongAdder(); |
| 61 | + this.httpPendingRequest = new LongAdder(); |
| 62 | + this.http1PendingRequest = new LongAdder(); |
| 63 | + this.http2PendingRequest = new LongAdder(); |
| 64 | + this.proxyPendingRequest = new LongAdder(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Increments the number of active requests for the |
| 69 | + * specified {@link EndpointGroup}. |
| 70 | + * @param endpointGroup the endpoint group. |
| 71 | + */ |
| 72 | + public void incrementActiveRequest(EndpointGroup endpointGroup) { |
| 73 | + if (endpointGroup == null) { |
| 74 | + return; |
| 75 | + } |
| 76 | + this.activeRequestsPerEndpointGroup |
| 77 | + .computeIfAbsent(endpointGroup, unusedKey -> new LongAdder()) |
| 78 | + .increment(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Decrements the number of active requests for the |
| 83 | + * specified {@link EndpointGroup}. If the counter for the |
| 84 | + * {@code endpointGroup} becomes zero after decrement, |
| 85 | + * the entry is removed from {@code activeRequestsPerEndpointGroup}. |
| 86 | + * @param endpointGroup the endpoint group. |
| 87 | + */ |
| 88 | + public void decrementActiveRequest(EndpointGroup endpointGroup) { |
| 89 | + if (endpointGroup == null) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + activeRequestsPerEndpointGroup.computeIfPresent(endpointGroup, (key, counter) -> { |
| 94 | + counter.decrement(); |
| 95 | + final long currentCount = counter.sum(); |
| 96 | + assert currentCount >= 0; |
| 97 | + return currentCount == 0 ? null : counter; |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Decrements the number of pending requests for the given {@link SessionProtocol}. |
| 103 | + * @param desiredProtocol the desired protocol. |
| 104 | + */ |
| 105 | + public void decrementPendingRequest(SessionProtocol desiredProtocol) { |
| 106 | + final LongAdder counter = counter(desiredProtocol); |
| 107 | + if (counter != null) { |
| 108 | + counter.decrement(); |
| 109 | + assert counter.sum() >= 0; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Increments the number of pending requests for the given {@link SessionProtocol}. |
| 115 | + * @param desiredProtocol the desired protocol. |
| 116 | + */ |
| 117 | + public void incrementPendingRequest(SessionProtocol desiredProtocol) { |
| 118 | + final LongAdder counter = counter(desiredProtocol); |
| 119 | + if (counter != null) { |
| 120 | + counter.increment(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Returns the total number of pending requests across all supported protocols. |
| 126 | + * @return the total number of pending requests |
| 127 | + */ |
| 128 | + public long pendingRequests() { |
| 129 | + return httpPendingRequests() + |
| 130 | + httpsPendingRequests() + |
| 131 | + http1PendingRequests() + |
| 132 | + http2PendingRequests() + |
| 133 | + proxyPendingRequests(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns the count of http pending requests. |
| 138 | + * @return the count of http pending requests. |
| 139 | + */ |
| 140 | + public long httpPendingRequests() { |
| 141 | + return httpPendingRequest.sum(); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Returns the count of https pending requests. |
| 146 | + * @return the count of https pending requests. |
| 147 | + */ |
| 148 | + public long httpsPendingRequests() { |
| 149 | + return httpsPendingRequest.sum(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns the count of http1 pending requests. |
| 154 | + * @return the count of http1 pending requests. |
| 155 | + */ |
| 156 | + public long http1PendingRequests() { |
| 157 | + return http1PendingRequest.sum(); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Returns the count of http2 pending requests. |
| 162 | + * @return the count of http2 pending requests. |
| 163 | + */ |
| 164 | + public long http2PendingRequests() { |
| 165 | + return http2PendingRequest.sum(); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Returns the count of proxy pending requests. |
| 170 | + * @return the count of proxy pending requests. |
| 171 | + */ |
| 172 | + public long proxyPendingRequests() { |
| 173 | + return proxyPendingRequest.sum(); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Returns a snapshot of the number of active requests per {@link EndpointGroup}. |
| 178 | + * @return a map whose key is an {@link EndpointGroup} and whose value is the number of |
| 179 | + * active requests currently associated with it |
| 180 | + */ |
| 181 | + public Map<EndpointGroup, Long> activeRequestPerEndpointGroup() { |
| 182 | + final Map<EndpointGroup, Long> result = new HashMap<>(); |
| 183 | + for (Map.Entry<EndpointGroup, LongAdder> entry : activeRequestsPerEndpointGroup.entrySet()) { |
| 184 | + final EndpointGroup key = entry.getKey(); |
| 185 | + final LongAdder value = entry.getValue(); |
| 186 | + result.put(key, value.sum()); |
| 187 | + } |
| 188 | + |
| 189 | + return result; |
| 190 | + } |
| 191 | + |
| 192 | + @Nullable |
| 193 | + private LongAdder counter(SessionProtocol desiredProtocol) { |
| 194 | + switch (desiredProtocol) { |
| 195 | + case HTTPS: |
| 196 | + return httpsPendingRequest; |
| 197 | + case HTTP: |
| 198 | + return httpPendingRequest; |
| 199 | + case H1: |
| 200 | + return http1PendingRequest; |
| 201 | + case H1C: |
| 202 | + return http1PendingRequest; |
| 203 | + case H2: |
| 204 | + return http2PendingRequest; |
| 205 | + case H2C: |
| 206 | + return http2PendingRequest; |
| 207 | + case PROXY: |
| 208 | + return proxyPendingRequest; |
| 209 | + default: |
| 210 | + // To prevent log explosion in production environment. |
| 211 | + logger.debug("Unexpected SessionProtocol: {}", desiredProtocol); |
| 212 | + return null; |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments