diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/OpenTelemetryBootstrappingUtils.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/OpenTelemetryBootstrappingUtils.java index 1a7ab2e5879e..2a8e4b5c60de 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/OpenTelemetryBootstrappingUtils.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/OpenTelemetryBootstrappingUtils.java @@ -349,6 +349,7 @@ static SdkMeterProvider createDefaultClientMeterProvider( ImmutableList.of( StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_OPERATIONS, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ATTEMPTS, + StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RETRIES, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ERRORS, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_REQUEST_ACTIVE, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_SERVER_UNREACHED, diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/Retrying.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/Retrying.java index 00d01d7511b3..38c81a071abb 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/Retrying.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/Retrying.java @@ -22,7 +22,6 @@ import com.google.api.gax.retrying.BasicResultRetryAlgorithm; import com.google.api.gax.retrying.ResultRetryAlgorithm; import com.google.api.gax.retrying.RetrySettings; -import com.google.cloud.BaseServiceException; import com.google.cloud.storage.Backoff.Jitterer; import com.google.cloud.storage.Conversions.Decoder; import com.google.cloud.storage.spi.v1.HttpRpcContext; @@ -264,22 +263,11 @@ public Model run( metricsContext.recordAttemptSuccess(metrics); } return decoded; - } catch (StorageException se) { - if (metricsContext != null) { - metricsContext.recordAttemptFailure(metrics, se); - } - ctx.recordError(se, () -> attemptAgain.set(true), failure::set); - } catch (IllegalArgumentException iae) { - if (metricsContext != null) { - metricsContext.recordAttemptFailure(metrics, iae); - } - ctx.recordError(iae, () -> attemptAgain.set(true), failure::set); + } catch (StorageException | IllegalArgumentException e) { + handleAttemptError(ctx, metricsContext, e, e, attemptAgain, failure); } catch (Exception e) { - if (metricsContext != null) { - metricsContext.recordAttemptFailure(metrics, e); - } - BaseServiceException coalesced = StorageException.coalesce(e); - ctx.recordError(coalesced, () -> attemptAgain.set(true), failure::set); + handleAttemptError( + ctx, metricsContext, e, StorageException.coalesce(e), attemptAgain, failure); } } while (attemptAgain.get()); @@ -302,6 +290,22 @@ public Model run( } } } + + private void handleAttemptError( + RetryContext ctx, + @Nullable StorageMetricsContext metricsContext, + Throwable rawError, + Exception retryError, + AtomicBoolean attemptAgain, + AtomicReference failure) { + if (metricsContext != null) { + metricsContext.recordAttemptFailure(metrics, rawError); + } + ctx.recordError(retryError, () -> attemptAgain.set(true), failure::set); + if (attemptAgain.get() && metricsContext != null) { + metricsContext.recordRetry(metrics, rawError); + } + } } static final class HttpRetrier implements Retrier { diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClientMetrics.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClientMetrics.java index dff0b43ea78b..915aeef51ea0 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClientMetrics.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageClientMetrics.java @@ -79,6 +79,7 @@ final class StorageClientMetrics implements AutoCloseable { static final String METRIC_GCP_CLIENT_REQUEST_DURATION = "gcp.client.request.duration"; static final String METRIC_GCP_STORAGE_CLIENT_OPERATIONS = "gcp.storage.client.operations"; static final String METRIC_GCP_STORAGE_CLIENT_ATTEMPTS = "gcp.storage.client.attempts"; + static final String METRIC_GCP_STORAGE_CLIENT_RETRIES = "gcp.storage.client.retries"; static final String METRIC_GCP_STORAGE_CLIENT_ERRORS = "gcp.storage.client.errors"; static final String METRIC_GCP_STORAGE_CLIENT_TTFB = "gcp.storage.client.ttfb"; static final String METRIC_GCP_STORAGE_CLIENT_REQUEST_BODY_SIZE = @@ -185,6 +186,7 @@ static StorageClientMetrics create( @Nullable private final DoubleHistogram gcpClientRequestDuration; @Nullable private final LongCounter operations; @Nullable private final LongCounter attempts; + @Nullable private final LongCounter retries; @Nullable private final LongCounter errors; @Nullable private final DoubleHistogram ttfb; @Nullable private final LongHistogram requestBodySize; @@ -251,6 +253,12 @@ private StorageClientMetrics( + " including retries, resumable upload chunks and list pages.") .setUnit("{attempt}") .build(); + this.retries = + meter + .counterBuilder(METRIC_GCP_STORAGE_CLIENT_RETRIES) + .setDescription("Number of GCS client retry attempts") + .setUnit("{attempt}") + .build(); this.errors = meter .counterBuilder(METRIC_GCP_STORAGE_CLIENT_ERRORS) @@ -295,6 +303,7 @@ private StorageClientMetrics( this.gcpClientRequestDuration = null; this.operations = null; this.attempts = null; + this.retries = null; this.errors = null; this.ttfb = null; this.requestBodySize = null; @@ -400,6 +409,10 @@ Attributes attachClientAttributes(@Nullable Attributes other) { return attempts; } + @Nullable LongCounter getRetries() { + return retries; + } + @Nullable LongCounter getErrors() { return errors; } @@ -501,6 +514,20 @@ void recordTtfb( ttfb.record(ttfbSeconds, attributes, otelContext); } + void recordRetry( + Context otelContext, String operationName, String networkPath, String errorType) { + if (retries == null) { + return; + } + Attributes attributes = + baseAttributes.toBuilder() + .put(KEY_GCP_STORAGE_OPERATION, operationName) + .put(KEY_NETWORK_PATH, networkPath) + .put(KEY_ERROR_TYPE, errorType) + .build(); + retries.add(1L, attributes, otelContext); + } + @Nullable static StorageMetricsContext createContext( @Nullable StorageClientMetrics metrics, String operationName) { diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java index 142f75c5d846..50902753d1e6 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java @@ -317,6 +317,17 @@ void recordAttemptFailure(@Nullable StorageClientMetrics metrics, Throwable e) { } } + void recordRetry(@Nullable StorageClientMetrics metrics, Throwable priorError) { + if (metrics == null) { + return; + } + metrics.recordRetry( + otelContext, + operationName, + resolveNetworkPath(), + StorageErrorTypeMapper.mapThrowable(priorError)); + } + void setNetworkPath(String networkPath) { this.networkPath = networkPath; } diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/RetryingMetricsTest.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/RetryingMetricsTest.java index 06fce76f2973..fe1583faf069 100644 --- a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/RetryingMetricsTest.java +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/RetryingMetricsTest.java @@ -25,6 +25,7 @@ import static com.google.cloud.storage.StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ATTEMPTS; import static com.google.cloud.storage.StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ERRORS; import static com.google.cloud.storage.StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_OPERATIONS; +import static com.google.cloud.storage.StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RETRIES; import static com.google.cloud.storage.StorageErrorTypeMapper.STATUS_DEADLINE_EXCEEDED; import static com.google.cloud.storage.StorageErrorTypeMapper.STATUS_DNS_FAILURE; import static com.google.cloud.storage.StorageErrorTypeMapper.STATUS_NOT_FOUND; @@ -38,6 +39,7 @@ import com.google.api.core.NanoClock; import com.google.api.gax.grpc.GrpcCallContext; import com.google.api.gax.retrying.RetrySettings; +import com.google.cloud.NoCredentials; import com.google.cloud.storage.Conversions.Decoder; import com.google.cloud.storage.Retrying.DefaultRetrier; import com.google.cloud.storage.Retrying.Retrier; @@ -103,6 +105,7 @@ public void successfulCallOnFirstAttempt_recordsExpectedMetrics() { () -> "success", Decoder.identity(), StorageMetricsContext.create("GetObject")); assertThat(result).isEqualTo("success"); assertOutcomeMetrics(STATUS_OK, 1L, 0L); + assertThat(getCounterValue(collect(), METRIC_GCP_STORAGE_CLIENT_RETRIES)).isEqualTo(0L); assertThat(assertDuration(STATUS_OK).getAttributes().get(KEY_GCP_CLIENT_SERVICE)) .isEqualTo("storage"); } @@ -126,6 +129,7 @@ public void retriedCallSuccessOnThirdAttempt_recordsAttemptsAndErrors() { assertThat(attemptCounter.get()).isEqualTo(3); assertOutcomeMetrics(STATUS_OK, 3L, 2L); assertAttr(METRIC_GCP_STORAGE_CLIENT_ERRORS, KEY_ERROR_TYPE, STATUS_UNAVAILABLE, 2L); + assertAttr(METRIC_GCP_STORAGE_CLIENT_RETRIES, KEY_ERROR_TYPE, STATUS_UNAVAILABLE, 2L); assertDuration(STATUS_OK); } @@ -169,6 +173,7 @@ public void terminalFailureAllAttemptsFail_recordsExhaustedRetries() { assertThat(se.getCode()).isEqualTo(503); assertOutcomeMetrics(STATUS_UNAVAILABLE, 3L, 3L); assertAttr(METRIC_GCP_STORAGE_CLIENT_ERRORS, KEY_ERROR_TYPE, STATUS_UNAVAILABLE, 3L); + assertAttr(METRIC_GCP_STORAGE_CLIENT_RETRIES, KEY_ERROR_TYPE, STATUS_UNAVAILABLE, 2L); assertDuration(STATUS_UNAVAILABLE); } @@ -249,6 +254,7 @@ public void nonRetryableError_singleAttemptTerminalFailure_recordsErrorAndTermin assertThat(se.getCode()).isEqualTo(404); assertOutcomeMetrics(STATUS_NOT_FOUND, 1L, 1L); assertAttr(METRIC_GCP_STORAGE_CLIENT_ERRORS, KEY_ERROR_TYPE, STATUS_NOT_FOUND, 1L); + assertThat(getCounterValue(collect(), METRIC_GCP_STORAGE_CLIENT_RETRIES)).isEqualTo(0L); assertDuration(STATUS_NOT_FOUND); } @@ -279,6 +285,9 @@ public void recordAttemptError_specificExceptions_mappedToCanonicalTypes() { assertAttr(METRIC_GCP_STORAGE_CLIENT_ERRORS, KEY_ERROR_TYPE, STATUS_DEADLINE_EXCEEDED, 1L); assertAttr(METRIC_GCP_STORAGE_CLIENT_ERRORS, KEY_ERROR_TYPE, STATUS_TIMEOUT, 1L); assertAttr(METRIC_GCP_STORAGE_CLIENT_ERRORS, KEY_ERROR_TYPE, STATUS_DNS_FAILURE, 1L); + assertAttr(METRIC_GCP_STORAGE_CLIENT_RETRIES, KEY_ERROR_TYPE, STATUS_DEADLINE_EXCEEDED, 1L); + assertAttr(METRIC_GCP_STORAGE_CLIENT_RETRIES, KEY_ERROR_TYPE, STATUS_TIMEOUT, 1L); + assertAttr(METRIC_GCP_STORAGE_CLIENT_RETRIES, KEY_ERROR_TYPE, STATUS_DNS_FAILURE, 0L); } @Test @@ -528,6 +537,59 @@ public void withMetricsContext_withAlg_runTwoArgs_recordsAttemptsAndErrors() { assertDuration(STATUS_OK); } + @Test + public void retriedCall_viaStorageOptions_recordsExactRetryAttributes() { + GrpcStorageOptions options = + StorageOptions.grpc() + .setProjectId("test-project") + .setCredentials(NoCredentials.getInstance()) + .setRetrySettings(RETRY_SETTINGS) + .setEnableOtelMetrics(true) + .setMeterProvider(provider) + .build(); + StorageMetricsContext ctx = options.getStorageClientMetrics().createContext("GetObject"); + ctx.setNetworkPath(StorageClientMetrics.NETWORK_PATH_DIRECTPATH); + Retrier bound = options.createRetrier().withMetricsContext(ctx); + + AtomicInteger attempts = new AtomicInteger(0); + String result = + bound.run( + Retrying.alwaysRetry(), + () -> { + if (attempts.incrementAndGet() == 1) { + throw new StorageException(503, "Unavailable"); + } + return "ok"; + }, + Decoder.identity()); + assertThat(result).isEqualTo("ok"); + + MetricData retriesMetric = findMetric(collect(), METRIC_GCP_STORAGE_CLIENT_RETRIES); + assertThat(retriesMetric).isNotNull(); + assertThat(retriesMetric.getUnit()).isEqualTo("{attempt}"); + assertThat(retriesMetric.getLongSumData().getPoints()).hasSize(1); + LongPointData point = retriesMetric.getLongSumData().getPoints().iterator().next(); + assertThat(point.getValue()).isEqualTo(1L); + assertThat(point.getAttributes()) + .isEqualTo( + Attributes.builder() + .put( + StorageClientMetrics.KEY_GCP_CLIENT_SERVICE, + StorageClientMetrics.SERVICE_STORAGE) + .put(StorageClientMetrics.KEY_GCP_CLIENT_VERSION, StorageOptions.version()) + .put( + StorageClientMetrics.KEY_GCP_CLIENT_ARTIFACT, + StorageClientMetrics.ARTIFACT_STORAGE) + .put(StorageClientMetrics.KEY_RPC_SYSTEM_NAME, "grpc") + .put(StorageClientMetrics.KEY_SERVER_ADDRESS, "storage.googleapis.com") + .put(StorageClientMetrics.KEY_GCP_STORAGE_OPERATION, "GetObject") + .put( + StorageClientMetrics.KEY_NETWORK_PATH, + StorageClientMetrics.NETWORK_PATH_DIRECTPATH) + .put(StorageClientMetrics.KEY_ERROR_TYPE, STATUS_UNAVAILABLE) + .build()); + } + private void assertCreateRetrierWiresMetricsWhenEnabled(StorageOptions options) { assertThat(options.getStorageClientMetrics()).isNotNull(); Retrier retrier = options.createRetrier(); diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageClientMetricsTest.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageClientMetricsTest.java index 505aab2280d2..39adecefeaf6 100644 --- a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageClientMetricsTest.java +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageClientMetricsTest.java @@ -50,6 +50,7 @@ public final class StorageClientMetricsTest { StorageClientMetrics::getGcpClientRequestDuration, StorageClientMetrics::getOperations, StorageClientMetrics::getAttempts, + StorageClientMetrics::getRetries, StorageClientMetrics::getErrors, StorageClientMetrics::getTtfb, StorageClientMetrics::getRequestBodySize, @@ -144,6 +145,7 @@ public void inMemoryMetricReader_verifiesViewsAndBoundaries() { metrics.getOperations().add(1); metrics.getAttempts().add(2); + metrics.getRetries().add(1); metrics.getErrors().add(1); metrics.getRequestActive().add(1); metrics.getServerUnreached().add(1); @@ -207,6 +209,10 @@ public void inMemoryMetricReader_verifiesViewsAndBoundaries() { assertThat(metricsMap).containsKey(attemptsName); assertThat(metricsMap.get(attemptsName).getUnit()).isEqualTo("{attempt}"); + String retriesName = StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RETRIES.replace(".", "/"); + assertThat(metricsMap).containsKey(retriesName); + assertThat(metricsMap.get(retriesName).getUnit()).isEqualTo("{attempt}"); + String errorsName = StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_ERRORS.replace(".", "/"); assertThat(metricsMap).containsKey(errorsName); assertThat(metricsMap.get(errorsName).getUnit()).isEqualTo("{error}");