|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2023 The gRPC-Spring 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 net.devh.boot.grpc.server.metrics; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | + |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
| 23 | +import java.util.concurrent.atomic.AtomicLongFieldUpdater; |
| 24 | +import java.util.function.Supplier; |
| 25 | + |
| 26 | +import com.google.common.base.Stopwatch; |
| 27 | + |
| 28 | +import io.grpc.Metadata; |
| 29 | +import io.grpc.ServerStreamTracer; |
| 30 | +import io.grpc.Status; |
| 31 | +import io.micrometer.core.instrument.MeterRegistry; |
| 32 | +import io.micrometer.core.instrument.Tags; |
| 33 | + |
| 34 | +/** |
| 35 | + * Provides factories for {@link io.grpc.StreamTracer} that records metrics. |
| 36 | + * |
| 37 | + * <p> |
| 38 | + * On the server-side, there is only one ServerStream per each ServerCall, and ServerStream starts earlier than the |
| 39 | + * ServerCall. Therefore, only one tracer is created per stream/call and it's the tracer that reports the metrics |
| 40 | + * summary. |
| 41 | + * |
| 42 | + * <b>Note:</b> This class uses experimental grpc-java-API features. |
| 43 | + */ |
| 44 | +public final class MetricsServerStreamTracers { |
| 45 | + |
| 46 | + private static final Supplier<Stopwatch> STOPWATCH_SUPPLIER = Stopwatch::createUnstarted; |
| 47 | + private final Supplier<Stopwatch> stopwatchSupplier; |
| 48 | + |
| 49 | + public MetricsServerStreamTracers() { |
| 50 | + this(STOPWATCH_SUPPLIER); |
| 51 | + } |
| 52 | + |
| 53 | + public MetricsServerStreamTracers(Supplier<Stopwatch> stopwatchSupplier) { |
| 54 | + this.stopwatchSupplier = checkNotNull(stopwatchSupplier, "stopwatchSupplier"); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns a {@link io.grpc.ServerStreamTracer.Factory} with default metrics definitions. |
| 59 | + * |
| 60 | + * @param registry The MeterRegistry used to create the metrics. |
| 61 | + */ |
| 62 | + public ServerStreamTracer.Factory getMetricsServerTracerFactory(MeterRegistry registry) { |
| 63 | + return new MetricsServerTracerFactory(registry); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns a {@link io.grpc.ServerStreamTracer.Factory} with metrics definitions from custom |
| 68 | + * {@link MetricsServerMeters}. |
| 69 | + * |
| 70 | + * @param meters The MetricsServerMeters used to configure the metrics definitions. |
| 71 | + */ |
| 72 | + public ServerStreamTracer.Factory getMetricsServerTracerFactory(MetricsServerMeters meters) { |
| 73 | + return new MetricsServerTracerFactory(meters); |
| 74 | + } |
| 75 | + |
| 76 | + private static final class ServerTracer extends ServerStreamTracer { |
| 77 | + private final MetricsServerStreamTracers tracer; |
| 78 | + private final String fullMethodName; |
| 79 | + private final MetricsServerMeters metricsServerMeters; |
| 80 | + private final Stopwatch stopwatch; |
| 81 | + private static final AtomicLongFieldUpdater<ServerTracer> outboundWireSizeUpdater = |
| 82 | + AtomicLongFieldUpdater.newUpdater(ServerTracer.class, "outboundWireSize"); |
| 83 | + private static final AtomicLongFieldUpdater<ServerTracer> inboundWireSizeUpdater = |
| 84 | + AtomicLongFieldUpdater.newUpdater(ServerTracer.class, "inboundWireSize"); |
| 85 | + private static final AtomicIntegerFieldUpdater<ServerTracer> streamClosedUpdater = |
| 86 | + AtomicIntegerFieldUpdater.newUpdater(ServerTracer.class, "streamClosed"); |
| 87 | + private volatile long outboundWireSize; |
| 88 | + private volatile long inboundWireSize; |
| 89 | + private volatile int streamClosed; |
| 90 | + |
| 91 | + |
| 92 | + ServerTracer(MetricsServerStreamTracers tracer, String fullMethodName, MetricsServerMeters meters) { |
| 93 | + this.tracer = checkNotNull(tracer, "tracer"); |
| 94 | + this.fullMethodName = fullMethodName; |
| 95 | + this.metricsServerMeters = meters; |
| 96 | + // start stopwatch |
| 97 | + this.stopwatch = tracer.stopwatchSupplier.get().start(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void serverCallStarted(ServerCallInfo<?, ?> callInfo) { |
| 102 | + this.metricsServerMeters.getServerCallCounter() |
| 103 | + .withTags(Tags.of("grpc.method", this.fullMethodName)) |
| 104 | + .increment(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void outboundWireSize(long bytes) { |
| 109 | + outboundWireSizeUpdater.getAndAdd(this, bytes); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void inboundWireSize(long bytes) { |
| 114 | + inboundWireSizeUpdater.getAndAdd(this, bytes); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void streamClosed(Status status) { |
| 119 | + if (streamClosedUpdater.getAndSet(this, 1) != 0) { |
| 120 | + return; |
| 121 | + } |
| 122 | + long callLatencyNanos = stopwatch.elapsed(TimeUnit.NANOSECONDS); |
| 123 | + |
| 124 | + Tags serverMetricTags = |
| 125 | + Tags.of("grpc.method", this.fullMethodName, "grpc.status", status.getCode().toString()); |
| 126 | + this.metricsServerMeters.getServerCallDuration() |
| 127 | + .withTags(serverMetricTags) |
| 128 | + .record(callLatencyNanos, TimeUnit.NANOSECONDS); |
| 129 | + this.metricsServerMeters.getSentMessageSizeDistribution() |
| 130 | + .withTags(serverMetricTags) |
| 131 | + .record(outboundWireSize); |
| 132 | + this.metricsServerMeters.getReceivedMessageSizeDistribution() |
| 133 | + .withTags(serverMetricTags) |
| 134 | + .record(inboundWireSize); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + final class MetricsServerTracerFactory extends ServerStreamTracer.Factory { |
| 139 | + |
| 140 | + private final MetricsServerMeters metricsServerMeters; |
| 141 | + |
| 142 | + MetricsServerTracerFactory(MeterRegistry registry) { |
| 143 | + this(MetricsServerInstruments.newServerMetricsMeters(registry)); |
| 144 | + } |
| 145 | + |
| 146 | + MetricsServerTracerFactory(MetricsServerMeters metricsServerMeters) { |
| 147 | + this.metricsServerMeters = metricsServerMeters; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public ServerStreamTracer newServerStreamTracer(String fullMethodName, Metadata headers) { |
| 152 | + return new ServerTracer(MetricsServerStreamTracers.this, fullMethodName, this.metricsServerMeters); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments