|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package com.google.api.gax.tracing; |
| 31 | + |
| 32 | +import static com.google.api.gax.tracing.ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE; |
| 33 | + |
| 34 | +import com.google.api.gax.rpc.StatusCode; |
| 35 | +import com.google.common.annotations.VisibleForTesting; |
| 36 | +import com.google.common.base.Stopwatch; |
| 37 | +import com.google.common.base.Ticker; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.concurrent.TimeUnit; |
| 41 | + |
| 42 | +/** |
| 43 | + * This class computes golden signal metrics that can be observed in the lifecycle of an RPC |
| 44 | + * operation. The responsibility of recording metrics should delegate to {@link |
| 45 | + * GoldenSignalsMetricsRecorder}, hence this class should not have any knowledge about the |
| 46 | + * observability framework (e.g. OpenTelemetry). |
| 47 | + */ |
| 48 | +class GoldenSignalsMetricsTracer implements ApiTracer { |
| 49 | + private final Stopwatch clientRequestTimer; |
| 50 | + private final GoldenSignalsMetricsRecorder metricsRecorder; |
| 51 | + private final Map<String, String> attributes = new HashMap<>(); |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates the following instruments for the following metrics: |
| 55 | + * |
| 56 | + * <ul> |
| 57 | + * <li>Client Request Duration: Histogram |
| 58 | + * </ul> |
| 59 | + * |
| 60 | + * @param metricsRecorder OpenTelemetry |
| 61 | + */ |
| 62 | + GoldenSignalsMetricsTracer(GoldenSignalsMetricsRecorder metricsRecorder) { |
| 63 | + this.clientRequestTimer = Stopwatch.createStarted(); |
| 64 | + this.metricsRecorder = metricsRecorder; |
| 65 | + } |
| 66 | + |
| 67 | + @VisibleForTesting |
| 68 | + GoldenSignalsMetricsTracer(GoldenSignalsMetricsRecorder metricsRecorder, Ticker ticker) { |
| 69 | + this.clientRequestTimer = Stopwatch.createStarted(ticker); |
| 70 | + this.metricsRecorder = metricsRecorder; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * The concept of "operation" and "client request" are the same. They both represent the total |
| 75 | + * time taken for a logical client request, including any retries, backoff, and |
| 76 | + * pre/post-processing |
| 77 | + */ |
| 78 | + @Override |
| 79 | + public void operationSucceeded() { |
| 80 | + attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, StatusCode.Code.OK.toString()); |
| 81 | + metricsRecorder.recordOperationLatency( |
| 82 | + clientRequestTimer.elapsed(TimeUnit.NANOSECONDS) / 1_000_000_000.0, attributes); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void operationCancelled() { |
| 87 | + attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, StatusCode.Code.CANCELLED.toString()); |
| 88 | + metricsRecorder.recordOperationLatency( |
| 89 | + clientRequestTimer.elapsed(TimeUnit.NANOSECONDS) / 1_000_000_000.0, attributes); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void operationFailed(Throwable error) { |
| 94 | + attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, ObservabilityUtils.extractStatus(error)); |
| 95 | + metricsRecorder.recordOperationLatency( |
| 96 | + clientRequestTimer.elapsed(TimeUnit.NANOSECONDS) / 1_000_000_000.0, attributes); |
| 97 | + } |
| 98 | +} |
0 commit comments