|
| 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 | + |
| 31 | +package com.google.api.gax.tracing; |
| 32 | + |
| 33 | +import com.google.api.core.BetaApi; |
| 34 | +import com.google.api.core.InternalApi; |
| 35 | +import com.google.api.gax.logging.LoggerProvider; |
| 36 | +import com.google.api.gax.logging.LoggingUtils; |
| 37 | +import com.google.api.gax.rpc.ApiException; |
| 38 | +import com.google.rpc.ErrorInfo; |
| 39 | +import java.util.HashMap; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +/** |
| 43 | + * An {@link ApiTracer} that logs actionable errors using {@link LoggingUtils} when an RPC attempt |
| 44 | + * fails. |
| 45 | + */ |
| 46 | +@BetaApi |
| 47 | +@InternalApi |
| 48 | +public class LoggingTracer extends BaseApiTracer { |
| 49 | + private static final LoggerProvider LOGGER_PROVIDER = |
| 50 | + LoggerProvider.forClazz(LoggingTracer.class); |
| 51 | + |
| 52 | + private final ApiTracerContext apiTracerContext; |
| 53 | + |
| 54 | + public LoggingTracer(ApiTracerContext apiTracerContext) { |
| 55 | + this.apiTracerContext = apiTracerContext; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void attemptFailed(Throwable error, org.threeten.bp.Duration delay) { |
| 60 | + recordActionableError(error); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void attemptFailedDuration(Throwable error, java.time.Duration delay) { |
| 65 | + recordActionableError(error); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void attemptFailedRetriesExhausted(Throwable error) { |
| 70 | + recordActionableError(error); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void attemptPermanentFailure(Throwable error) { |
| 75 | + recordActionableError(error); |
| 76 | + } |
| 77 | + |
| 78 | + private void recordActionableError(Throwable error) { |
| 79 | + Map<String, Object> logContext = new HashMap<>(); |
| 80 | + |
| 81 | + if (apiTracerContext.rpcSystemName() != null) { |
| 82 | + logContext.put( |
| 83 | + ObservabilityAttributes.RPC_SYSTEM_NAME_ATTRIBUTE, apiTracerContext.rpcSystemName()); |
| 84 | + } |
| 85 | + if (apiTracerContext.fullMethodName() != null) { |
| 86 | + logContext.put( |
| 87 | + ObservabilityAttributes.GRPC_RPC_METHOD_ATTRIBUTE, apiTracerContext.fullMethodName()); |
| 88 | + } |
| 89 | + if (apiTracerContext.serverPort() != null) { |
| 90 | + logContext.put(ObservabilityAttributes.SERVER_PORT_ATTRIBUTE, apiTracerContext.serverPort()); |
| 91 | + } |
| 92 | + if (apiTracerContext.libraryMetadata() != null |
| 93 | + && !apiTracerContext.libraryMetadata().isEmpty()) { |
| 94 | + if (apiTracerContext.libraryMetadata().repository() != null) { |
| 95 | + logContext.put( |
| 96 | + ObservabilityAttributes.REPO_ATTRIBUTE, |
| 97 | + apiTracerContext.libraryMetadata().repository()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (error != null) { |
| 102 | + logContext.put( |
| 103 | + ObservabilityAttributes.RPC_RESPONSE_STATUS_ATTRIBUTE, |
| 104 | + ObservabilityUtils.extractStatus(error)); |
| 105 | + } |
| 106 | + |
| 107 | + if (error instanceof ApiException) { |
| 108 | + ApiException apiException = (ApiException) error; |
| 109 | + if (apiException.getErrorDetails() != null) { |
| 110 | + ErrorInfo errorInfo = apiException.getErrorDetails().getErrorInfo(); |
| 111 | + if (errorInfo != null) { |
| 112 | + logContext.put("error.type", errorInfo.getReason()); |
| 113 | + logContext.put("gcp.errors.domain", errorInfo.getDomain()); |
| 114 | + for (Map.Entry<String, String> entry : errorInfo.getMetadataMap().entrySet()) { |
| 115 | + logContext.put("gcp.errors.metadata." + entry.getKey(), entry.getValue()); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + String message = "Unknown Error"; |
| 122 | + if (error != null) { |
| 123 | + message = error.getMessage() != null ? error.getMessage() : error.getClass().getName(); |
| 124 | + } |
| 125 | + LoggingUtils.logActionableError(logContext, LOGGER_PROVIDER, message); |
| 126 | + } |
| 127 | +} |
0 commit comments