Repository navigation
feat(storage): record gcp.storage.client.ttfb for HTTP unary and upload requests #15066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nidhiii-27
wants to merge
1
commit into
feat/storage-response-body-size
Choose a base branch
from
feat/storage-http-ttfb-unary-upload
base: feat/storage-response-body-size
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+652
−89
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,9 +26,11 @@ | |
| import com.google.api.client.http.HttpResponseInterceptor; | ||
| import com.google.api.client.http.HttpUnsuccessfulResponseHandler; | ||
| import com.google.api.core.ApiClock; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.collect.ImmutableSet; | ||
| import io.opentelemetry.context.Context; | ||
| import java.io.IOException; | ||
| import java.util.Locale; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import org.jspecify.annotations.NullMarked; | ||
|
|
@@ -44,16 +46,68 @@ | |
| * for recording when the response body is closed). End-to-end download duration including body | ||
| * transfer is captured by {@code gcp.client.request.duration}. | ||
| * | ||
| * <p>{@code gcp.storage.client.ttfb} is recorded here for media download requests across all HTTP | ||
| * response status codes when response headers arrive (HTTP unary and upload TTFB are tracked for a | ||
| * follow-up). | ||
| * <p>{@code gcp.storage.client.ttfb} is recorded here across all HTTP operations and response | ||
| * status codes when response headers arrive, resolving {@code gcp.storage.operation} from {@link | ||
| * StorageMetricsContext#current()} when active or falling back to route-based resolution. | ||
| */ | ||
| @NullMarked | ||
| final class StorageHttpMetricsInterceptor implements HttpRequestInitializer { | ||
|
|
||
| private static final ImmutableSet<String> OBJECT_SUB_RESOURCES = | ||
| ImmutableSet.of("acl", "compose", "restore"); | ||
|
|
||
| private static final ImmutableMap<String, String> ROUTE_OPERATIONS = | ||
| ImmutableMap.<String, String>builder() | ||
| .put("POST /batch/storage/v1", "Batch") | ||
| .put("POST /storage/v1/b", "CreateBucket") | ||
| .put("GET /storage/v1/b", "ListBuckets") | ||
| .put("GET /storage/v1/b/{bucket}", "GetBucket") | ||
| .put("PATCH /storage/v1/b/{bucket}", "UpdateBucket") | ||
| .put("DELETE /storage/v1/b/{bucket}", "DeleteBucket") | ||
| .put("POST /storage/v1/b/{bucket}/lockRetentionPolicy", "LockBucketRetentionPolicy") | ||
| .put("GET /storage/v1/b/{bucket}/iam", "GetIamPolicy") | ||
| .put("PUT /storage/v1/b/{bucket}/iam", "SetIamPolicy") | ||
| .put("GET /storage/v1/b/{bucket}/iam/testPermissions", "TestIamPermissions") | ||
| .put("POST /storage/v1/b/{bucket}/acl", "CreateBucketACL") | ||
| .put("GET /storage/v1/b/{bucket}/acl", "ListBucketACLs") | ||
| .put("GET /storage/v1/b/{bucket}/acl/{entity}", "GetBucketACL") | ||
| .put("PATCH /storage/v1/b/{bucket}/acl/{entity}", "UpdateBucketACL") | ||
| .put("DELETE /storage/v1/b/{bucket}/acl/{entity}", "DeleteBucketACL") | ||
| .put("POST /storage/v1/b/{bucket}/defaultObjectAcl", "CreateDefaultObjectACL") | ||
| .put("GET /storage/v1/b/{bucket}/defaultObjectAcl", "ListDefaultObjectACLs") | ||
| .put("GET /storage/v1/b/{bucket}/defaultObjectAcl/{entity}", "GetDefaultObjectACL") | ||
| .put("PATCH /storage/v1/b/{bucket}/defaultObjectAcl/{entity}", "UpdateDefaultObjectACL") | ||
| .put("DELETE /storage/v1/b/{bucket}/defaultObjectAcl/{entity}", "DeleteDefaultObjectACL") | ||
| .put("POST /storage/v1/b/{bucket}/notificationConfigs", "CreateNotification") | ||
| .put("GET /storage/v1/b/{bucket}/notificationConfigs", "ListNotifications") | ||
| .put("GET /storage/v1/b/{bucket}/notificationConfigs/{notification}", "GetNotification") | ||
| .put( | ||
| "DELETE /storage/v1/b/{bucket}/notificationConfigs/{notification}", | ||
| "DeleteNotification") | ||
| .put("POST /storage/v1/b/{bucket}/o", "WriteObject") | ||
| .put("GET /storage/v1/b/{bucket}/o", "ListObjects") | ||
| .put("GET /storage/v1/b/{bucket}/o/{object}", "GetObject") | ||
| .put("PATCH /storage/v1/b/{bucket}/o/{object}", "UpdateObject") | ||
| .put("DELETE /storage/v1/b/{bucket}/o/{object}", "DeleteObject") | ||
| .put("POST /storage/v1/b/{bucket}/o/{object}/compose", "ComposeObject") | ||
| .put("POST /storage/v1/b/{bucket}/o/{object}/restore", "RestoreObject") | ||
| .put( | ||
| "POST /storage/v1/b/{bucket}/o/{object}/rewriteTo/b/{bucket}/o/{object}", | ||
| "RewriteObject") | ||
| .put("POST /storage/v1/b/{bucket}/o/{object}/moveTo/o/{object}", "MoveObject") | ||
| .put("POST /storage/v1/b/{bucket}/o/{object}/acl", "CreateObjectACL") | ||
| .put("GET /storage/v1/b/{bucket}/o/{object}/acl", "ListObjectACLs") | ||
| .put("GET /storage/v1/b/{bucket}/o/{object}/acl/{entity}", "GetObjectACL") | ||
| .put("PATCH /storage/v1/b/{bucket}/o/{object}/acl/{entity}", "UpdateObjectACL") | ||
| .put("DELETE /storage/v1/b/{bucket}/o/{object}/acl/{entity}", "DeleteObjectACL") | ||
| .put("POST /storage/v1/projects/{project}/hmacKeys", "CreateHMACKey") | ||
| .put("GET /storage/v1/projects/{project}/hmacKeys", "ListHMACKeys") | ||
| .put("GET /storage/v1/projects/{project}/hmacKeys/{access_id}", "GetHMACKey") | ||
| .put("PUT /storage/v1/projects/{project}/hmacKeys/{access_id}", "UpdateHMACKey") | ||
| .put("DELETE /storage/v1/projects/{project}/hmacKeys/{access_id}", "DeleteHMACKey") | ||
| .put("GET /storage/v1/projects/{project}/serviceAccount", "GetServiceAccount") | ||
| .build(); | ||
|
|
||
| @Nullable private final HttpRequestInitializer delegate; | ||
| private final StorageClientMetrics metrics; | ||
| private final ApiClock clock; | ||
|
|
@@ -119,11 +173,12 @@ public void initialize(HttpRequest request) throws IOException { | |
| request.setIOExceptionHandler( | ||
| (req, supportsRetry) -> { | ||
| if (recorded.compareAndSet(false, true)) { | ||
| double durationSeconds = (clock.nanoTime() - startTimeNanos.get()) / 1_000_000_000.0; | ||
| recordDuration( | ||
| req, | ||
| startTimeNanos.get(), | ||
| clock.nanoTime(), | ||
| durationSeconds, | ||
| 0L, | ||
| extractUrlTemplate(req.getUrl()), | ||
| StorageErrorTypeMapper.STATUS_CONNECTION_ERROR); | ||
| } | ||
| return existingIoHandler != null | ||
|
|
@@ -132,36 +187,42 @@ public void initialize(HttpRequest request) throws IOException { | |
| } | ||
|
|
||
| private void recordResponse(HttpRequest request, long startNanos, long endNanos, int statusCode) { | ||
| double durationSeconds = (endNanos - startNanos) / 1_000_000_000.0; | ||
| String urlTemplate = extractUrlTemplate(request.getUrl()); | ||
| String errorType = | ||
| statusCode >= 400 | ||
| ? StorageErrorTypeMapper.mapHttpResponseCode(statusCode) | ||
| : StorageErrorTypeMapper.STATUS_OK; | ||
| recordDuration(request, startNanos, endNanos, statusCode, errorType); | ||
| // Evaluate isMediaDownloadRequest(request) at response time rather than in | ||
| // initialize(HttpRequest), because callers such as MediaHttpDownloader may append | ||
| // query parameters (e.g. alt=media) to request.getUrl() after buildGetRequest(). | ||
| if (isMediaDownloadRequest(request)) { | ||
| metrics.recordTtfb( | ||
| Context.current(), | ||
| "ReadObject", | ||
| (endNanos - startNanos) / 1_000_000_000.0, | ||
| StorageClientMetrics.NETWORK_PATH_CLOUDPATH); | ||
| } | ||
| recordDuration(request, durationSeconds, statusCode, urlTemplate, errorType); | ||
| // Evaluate request URL and query parameters at response time rather than in | ||
| // initialize(HttpRequest), because callers such as MediaHttpDownloader or MediaHttpUploader | ||
| // may append query parameters (e.g. alt=media, uploadType) after buildRequest(). | ||
| StorageMetricsContext ctx = StorageMetricsContext.current(); | ||
| Context otelContext = ctx != null ? ctx.getOtelContext() : Context.current(); | ||
| metrics.recordTtfb( | ||
| otelContext, | ||
| resolveOperationName(request, urlTemplate), | ||
| durationSeconds, | ||
| StorageClientMetrics.NETWORK_PATH_CLOUDPATH); | ||
| } | ||
|
|
||
| private void recordDuration( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| HttpRequest request, long startNanos, long endNanos, long statusCode, String errorType) { | ||
| double durationSeconds = (endNanos - startNanos) / 1_000_000_000.0; | ||
| HttpRequest request, | ||
| double durationSeconds, | ||
| long statusCode, | ||
| String urlTemplate, | ||
| String errorType) { | ||
| metrics.recordHttpRequestDuration( | ||
| durationSeconds, | ||
| request.getRequestMethod(), | ||
| statusCode, | ||
| extractUrlTemplate(request.getUrl()), | ||
| errorType); | ||
| durationSeconds, request.getRequestMethod(), statusCode, urlTemplate, errorType); | ||
| } | ||
|
|
||
| static String resolveOperationName(HttpRequest request, String urlTemplate) { | ||
| StorageMetricsContext ctx = StorageMetricsContext.current(); | ||
| return ctx != null ? ctx.getOperationName() : defaultOperationName(request, urlTemplate); | ||
| } | ||
|
|
||
| static boolean isMediaDownloadRequest(HttpRequest request) { | ||
| if (!"GET".equalsIgnoreCase(request.getRequestMethod())) { | ||
| if (!"GET".equalsIgnoreCase(effectiveMethod(request))) { | ||
| return false; | ||
| } | ||
| GenericUrl url = request.getUrl(); | ||
|
|
@@ -170,6 +231,18 @@ static boolean isMediaDownloadRequest(HttpRequest request) { | |
| || "media".equals(url.getFirst("alt")); | ||
| } | ||
|
|
||
| static boolean isMediaUploadRequest(HttpRequest request) { | ||
| String method = effectiveMethod(request); | ||
| if (!"POST".equalsIgnoreCase(method) && !"PUT".equalsIgnoreCase(method)) { | ||
| return false; | ||
| } | ||
| GenericUrl url = request.getUrl(); | ||
| String path = url.getRawPath(); | ||
| return (path != null && path.startsWith("/upload/storage/v1/")) | ||
| || url.getFirst("uploadType") != null | ||
| || url.getFirst("upload_id") != null; | ||
| } | ||
|
|
||
| static String extractUrlTemplate(GenericUrl url) { | ||
| String path = url.getRawPath(); | ||
| if (path == null || path.isEmpty()) { | ||
|
|
@@ -204,6 +277,23 @@ static String extractUrlTemplate(GenericUrl url) { | |
| return parts.length == 1 ? "/{bucket}" : "/{bucket}/{object}"; | ||
| } | ||
|
|
||
| private static String defaultOperationName(HttpRequest request, String urlTemplate) { | ||
| if (isMediaDownloadRequest(request)) { | ||
| return "ReadObject"; | ||
| } | ||
| if (isMediaUploadRequest(request)) { | ||
| return "WriteObject"; | ||
| } | ||
| return ROUTE_OPERATIONS.getOrDefault( | ||
| effectiveMethod(request).toUpperCase(Locale.ROOT) + " " + urlTemplate, | ||
| StorageErrorTypeMapper.STATUS_UNKNOWN); | ||
| } | ||
|
|
||
| private static String effectiveMethod(HttpRequest request) { | ||
| String override = request.getHeaders().getFirstHeaderStringValue("X-HTTP-Method-Override"); | ||
| return override != null ? override : request.getRequestMethod(); | ||
| } | ||
|
|
||
| private static String extractV1UrlTemplate(String rest) { | ||
| if ("b".equals(rest)) { | ||
| return "/storage/v1/b"; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For HTTP uploads, measuring TTFB from
startTimeNanosincludes the time spent writing the request body. Should we wrapHttpContentto record the timestamp afterwriteTofinishes and measure upload TTFB from there instead?