Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -264,22 +263,11 @@ public <Response, Model> 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());

Expand All @@ -302,6 +290,22 @@ public <Response, Model> Model run(
}
}
}

private void handleAttemptError(
RetryContext ctx,
@Nullable StorageMetricsContext metricsContext,
Throwable rawError,
Exception retryError,
AtomicBoolean attemptAgain,
AtomicReference<Exception> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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}")
Comment thread
nidhiii-27 marked this conversation as resolved.
Resolved
.build();
this.errors =
meter
.counterBuilder(METRIC_GCP_STORAGE_CLIENT_ERRORS)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -400,6 +409,10 @@ Attributes attachClientAttributes(@Nullable Attributes other) {
return attempts;
}

@Nullable LongCounter getRetries() {
return retries;
}

@Nullable LongCounter getErrors() {
return errors;
}
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public final class StorageClientMetricsTest {
StorageClientMetrics::getGcpClientRequestDuration,
StorageClientMetrics::getOperations,
StorageClientMetrics::getAttempts,
StorageClientMetrics::getRetries,
StorageClientMetrics::getErrors,
StorageClientMetrics::getTtfb,
StorageClientMetrics::getRequestBodySize,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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}");
Expand Down
Loading