-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(storage): add client feature tracking support #14320
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
Merged
krishnamd-jkp
merged 5 commits into
googleapis:main
from
krishnamd-jkp:multistream-tracking
Apr 13, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,6 +124,10 @@ type grpcStorageClient struct { | |
| settings *settings | ||
| config *storageConfig | ||
| dpDiag string | ||
|
|
||
| // configFeatureAttributes tracks client-level features that are enabled for this | ||
| // client instance. | ||
| configFeatureAttributes uint32 | ||
| } | ||
|
|
||
| func enableClientMetrics(ctx context.Context, s *settings, config storageConfig) (*metricsContext, error) { | ||
|
|
@@ -240,6 +244,17 @@ func (c *grpcStorageClient) prepareDirectPathMetadata(ctx context.Context, targe | |
| md.Set(requestParamsHeaderKey, reason) | ||
| } | ||
| } | ||
|
|
||
| // Client level feature tracking. | ||
|
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. It would be better if this could be moved to a helper, this might even be reused in http. |
||
| features := featureAttributes(ctx) | ||
| features |= c.configFeatureAttributes | ||
| // Merge all existing headers for this key from metadata. | ||
| features |= mergeFeatureAttributes(md[featureTrackerHeaderName]) | ||
|
|
||
| if features > 0 { | ||
| md.Set(featureTrackerHeaderName, encodeUint32(features)) | ||
| } | ||
|
|
||
| return metadata.NewOutgoingContext(ctx, md), nil | ||
| } | ||
|
|
||
|
|
||
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
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
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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package storage | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/googleapis/gax-go/v2/callctx" | ||
| ) | ||
|
|
||
| const featureTrackerHeaderName = "x-goog-storage-go-features" | ||
|
|
||
| // addFeatureAttributes adds the specified feature codes to the context. | ||
| // Features are stored as a bitmask in the callctx headers and will be | ||
| // injected into the outgoing request headers by the transport. | ||
| func addFeatureAttributes(ctx context.Context, features ...trackedFeature) context.Context { | ||
| if len(features) == 0 { | ||
| return ctx | ||
| } | ||
|
|
||
| current := featureAttributes(ctx) | ||
| updated := current | ||
| for _, f := range features { | ||
| updated |= (1 << f) | ||
| } | ||
|
|
||
| if updated == current { | ||
| return ctx | ||
| } | ||
|
|
||
| return callctx.SetHeaders(ctx, featureTrackerHeaderName, encodeUint32(uint32(updated))) | ||
| } | ||
|
|
||
| // featureAttributes extracts and merges all feature attributes present in the context. | ||
| // It returns a bitmask represented as a uint8. | ||
| func featureAttributes(ctx context.Context) uint32 { | ||
| ctxHeaders := callctx.HeadersFromContext(ctx) | ||
| // If multiple values are present in the context (e.g. from nested calls), | ||
| // merge them into a single bitmask. | ||
| return mergeFeatureAttributes(ctxHeaders[featureTrackerHeaderName]) | ||
| } | ||
|
|
||
| func mergeFeatureAttributes(vals []string) uint32 { | ||
| features := uint32(0) | ||
| for _, val := range vals { | ||
| if decoded, err := decodeUint32(val); err == nil { | ||
| features |= decoded | ||
| } | ||
| } | ||
| return features | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package storage | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestAddFeatureAttributes(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| // Initial features should be 0. | ||
| if got := featureAttributes(ctx); got != 0 { | ||
| t.Errorf("getFeatureAttributes(empty) = %d; want 0", got) | ||
| } | ||
|
|
||
| // Add a single feature. | ||
| ctx = addFeatureAttributes(ctx, featureMultistreamInMRD) | ||
| if got := featureAttributes(ctx); got != uint32(1<<featureMultistreamInMRD) { | ||
| t.Errorf("getFeatureAttributes(MultiStream) = %d; want %d", got, featureMultistreamInMRD) | ||
| } | ||
|
|
||
| // Add another feature (merge). | ||
| ctx = addFeatureAttributes(ctx, featurePCU) | ||
| want := uint32(1<<featureMultistreamInMRD) | uint32(1<<featurePCU) | ||
| if got := featureAttributes(ctx); got != want { | ||
| t.Errorf("getFeatureAttributes(MultiStream | PCU) = %d; want %d", got, want) | ||
| } | ||
|
|
||
| // Adding same feature should be idempotent. | ||
| ctx = addFeatureAttributes(ctx, featurePCU) | ||
| if got := featureAttributes(ctx); got != want { | ||
| t.Errorf("getFeatureAttributes(MultiStream | PCU | PCU) = %d; want %d", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestMergeFeatureAttributes(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| vals []string | ||
| want uint32 | ||
| }{ | ||
| { | ||
| name: "empty", | ||
| vals: []string{}, | ||
| want: 0, | ||
| }, | ||
| { | ||
| name: "single value", | ||
| vals: []string{encodeUint32(1)}, | ||
| want: 1, | ||
| }, | ||
| { | ||
| name: "multiple values", | ||
| vals: []string{encodeUint32(1), encodeUint32(2), encodeUint32(4)}, | ||
| want: 7, | ||
| }, | ||
| { | ||
| name: "overlapping values", | ||
| vals: []string{encodeUint32(3), encodeUint32(6)}, | ||
| want: 7, // 011 | 110 = 111 (7) | ||
| }, | ||
| { | ||
| name: "invalid values ignored", | ||
| vals: []string{encodeUint32(1), "invalid", encodeUint32(8)}, | ||
| want: 9, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| got := mergeFeatureAttributes(tc.vals) | ||
| if got != tc.want { | ||
| t.Errorf("mergeFeatureAttributes(%v) = %d; want %d", tc.vals, got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.