|
| 1 | +/* |
| 2 | + * Copyright 2026 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.internal.extproc; |
| 18 | + |
| 19 | +import com.google.common.annotations.VisibleForTesting; |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | +import io.grpc.DoubleHistogramMetricInstrument; |
| 22 | +import io.grpc.MetricInstrumentRegistry; |
| 23 | +import io.grpc.internal.GrpcUtil; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +/** |
| 27 | + * Holds metric instrument definitions for server-side external processing. |
| 28 | + */ |
| 29 | +public final class ExternalProcessorServerInterceptorMetricInstruments { |
| 30 | + @VisibleForTesting |
| 31 | + public static DoubleHistogramMetricInstrument clientHeadersDuration; |
| 32 | + @VisibleForTesting |
| 33 | + public static DoubleHistogramMetricInstrument clientHalfCloseDuration; |
| 34 | + @VisibleForTesting |
| 35 | + public static DoubleHistogramMetricInstrument serverHeadersDuration; |
| 36 | + @VisibleForTesting |
| 37 | + public static DoubleHistogramMetricInstrument serverTrailersDuration; |
| 38 | + |
| 39 | + // Copied from io.grpc.opentelemetry.internal.OpenTelemetryConstants.LATENCY_BUCKETS |
| 40 | + private static final List<Double> LATENCY_BUCKETS = ImmutableList.of( |
| 41 | + 0d, 0.00001d, 0.00005d, 0.0001d, 0.0003d, 0.0006d, 0.0008d, 0.001d, 0.002d, |
| 42 | + 0.003d, 0.004d, 0.005d, 0.006d, 0.008d, 0.01d, 0.013d, 0.016d, 0.02d, |
| 43 | + 0.025d, 0.03d, 0.04d, 0.05d, 0.065d, 0.08d, 0.1d, 0.13d, 0.16d, |
| 44 | + 0.2d, 0.25d, 0.3d, 0.4d, 0.5d, 0.65d, 0.8d, 1d, 2d, |
| 45 | + 5d, 10d, 20d, 50d, 100d); |
| 46 | + |
| 47 | + static { |
| 48 | + initMetricInstruments(); |
| 49 | + } |
| 50 | + |
| 51 | + public static synchronized void initMetricInstruments() { |
| 52 | + if (GrpcUtil.getFlag("GRPC_EXPERIMENTAL_XDS_EXT_PROC_ON_SERVER", false)) { |
| 53 | + if (clientHeadersDuration == null) { |
| 54 | + MetricInstrumentRegistry registry = MetricInstrumentRegistry.getDefaultRegistry(); |
| 55 | + |
| 56 | + clientHeadersDuration = registry.registerDoubleHistogram( |
| 57 | + "grpc.server_ext_proc.client_headers_duration", |
| 58 | + "Time between when the ext_proc filter sees the client's headers and when " |
| 59 | + + "it allows those headers to continue on to the next filter", |
| 60 | + "s", |
| 61 | + LATENCY_BUCKETS, |
| 62 | + ImmutableList.of(), |
| 63 | + ImmutableList.of(), |
| 64 | + true); |
| 65 | + |
| 66 | + clientHalfCloseDuration = registry.registerDoubleHistogram( |
| 67 | + "grpc.server_ext_proc.client_half_close_duration", |
| 68 | + "Time between when the ext_proc filter sees the client's half-close and when " |
| 69 | + + "it allows that half-close to continue on to the next filter", |
| 70 | + "s", |
| 71 | + LATENCY_BUCKETS, |
| 72 | + ImmutableList.of(), |
| 73 | + ImmutableList.of(), |
| 74 | + true); |
| 75 | + |
| 76 | + serverHeadersDuration = registry.registerDoubleHistogram( |
| 77 | + "grpc.server_ext_proc.server_headers_duration", |
| 78 | + "Time between when the ext_proc filter sees the server's headers and when " |
| 79 | + + "it allows those headers to continue on to the next filter", |
| 80 | + "s", |
| 81 | + LATENCY_BUCKETS, |
| 82 | + ImmutableList.of(), |
| 83 | + ImmutableList.of(), |
| 84 | + true); |
| 85 | + |
| 86 | + serverTrailersDuration = registry.registerDoubleHistogram( |
| 87 | + "grpc.server_ext_proc.server_trailers_duration", |
| 88 | + "Time between when the ext_proc filter sees the server's trailers and when " |
| 89 | + + "it allows those trailers to continue on to the next filter", |
| 90 | + "s", |
| 91 | + LATENCY_BUCKETS, |
| 92 | + ImmutableList.of(), |
| 93 | + ImmutableList.of(), |
| 94 | + true); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private ExternalProcessorServerInterceptorMetricInstruments() {} |
| 100 | +} |
0 commit comments