-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mechanism to include feature flags in Polymer HTTP requests (#5727)
This adds a mechanism that dumps the client-side FeatureFlag object into each HTTP request that originates from the TB Polymer code base. (Googlers, see: http://go/tb-client-feature-flags-in-data-provider for the motivation and high level design.) See #5718 for the corresponding change for Angular and #5721 for server-side support. The data is dumped into a custom header. The format is effectively: X-TensorBoard-Feature-Flags: JSON.stringify(currentFeatureFlags) We implement this directly in the RequestManager object, through which all our Polymer HTTP requests are routed. Additional things worth noting: * I added a const for the header name to be shared between the HttpInterceptor and the RequestManager. * I added a new angular<->polymer interop object, tf-feature-flags, modeled after some of the other interop objects we've added in the past. This interop object should now be the source of truth for accessing feature flags from the polymer code base. It is populated by feature_flags_effects soon after we fetch feature flag overrides from the data source. * This will send ALL feature flags in the request header rather than a curated subset of them, just like in #5718. In a subsequent PR, we'll add support to mark certain feature flags as "sendToServer" and only send that subset.
- Loading branch information
Showing
17 changed files
with
281 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,38 @@ | ||
load("//tensorboard/defs:defs.bzl", "tf_ng_web_test_suite", "tf_ts_library") | ||
|
||
package(default_visibility = ["//tensorboard:internal"]) | ||
|
||
licenses(["notice"]) | ||
|
||
tf_ts_library( | ||
name = "tf_feature_flags", | ||
srcs = [ | ||
"feature-flags.ts", | ||
"tf-feature-flags-polymer.ts", | ||
], | ||
deps = [ | ||
"//tensorboard/webapp/feature_flag:types", | ||
"@npm//@polymer/decorators", | ||
"@npm//@polymer/polymer", | ||
], | ||
) | ||
|
||
tf_ts_library( | ||
name = "test_lib", | ||
testonly = True, | ||
srcs = [ | ||
"feature-flags-test.ts", | ||
], | ||
deps = [ | ||
":tf_feature_flags", | ||
"//tensorboard/webapp/feature_flag:testing", | ||
"@npm//@types/jasmine", | ||
], | ||
) | ||
|
||
tf_ng_web_test_suite( | ||
name = "karma_test", | ||
deps = [ | ||
":test_lib", | ||
], | ||
) |
36 changes: 36 additions & 0 deletions
36
tensorboard/components/tf_feature_flags/feature-flags-test.ts
This file contains 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,36 @@ | ||
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
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. | ||
==============================================================================*/ | ||
|
||
import {buildFeatureFlag} from '../../webapp/feature_flag/testing'; | ||
import * as feature_flags from './feature-flags'; | ||
|
||
describe('feature-flags', () => { | ||
beforeEach(() => { | ||
feature_flags.initializeFeatureFlags(); | ||
}); | ||
|
||
it('sets and gets FeatureFlags', () => { | ||
feature_flags.setFeatureFlags(buildFeatureFlag({inColab: true})); | ||
expect(feature_flags.getFeatureFlags()).toEqual( | ||
buildFeatureFlag({inColab: true}) | ||
); | ||
}); | ||
|
||
it('throws Error if getFeatureFlags is called before setFeatureFlags', () => { | ||
expect(() => feature_flags.getFeatureFlags()).toThrow( | ||
new Error('FeatureFlags have not yet been determined by TensorBoard.') | ||
); | ||
}); | ||
}); |
This file contains 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,50 @@ | ||
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
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. | ||
==============================================================================*/ | ||
|
||
import {FeatureFlags} from '../../webapp/feature_flag/types'; | ||
|
||
// FeatureFlags are managed and injected by the Angular portion of the TB code | ||
// base. In practice they are set one time soon after application load and never | ||
// again for the lifetime of the application. | ||
let _featureFlags: FeatureFlags | null; | ||
initializeFeatureFlags(); | ||
|
||
export function initializeFeatureFlags(): void { | ||
_featureFlags = null; | ||
} | ||
|
||
/** | ||
* Sets the FeatureFlags for use in the Polymer portion of the TB code base. | ||
* | ||
* In practice this should only be called by the Angular portion of the TB code | ||
* base immediately after it determines the final set of FeatureFlags. | ||
*/ | ||
export function setFeatureFlags(featureFlags: FeatureFlags): void { | ||
_featureFlags = featureFlags; | ||
} | ||
|
||
/** | ||
* Retrieves FeatureFlags. | ||
* | ||
* @throws Error if FeatureFlags have not yet been set. In practice they should | ||
* be set soon after application load before any Polymer code is invoked. | ||
* This runtime check acts as a sanity check to enforce that assumption. | ||
*/ | ||
export function getFeatureFlags(): FeatureFlags { | ||
if (_featureFlags === null) { | ||
throw Error('FeatureFlags have not yet been determined by TensorBoard.'); | ||
} | ||
return _featureFlags; | ||
} |
27 changes: 27 additions & 0 deletions
27
tensorboard/components/tf_feature_flags/tf-feature-flags-polymer.ts
This file contains 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,27 @@ | ||
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
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. | ||
==============================================================================*/ | ||
import {customElement} from '@polymer/decorators'; | ||
import {PolymerElement} from '@polymer/polymer'; | ||
import * as tf_feature_flags from './feature-flags'; | ||
|
||
/** | ||
* This Polymer component allows the underlying API to be accessible from | ||
* Angular TensorBoard by exposing otherwise mangled smybols. | ||
*/ | ||
@customElement('tf-feature-flags') | ||
class TfFeatureFlags extends PolymerElement { | ||
override _template = null; | ||
tf_feature_flags = tf_feature_flags; | ||
} |
This file contains 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 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 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 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 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 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,16 @@ | ||
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
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. | ||
==============================================================================*/ | ||
|
||
export const FEATURE_FLAGS_HEADER_NAME = 'X-TensorBoard-Feature-Flags'; |
Oops, something went wrong.