|
| 1 | +/* |
| 2 | + * Copyright 2024 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; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +/** |
| 25 | + * TCP Metrics defined to be shared across transport implementations. |
| 26 | + */ |
| 27 | +@Internal |
| 28 | +public final class InternalTcpMetrics { |
| 29 | + |
| 30 | + private InternalTcpMetrics() { |
| 31 | + } |
| 32 | + |
| 33 | + private static final List<String> OPTIONAL_LABELS = Arrays.asList( |
| 34 | + "network.local.address", |
| 35 | + "network.local.port", |
| 36 | + "network.peer.address", |
| 37 | + "network.peer.port"); |
| 38 | + |
| 39 | + public static final DoubleHistogramMetricInstrument MIN_RTT_INSTRUMENT = |
| 40 | + MetricInstrumentRegistry.getDefaultRegistry() |
| 41 | + .registerDoubleHistogram( |
| 42 | + "grpc.tcp.min_rtt", |
| 43 | + "Minimum round-trip time of a TCP connection", |
| 44 | + "s", |
| 45 | + getMinRttBuckets(), |
| 46 | + Collections.emptyList(), |
| 47 | + OPTIONAL_LABELS, |
| 48 | + false); |
| 49 | + |
| 50 | + public static final LongCounterMetricInstrument CONNECTIONS_CREATED_INSTRUMENT = |
| 51 | + MetricInstrumentRegistry |
| 52 | + .getDefaultRegistry() |
| 53 | + .registerLongCounter( |
| 54 | + "grpc.tcp.connections_created", |
| 55 | + "The total number of TCP connections established.", |
| 56 | + "{connection}", |
| 57 | + Collections.emptyList(), |
| 58 | + OPTIONAL_LABELS, |
| 59 | + false); |
| 60 | + |
| 61 | + public static final LongUpDownCounterMetricInstrument CONNECTION_COUNT_INSTRUMENT = |
| 62 | + MetricInstrumentRegistry |
| 63 | + .getDefaultRegistry() |
| 64 | + .registerLongUpDownCounter( |
| 65 | + "grpc.tcp.connection_count", |
| 66 | + "The current number of active TCP connections.", |
| 67 | + "{connection}", |
| 68 | + Collections.emptyList(), |
| 69 | + OPTIONAL_LABELS, |
| 70 | + false); |
| 71 | + |
| 72 | + public static final LongCounterMetricInstrument PACKETS_RETRANSMITTED_INSTRUMENT = |
| 73 | + MetricInstrumentRegistry |
| 74 | + .getDefaultRegistry() |
| 75 | + .registerLongCounter( |
| 76 | + "grpc.tcp.packets_retransmitted", |
| 77 | + "The total number of packets retransmitted for all TCP connections.", |
| 78 | + "{packet}", |
| 79 | + Collections.emptyList(), |
| 80 | + OPTIONAL_LABELS, |
| 81 | + false); |
| 82 | + |
| 83 | + public static final LongCounterMetricInstrument RECURRING_RETRANSMITS_INSTRUMENT = |
| 84 | + MetricInstrumentRegistry |
| 85 | + .getDefaultRegistry() |
| 86 | + .registerLongCounter( |
| 87 | + "grpc.tcp.recurring_retransmits", |
| 88 | + "The total number of times the retransmit timer popped for all TCP" |
| 89 | + + " connections.", |
| 90 | + "{timeout}", |
| 91 | + Collections.emptyList(), |
| 92 | + OPTIONAL_LABELS, |
| 93 | + false); |
| 94 | + |
| 95 | + private static List<Double> getMinRttBuckets() { |
| 96 | + List<Double> buckets = new ArrayList<>(100); |
| 97 | + for (int i = 1; i <= 100; i++) { |
| 98 | + buckets.add(1e-6 * Math.pow(2.0, i * 0.24)); |
| 99 | + } |
| 100 | + return Collections.unmodifiableList(buckets); |
| 101 | + } |
| 102 | +} |
0 commit comments