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 @@ -86,14 +86,14 @@ final class StorageClientMetrics implements AutoCloseable {
"gcp.storage.client.request.body.size";
static final String METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE =
"gcp.storage.client.response.body.size";
static final String METRIC_GCP_STORAGE_CLIENT_SERVER_UNREACHED =
"gcp.storage.client.server.unreached";

// Debug metric names
static final String METRIC_GCP_STORAGE_CLIENT_REQUEST_ACTIVE =
"gcp.storage.client.request.active";
static final String METRIC_GCP_STORAGE_CLIENT_SERVER_DURATION =
"gcp.storage.client.server.duration";
static final String METRIC_GCP_STORAGE_CLIENT_SERVER_UNREACHED =
"gcp.storage.client.server.unreached";
static final String METRIC_GCP_STORAGE_CLIENT_STALL_DURATION =
"gcp.storage.client.stall.duration";
static final String METRIC_GCP_STORAGE_CLIENT_NETWORK_IO = "gcp.storage.client.network.io";
Expand Down Expand Up @@ -191,10 +191,10 @@ static StorageClientMetrics create(
@Nullable private final DoubleHistogram ttfb;
@Nullable private final LongHistogram requestBodySize;
@Nullable private final LongHistogram responseBodySize;
@Nullable private final LongCounter serverUnreached;

@Nullable private final LongUpDownCounter requestActive;
@Nullable private final DoubleHistogram serverDuration;
@Nullable private final LongCounter serverUnreached;
@Nullable private final DoubleHistogram stallDuration;
@Nullable private final LongCounter networkIo;
@Nullable private final DoubleHistogram credentialRefreshDuration;
Expand Down Expand Up @@ -297,6 +297,13 @@ private StorageClientMetrics(
.setExplicitBucketBoundariesAdvice(
OpenTelemetryBootstrappingUtils.sizeHistogramLongBoundaries())
.build();
this.serverUnreached =
meter
.counterBuilder(METRIC_GCP_STORAGE_CLIENT_SERVER_UNREACHED)
.setDescription(
"Number of GCS client requests that failed before reaching the server")
.setUnit("{request}")
.build();
} else {
this.rpcClientCallDuration = null;
this.httpClientRequestDuration = null;
Expand All @@ -308,6 +315,7 @@ private StorageClientMetrics(
this.ttfb = null;
this.requestBodySize = null;
this.responseBodySize = null;
this.serverUnreached = null;
}

if (enableOtelDebugMetrics) {
Expand All @@ -327,15 +335,6 @@ private StorageClientMetrics(
.setExplicitBucketBoundariesAdvice(
OpenTelemetryBootstrappingUtils.latencyHistogramBoundaries())
.build();
this.serverUnreached =
meter
.counterBuilder(METRIC_GCP_STORAGE_CLIENT_SERVER_UNREACHED)
.setDescription(
"Number of attempts that received no response from a GCS server (gRPC:"
+ " grpc-server-stats-bin trailer absent; HTTP: X-GUploader-UploadID header"
+ " absent).")
.setUnit("{attempt}")
.build();
this.stallDuration =
meter
.histogramBuilder(METRIC_GCP_STORAGE_CLIENT_STALL_DURATION)
Expand Down Expand Up @@ -367,7 +366,6 @@ private StorageClientMetrics(
} else {
this.requestActive = null;
this.serverDuration = null;
this.serverUnreached = null;
this.stallDuration = null;
this.networkIo = null;
this.credentialRefreshDuration = null;
Expand Down Expand Up @@ -429,6 +427,10 @@ Attributes attachClientAttributes(@Nullable Attributes other) {
return responseBodySize;
}

@Nullable LongCounter getServerUnreached() {
return serverUnreached;
}

@Nullable LongUpDownCounter getRequestActive() {
return requestActive;
}
Expand All @@ -437,10 +439,6 @@ Attributes attachClientAttributes(@Nullable Attributes other) {
return serverDuration;
}

@Nullable LongCounter getServerUnreached() {
return serverUnreached;
}

@Nullable DoubleHistogram getStallDuration() {
return stallDuration;
}
Expand Down Expand Up @@ -550,6 +548,20 @@ void recordServerDuration(
serverDuration.record(durationSeconds, attributes, otelContext);
}

void recordServerUnreached(
Context otelContext, String operationName, String networkPath, String errorType) {
if (serverUnreached == null) {
return;
}
Attributes attributes =
baseAttributes.toBuilder()
.put(KEY_GCP_STORAGE_OPERATION, operationName)
.put(KEY_NETWORK_PATH, networkPath)
.put(KEY_ERROR_TYPE, errorType)
.build();
Comment thread
nidhiii-27 marked this conversation as resolved.
Resolved
serverUnreached.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 @@ -52,6 +52,8 @@ final class StorageGrpcMetricsInterceptor implements ClientInterceptor {

static final Metadata.Key<byte[]> GRPC_SERVER_STATS_BIN_KEY =
Metadata.Key.of("grpc-server-stats-bin", Metadata.BINARY_BYTE_MARSHALLER);
static final Metadata.Key<String> UPLOADER_UPLOAD_ID_KEY =
Metadata.Key.of("x-guploader-uploadid", Metadata.ASCII_STRING_MARSHALLER);

private final StorageClientMetrics metrics;
private final ApiClock clock;
Expand Down Expand Up @@ -104,8 +106,15 @@ private void startWithMetrics(
Listener<RespT> responseListener, Metadata headers, long startTimeNanos) {
super.start(
new SimpleForwardingClientCallListener<RespT>(responseListener) {
private boolean receivedServerHeaders;
private boolean headerHasUploadId;

@Override
public void onHeaders(Metadata headers) {
receivedServerHeaders = true;
if (hasNonEmptyMetadata(headers, UPLOADER_UPLOAD_ID_KEY)) {
headerHasUploadId = true;
}
if (metricsCtx != null) {
metricsCtx.setNetworkPath(detectNetworkPath(getAttributes()));
}
Expand Down Expand Up @@ -137,12 +146,21 @@ public void onClose(Status status, Metadata trailers) {

metrics.recordRpcCallDuration(
otelContext, durationSeconds, rpcMethod, statusCode, networkPath, errorType);
if (metrics.getServerDuration() != null) {
double serverDurationSeconds = decodeServerDurationSeconds(trailers);
if (serverDurationSeconds >= 0.0) {
metrics.recordServerDuration(
otelContext, serverDurationSeconds, operationName, networkPath);
}
double serverDurationSeconds = decodeServerDurationSeconds(trailers);
if (serverDurationSeconds >= 0.0) {
metrics.recordServerDuration(
otelContext, serverDurationSeconds, operationName, networkPath);
}
if (isServerUnreached(
networkPath,
status,
errorType,
serverDurationSeconds >= 0.0,
receivedServerHeaders,
headerHasUploadId,
trailers)) {
metrics.recordServerUnreached(
otelContext, operationName, networkPath, errorType);
}
} finally {
releaseActive();
Expand Down Expand Up @@ -232,4 +250,41 @@ static boolean isDirectPathIp(InetAddress addr) {
}
return false;
}

private static boolean isServerUnreached(
String networkPath,
Status status,
String errorType,
boolean hasServerDuration,
boolean receivedServerHeaders,
boolean headerHasUploadId,
Metadata trailers) {
if (StorageClientMetrics.NETWORK_PATH_DIRECTPATH.equals(networkPath)) {
boolean reached =
hasServerDuration || receivedServerHeaders || !trailers.keys().isEmpty() || status.isOk();
return !reached && isPreServerErrorType(errorType);
}
boolean hasUploadId =
headerHasUploadId || hasNonEmptyMetadata(trailers, UPLOADER_UPLOAD_ID_KEY);
boolean reached = hasServerDuration || hasUploadId;
return !reached && !isAmbiguousClientAbort(errorType);
}
Comment thread
nidhiii-27 marked this conversation as resolved.
Resolved

private static boolean isAmbiguousClientAbort(String errorType) {
return StorageErrorTypeMapper.STATUS_CANCELLED.equals(errorType)
|| StorageErrorTypeMapper.STATUS_TIMEOUT.equals(errorType)
|| StorageErrorTypeMapper.STATUS_DEADLINE_EXCEEDED.equals(errorType);
}

private static boolean isPreServerErrorType(String errorType) {
return StorageErrorTypeMapper.STATUS_AUTHENTICATION_ERROR.equals(errorType)
|| StorageErrorTypeMapper.STATUS_TLS_FAILURE.equals(errorType)
|| StorageErrorTypeMapper.STATUS_DNS_FAILURE.equals(errorType)
|| StorageErrorTypeMapper.STATUS_CONNECTION_ERROR.equals(errorType);
}

private static boolean hasNonEmptyMetadata(Metadata metadata, Metadata.Key<String> key) {
String value = metadata.get(key);
return value != null && !value.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.api.client.http.HttpIOExceptionHandler;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpResponseInterceptor;
import com.google.api.client.http.HttpUnsuccessfulResponseHandler;
import com.google.api.core.ApiClock;
Expand Down Expand Up @@ -55,6 +56,9 @@
@NullMarked
final class StorageHttpMetricsInterceptor implements HttpRequestInitializer {

static final String HEADER_UPLOADER_UPLOAD_ID = "X-GUploader-UploadID";
static final String HEADER_GFE_SERVICE_TIME = "X-Goog-Gfe-Service-Time";

private static final ImmutableSet<String> OBJECT_SUB_RESOURCES =
ImmutableSet.of("acl", "compose", "restore");

Expand Down Expand Up @@ -180,7 +184,7 @@ public void initialize(HttpRequest request) throws IOException {
&& recorded.compareAndSet(false, true)) {
metrics.recordRequestActive(
activeOtelContext.get(), activeOperationName.get(), -1L);
recordResponse(req, startTimeNanos.get(), endNanos, response.getStatusCode());
recordResponse(req, response, startTimeNanos.get(), endNanos);
}
}
if (handled
Expand All @@ -203,7 +207,7 @@ public void initialize(HttpRequest request) throws IOException {
} finally {
if (response.getRequest() == request && recorded.compareAndSet(false, true)) {
metrics.recordRequestActive(activeOtelContext.get(), activeOperationName.get(), -1L);
recordResponse(request, startTimeNanos.get(), endNanos, response.getStatusCode());
recordResponse(request, response, startTimeNanos.get(), endNanos);
}
}
});
Expand All @@ -216,11 +220,18 @@ public void initialize(HttpRequest request) throws IOException {
if (recorded.compareAndSet(false, true)) {
metrics.recordRequestActive(activeOtelContext.get(), activeOperationName.get(), -1L);
double durationSeconds = (clock.nanoTime() - startTimeNanos.get()) / 1_000_000_000.0;
String urlTemplate = extractUrlTemplate(req.getUrl());
recordDuration(
req,
durationSeconds,
0L,
extractUrlTemplate(req.getUrl()),
urlTemplate,
StorageErrorTypeMapper.STATUS_CONNECTION_ERROR);
StorageMetricsContext ctx = StorageMetricsContext.current();
metrics.recordServerUnreached(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HttpIOExceptionHandler also fires on SocketTimeoutException read timeouts and hides DNS/TLS subtypes, so can we record server.unreached where the actual IOException is available instead of counting every IOException as CONNECTION_ERROR?

ctx != null ? ctx.getOtelContext() : Context.current(),
resolveOperationName(req, urlTemplate),
StorageClientMetrics.NETWORK_PATH_CLOUDPATH,
StorageErrorTypeMapper.STATUS_CONNECTION_ERROR);
}
boolean handled =
Expand Down Expand Up @@ -252,7 +263,9 @@ private void recordInternalRetry(HttpRequest request, int priorStatusCode) {
errorType);
}

private void recordResponse(HttpRequest request, long startNanos, long endNanos, int statusCode) {
private void recordResponse(
HttpRequest request, HttpResponse response, long startNanos, long endNanos) {
int statusCode = response.getStatusCode();
double durationSeconds = (endNanos - startNanos) / 1_000_000_000.0;
String urlTemplate = extractUrlTemplate(request.getUrl());
String errorType =
Expand All @@ -265,11 +278,16 @@ private void recordResponse(HttpRequest request, long startNanos, long endNanos,
// may append query parameters (e.g. alt=media, uploadType) after buildRequest().
StorageMetricsContext ctx = StorageMetricsContext.current();
Context otelContext = ctx != null ? ctx.getOtelContext() : Context.current();
String operationName = resolveOperationName(request, urlTemplate);
metrics.recordTtfb(
otelContext,
resolveOperationName(request, urlTemplate),
durationSeconds,
StorageClientMetrics.NETWORK_PATH_CLOUDPATH);
otelContext, operationName, durationSeconds, StorageClientMetrics.NETWORK_PATH_CLOUDPATH);
boolean reached =
hasNonEmptyHeader(response, HEADER_UPLOADER_UPLOAD_ID)
|| hasNonEmptyHeader(response, HEADER_GFE_SERVICE_TIME);
if (!reached) {
metrics.recordServerUnreached(
otelContext, operationName, StorageClientMetrics.NETWORK_PATH_CLOUDPATH, errorType);
}
Comment thread
nidhiii-27 marked this conversation as resolved.
Resolved
}

private void recordDuration(
Expand Down Expand Up @@ -423,4 +441,9 @@ private static String extractObjectUrlTemplate(String[] segments) {
}
return "/storage/v1/b/{bucket}/o/{object}";
}

private static boolean hasNonEmptyHeader(HttpResponse response, String headerName) {
String value = response.getHeaders().getFirstHeaderStringValue(headerName);
return value != null && !value.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public final class StorageClientMetricsTest {
StorageClientMetrics::getErrors,
StorageClientMetrics::getTtfb,
StorageClientMetrics::getRequestBodySize,
StorageClientMetrics::getResponseBodySize);
StorageClientMetrics::getResponseBodySize,
StorageClientMetrics::getServerUnreached);

private static final List<Function<StorageClientMetrics, Object>> DEBUG_INSTRUMENTS =
ImmutableList.of(
StorageClientMetrics::getRequestActive,
StorageClientMetrics::getServerDuration,
StorageClientMetrics::getServerUnreached,
StorageClientMetrics::getStallDuration,
StorageClientMetrics::getNetworkIo,
StorageClientMetrics::getCredentialRefreshDuration);
Expand Down Expand Up @@ -226,7 +226,7 @@ public void inMemoryMetricReader_verifiesViewsAndBoundaries() {
String serverUnreachedName =
StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_SERVER_UNREACHED.replace(".", "/");
assertThat(metricsMap).containsKey(serverUnreachedName);
assertThat(metricsMap.get(serverUnreachedName).getUnit()).isEqualTo("{attempt}");
assertThat(metricsMap.get(serverUnreachedName).getUnit()).isEqualTo("{request}");

assertThat(metricsMap)
.containsKey(StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_NETWORK_IO.replace(".", "/"));
Expand Down
Loading
Loading