|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 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 com.google.cloud.spanner; |
| 18 | + |
| 19 | +import com.google.api.gax.rpc.ApiException; |
| 20 | +import com.google.api.gax.rpc.StatusCode; |
| 21 | +import com.google.api.gax.tracing.ApiTracer; |
| 22 | +import com.google.api.gax.tracing.MethodName; |
| 23 | +import com.google.api.gax.tracing.MetricsTracer; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.concurrent.CancellationException; |
| 27 | +import javax.annotation.Nullable; |
| 28 | + |
| 29 | +/** |
| 30 | + * Implements built-in metrics tracer. |
| 31 | + * |
| 32 | + * <p>This class extends the {@link MetricsTracer} which computes generic metrics that can be |
| 33 | + * observed in the lifecycle of an RPC operation. |
| 34 | + */ |
| 35 | +class BuiltInMetricsTracer extends MetricsTracer implements ApiTracer { |
| 36 | + |
| 37 | + private final BuiltInMetricsRecorder builtInOpenTelemetryMetricsRecorder; |
| 38 | + // These are RPC specific attributes and pertain to a specific API Trace |
| 39 | + private final Map<String, String> attributes = new HashMap<>(); |
| 40 | + |
| 41 | + private Long gfeLatency = null; |
| 42 | + |
| 43 | + BuiltInMetricsTracer( |
| 44 | + MethodName methodName, BuiltInMetricsRecorder builtInOpenTelemetryMetricsRecorder) { |
| 45 | + super(methodName, builtInOpenTelemetryMetricsRecorder); |
| 46 | + this.builtInOpenTelemetryMetricsRecorder = builtInOpenTelemetryMetricsRecorder; |
| 47 | + this.attributes.put(METHOD_ATTRIBUTE, methodName.toString()); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Adds an annotation that the attempt succeeded. Successful attempt add "OK" value to the status |
| 52 | + * attribute key. |
| 53 | + */ |
| 54 | + @Override |
| 55 | + public void attemptSucceeded() { |
| 56 | + super.attemptSucceeded(); |
| 57 | + if (gfeLatency != null) { |
| 58 | + attributes.put(STATUS_ATTRIBUTE, StatusCode.Code.OK.toString()); |
| 59 | + builtInOpenTelemetryMetricsRecorder.recordGFELatency(gfeLatency, attributes); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Add an annotation that the attempt was cancelled by the user. Cancelled attempt add "CANCELLED" |
| 65 | + * to the status attribute key. |
| 66 | + */ |
| 67 | + @Override |
| 68 | + public void attemptCancelled() { |
| 69 | + super.attemptCancelled(); |
| 70 | + if (gfeLatency != null) { |
| 71 | + attributes.put(STATUS_ATTRIBUTE, StatusCode.Code.CANCELLED.toString()); |
| 72 | + builtInOpenTelemetryMetricsRecorder.recordGFELatency(gfeLatency, attributes); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Adds an annotation that the attempt failed, but another attempt will be made after the delay. |
| 78 | + * |
| 79 | + * @param error the error that caused the attempt to fail. |
| 80 | + * @param delay the amount of time to wait before the next attempt will start. |
| 81 | + * <p>Failed attempt extracts the error from the throwable and adds it to the status attribute |
| 82 | + * key. |
| 83 | + */ |
| 84 | + @Override |
| 85 | + public void attemptFailedDuration(Throwable error, java.time.Duration delay) { |
| 86 | + super.attemptFailedDuration(error, delay); |
| 87 | + if (gfeLatency != null) { |
| 88 | + attributes.put(STATUS_ATTRIBUTE, extractStatus(error)); |
| 89 | + builtInOpenTelemetryMetricsRecorder.recordGFELatency(gfeLatency, attributes); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Adds an annotation that the attempt failed and that no further attempts will be made because |
| 95 | + * retry limits have been reached. This extracts the error from the throwable and adds it to the |
| 96 | + * status attribute key. |
| 97 | + * |
| 98 | + * @param error the last error received before retries were exhausted. |
| 99 | + */ |
| 100 | + @Override |
| 101 | + public void attemptFailedRetriesExhausted(Throwable error) { |
| 102 | + super.attemptFailedRetriesExhausted(error); |
| 103 | + if (gfeLatency != null) { |
| 104 | + attributes.put(STATUS_ATTRIBUTE, extractStatus(error)); |
| 105 | + builtInOpenTelemetryMetricsRecorder.recordGFELatency(gfeLatency, attributes); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Adds an annotation that the attempt failed and that no further attempts will be made because |
| 111 | + * the last error was not retryable. This extracts the error from the throwable and adds it to the |
| 112 | + * status attribute key. |
| 113 | + * |
| 114 | + * @param error the error that caused the final attempt to fail. |
| 115 | + */ |
| 116 | + @Override |
| 117 | + public void attemptPermanentFailure(Throwable error) { |
| 118 | + super.attemptPermanentFailure(error); |
| 119 | + if (gfeLatency != null) { |
| 120 | + attributes.put(STATUS_ATTRIBUTE, extractStatus(error)); |
| 121 | + builtInOpenTelemetryMetricsRecorder.recordGFELatency(gfeLatency, attributes); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + void recordGFELatency(Long gfeLatency) { |
| 126 | + this.gfeLatency = gfeLatency; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void addAttributes(Map<String, String> attributes) { |
| 131 | + super.addAttributes(attributes); |
| 132 | + this.attributes.putAll(attributes); |
| 133 | + }; |
| 134 | + |
| 135 | + @Override |
| 136 | + public void addAttributes(String key, String value) { |
| 137 | + super.addAttributes(key, value); |
| 138 | + this.attributes.put(key, value); |
| 139 | + } |
| 140 | + |
| 141 | + private static String extractStatus(@Nullable Throwable error) { |
| 142 | + final String statusString; |
| 143 | + |
| 144 | + if (error == null) { |
| 145 | + return StatusCode.Code.OK.toString(); |
| 146 | + } else if (error instanceof CancellationException) { |
| 147 | + statusString = StatusCode.Code.CANCELLED.toString(); |
| 148 | + } else if (error instanceof ApiException) { |
| 149 | + statusString = ((ApiException) error).getStatusCode().getCode().toString(); |
| 150 | + } else { |
| 151 | + statusString = StatusCode.Code.UNKNOWN.toString(); |
| 152 | + } |
| 153 | + |
| 154 | + return statusString; |
| 155 | + } |
| 156 | +} |
0 commit comments