Repository navigation
feat(storage): add MetricsRecordingCredentials decorator for credential_refresh.duration - #15233
nidhiii-27 wants to merge 8 commits into
Conversation
…al_refresh.duration Add Debug-tier StorageClientMetrics.recordCredentialRefreshDuration (exact 4-key clientAttributes + error.type, unit "s") and package-private MetricsRecordingCredentials extending GoogleCredentials to preserve PRIVACY_AND_INTEGRITY and QuotaProjectIdProvider, skip incompatible credential types (NoCredentials, non-GoogleCredentials, GdchCredentials, unscoped ServiceAccountCredentials, AppEngineCredentials), deduplicate concurrent coalesced refreshes with a backward-clobber-safe CAS loop, and handle Java serialization safely. [Generated-by: AI]
There was a problem hiding this comment.
Code Review
This pull request introduces MetricsRecordingCredentials, a decorator for GoogleCredentials that records credential refresh durations, along with corresponding metric recording methods and unit tests. The review feedback highlights a correctness issue where getAccessToken() and refreshAccessToken() are not overridden, which could cause downstream components to receive stale or null tokens. Additionally, an optimization is suggested for maybeRecordTokenChange to reduce lock contention by avoiding redundant calls to delegate.getAccessToken() inside a loop.
…age-credential-refresh-wrapper [Generated-by: AI]
Construct real GdchCredentials and ServiceAccountCredentials instances via their builders in MetricsRecordingCredentialsTest instead of Mockito mocks. In google-auth-library 1.58.0, both classes carry JSpecify @NullMarked (whose @target includes ElementType.MODULE), which causes ByteBuddy's annotation inspection to throw ArrayStoreException on JDK 8. [Generated-by: AI]
…age-credential-refresh-wrapper [Generated-by: AI]
…t test MetricsRecordingCredentialsTest already asserts the exact four attribute keys (no server.address or rpc.system.name) and the unit end to end. [Generated-by: AI]
…age-credential-refresh-wrapper [Generated-by: AI]
…age-credential-refresh-wrapper [Generated-by: AI]
…age-credential-refresh-wrapper [Generated-by: AI]
| * failures are recorded per waiter. | ||
| */ | ||
| @NullMarked | ||
| final class MetricsRecordingCredentials extends GoogleCredentials { |
There was a problem hiding this comment.
Should we override equals and hashCode to delegate to delegate, since inheriting GoogleCredentials.equals compares unset superclass fields and treats any two wrapped credentials as equal?
| } | ||
|
|
||
| @VisibleForTesting | ||
| static String mapAuthErrorType(@Nullable Throwable t) { |
There was a problem hiding this comment.
Nit: Can we move mapAuthErrorType into StorageErrorTypeMapper to reuse MAX_CAUSE_DEPTH and keep error-type mapping in one place?
| public GoogleCredentials createScoped( | ||
| Collection<String> scopes, Collection<String> defaultScopes) { | ||
| return rewrap(delegate.createScoped(scopes, defaultScopes)); | ||
| } |
There was a problem hiding this comment.
Should we also override createWithQuotaProject, createDelegated, createWithCustomRetryStrategy, and getCredentialInfo so createWithQuotaProject doesn't drop delegate via GoogleCredentials.toBuilder()?
| } | ||
|
|
||
| @Override | ||
| public Map<String, List<String>> getRequestMetadata(@Nullable URI uri) throws IOException { |
There was a problem hiding this comment.
Nit: Can we generalize runRecorded to return a value so getRequestMetadata(URI) doesn't duplicate the same timing and recording block?
| Map<String, List<String>> metadata = delegate.getRequestMetadata(uri); | ||
| maybeRecordTokenChange(m, c, otelContext, startNanos, before); | ||
| return metadata; | ||
| } catch (IOException e) { |
There was a problem hiding this comment.
Should we also catch RuntimeException here, in runRecorded, and around the async delegate.getRequestMetadata call since OAuth2Credentials rethrows RuntimeException causes like CancellationException directly?
| .isSameInstanceAs(nonGoogle); | ||
| assertThat(MetricsRecordingCredentials.wrap(gdch, debugMetrics, fakeClock)) | ||
| .isSameInstanceAs(gdch); | ||
| assertThat(MetricsRecordingCredentials.wrap(unscopedSa, debugMetrics, fakeClock)) |
There was a problem hiding this comment.
Can we also add a case for a scoped ServiceAccountCredentials to verify it gets wrapped?
| assertThat(wrapped.hasRequestMetadata()).isTrue(); | ||
| assertThat(wrapped.hasRequestMetadataOnly()).isTrue(); | ||
| assertThat(wrapped.createScopedRequired()).isFalse(); | ||
| assertThat(wrapped.createScoped(Collections.singletonList("scope"))).isSameInstanceAs(wrapped); |
There was a problem hiding this comment.
Can we also test both createScoped overloads with a delegate that returns a new instance so the rewrap() path where scoped != delegate is covered?
| assertThat(wrapped.getRequestMetadata(STORAGE_URI)).containsKey("Authorization"); | ||
| clockNanos.addAndGet(50_000_000L); | ||
| assertThat(wrapped.getRequestMetadata(STORAGE_URI)).containsKey("Authorization"); | ||
| wrapped.refreshIfExpired(); |
There was a problem hiding this comment.
Can we also test refreshIfExpired() on an uninitialized or expired credential to cover its refresh recording path?
Summary
Stacked on #15232.
Adds Debug-tier
gcp.storage.client.auth.credential_refresh.duration(DoubleHistogram, units) and the package-privateMetricsRecordingCredentialsdecorator to measure access token acquisition/refresh latency while omitting cached token hits.Changes
StorageClientMetrics: addrecordCredentialRefreshDuration(Context, double durationSeconds, String errorType)emitting exact 4-key attributes (gcp.client.service,gcp.client.version,gcp.client.artifact,error.type, omittingserver.addressandrpc.system.name).MetricsRecordingCredentials: package-private decorator extendingGoogleCredentials(preserving gRPCPRIVACY_AND_INTEGRITYandQuotaProjectIdProvidersemantics):NoCredentials, non-GoogleCredentials,GdchCredentials, unscopedServiceAccountCredentials, andAppEngineCredentials).getRequestMetadata, asynchronousgetRequestMetadata(URI, Executor, RequestMetadataCallback),refresh(), andrefreshIfExpired()using a backward-clobber-safe CAS loop so cached token hits and concurrent coalesced refreshes are not double-counted.StorageClientMetricsTestandMetricsRecordingCredentialsTest.[Generated-by: AI]