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 @@ -270,6 +270,7 @@ public <Response, Model> Model run(
attemptAgain.set(false);
if (metricsContext != null) {
metricsContext.incrementAttempts();
metricsContext.resetHttpResponseReceived();
}
try {
Response result = c.call();
Expand Down
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 @@ -66,6 +66,13 @@ final class StorageErrorTypeMapper {
static final String STATUS_AUTHENTICATION_ERROR = "AUTHENTICATION_ERROR";
static final String STATUS_CHECKSUM_MISMATCH = "CHECKSUM_MISMATCH";

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

static String mapGrpcStatus(@Nullable Status status) {
return mapGrpcStatus(status, 0);
}
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 @@ -105,8 +107,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 @@ -140,12 +149,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 @@ -247,4 +265,34 @@ 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 && StorageErrorTypeMapper.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 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 @@ -24,6 +24,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 @@ -58,6 +59,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 @@ -154,6 +158,10 @@ public void initialize(HttpRequest request) throws IOException {
if (priorStatusCode != NO_PENDING_RETRY) {
recordInternalRetry(req, priorStatusCode);
}
StorageMetricsContext ctx = StorageMetricsContext.current();
if (ctx != null) {
ctx.resetHttpResponseReceived();
}
if (existingExec != null) {
existingExec.intercept(req);
}
Expand All @@ -167,7 +175,6 @@ && isMediaUploadRequest(req)) {
startTimeNanos.set(clock.nanoTime());
boolean started = recorded.compareAndSet(true, false);
if (started && metrics.getRequestActive() != null) {
StorageMetricsContext ctx = StorageMetricsContext.current();
Context otelContext = ctx != null ? ctx.getOtelContext() : Context.current();
String operationName = resolveOperationName(req, extractUrlTemplate(req.getUrl()), ctx);
activeOtelContext.set(otelContext);
Expand All @@ -193,11 +200,7 @@ && isMediaUploadRequest(req)) {
metrics.recordRequestActive(
activeOtelContext.get(), activeOperationName.get(), -1L);
recordResponse(
req,
startTimeNanos.get(),
bodyWriteEndNanos.get(),
endNanos,
response.getStatusCode());
req, response, startTimeNanos.get(), bodyWriteEndNanos.get(), endNanos);
}
}
if (handled && supportsRetry && ownResponse && response.getStatusCode() >= 400) {
Expand All @@ -218,29 +221,28 @@ && isMediaUploadRequest(req)) {
if (response.getRequest() == request && recorded.compareAndSet(false, true)) {
metrics.recordRequestActive(activeOtelContext.get(), activeOperationName.get(), -1L);
recordResponse(
request,
startTimeNanos.get(),
bodyWriteEndNanos.get(),
endNanos,
response.getStatusCode());
request, response, startTimeNanos.get(), bodyWriteEndNanos.get(), endNanos);
}
}
});

// HttpIOExceptionHandler does not receive the thrown IOException instance from
// google-http-client, so transport-level IOExceptions default to STATUS_CONNECTION_ERROR here
// (specific subtypes like TIMEOUT, DNS_FAILURE, and TLS_FAILURE are mapped in Retrying).
// google-http-client and also fires on SocketTimeoutException read timeouts, so
// server.unreached is NOT recorded here; HTTP pre-server IOExceptions (DNS_FAILURE,
// TLS_FAILURE, CONNECTION_ERROR, AUTHENTICATION_ERROR) are recorded with the actual
// exception in StorageMetricsContext#recordAttemptFailure.
request.setIOExceptionHandler(
(req, supportsRetry) -> {
if (recorded.compareAndSet(false, true)) {
metrics.recordRequestActive(activeOtelContext.get(), activeOperationName.get(), -1L);
double durationSeconds =
Math.max(0L, clock.nanoTime() - startTimeNanos.get()) / 1_000_000_000.0;
String urlTemplate = extractUrlTemplate(req.getUrl());
metrics.recordHttpRequestDuration(
durationSeconds,
req.getRequestMethod(),
0L,
extractUrlTemplate(req.getUrl()),
urlTemplate,
StorageErrorTypeMapper.STATUS_CONNECTION_ERROR);
}
boolean handled =
Expand Down Expand Up @@ -273,7 +275,12 @@ private void recordInternalRetry(HttpRequest request, int priorStatusCode) {
}

private void recordResponse(
HttpRequest request, long startNanos, long writeEndNanos, long endNanos, int statusCode) {
HttpRequest request,
HttpResponse response,
long startNanos,
long writeEndNanos,
long endNanos) {
int statusCode = response.getStatusCode();
double durationSeconds = Math.max(0L, endNanos - startNanos) / 1_000_000_000.0;
String urlTemplate = extractUrlTemplate(request.getUrl());
String errorType =
Expand All @@ -289,12 +296,20 @@ private void recordResponse(
// initialize(HttpRequest), because callers such as MediaHttpDownloader or MediaHttpUploader
// may append query parameters (e.g. alt=media, uploadType) after buildRequest().
StorageMetricsContext ctx = StorageMetricsContext.current();
if (ctx != null) {
ctx.markHttpResponseReceived();
}
Context otelContext = ctx != null ? ctx.getOtelContext() : Context.current();
String operationName = resolveOperationName(request, urlTemplate, ctx);
metrics.recordTtfb(
otelContext,
resolveOperationName(request, urlTemplate, ctx),
ttfbSeconds,
StorageClientMetrics.NETWORK_PATH_CLOUDPATH);
otelContext, operationName, ttfbSeconds, 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
}

static String resolveOperationName(HttpRequest request, String urlTemplate) {
Expand Down Expand Up @@ -475,4 +490,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();
}
}
Loading
Loading