-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Feature Flags Service] Hello world 👋 #188562
Changes from 32 commits
478d593
5fb7016
cf8d28c
7abfbf4
7e82b16
8f22456
53a9daa
c2f1bd7
4df2227
1af5729
5f80536
d1a7d8c
37e23f7
7d4f065
3a2e5b6
8dc8628
6d3dc21
cd414b5
78cdc41
c92f509
2c00b35
0f6f908
42394a4
6dc2f51
f803423
3f77d4b
ac50eeb
9541f7e
320b717
3dc6060
5881d9e
b5e5699
ddb6063
bc268b8
eeacb85
59c2d99
9026d32
036a146
d888c6e
acaf70b
7f41811
728861c
43f366d
25afa70
9c97aaa
87179bb
5c8b3e0
3f76163
8b495d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# featureFlagsExample | ||
|
||
This plugin's goal is to demonstrate how to use the core feature flags service. | ||
|
||
Refer to [the docs](../../packages/core/feature-flags/README.mdx) to know more. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export const PLUGIN_ID = 'featureFlagsExample'; | ||
export const PLUGIN_NAME = 'Feature Flags Example'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"type": "plugin", | ||
"id": "@kbn/feature-flags-example-plugin", | ||
"owner": "@elastic/kibana-core", | ||
"description": "Plugin that shows how to make use of the feature flags core service.", | ||
"plugin": { | ||
"id": "featureFlagsExample", | ||
"server": true, | ||
"browser": true, | ||
"requiredPlugins": ["developerExamples"], | ||
"optionalPlugins": [] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { AppMountParameters, CoreStart } from '@kbn/core/public'; | ||
import { KibanaPageTemplate } from '@kbn/shared-ux-page-kibana-template'; | ||
import { KibanaRootContextProvider } from '@kbn/react-kibana-context-root'; | ||
import { FeatureFlagsExampleApp } from './components/app'; | ||
|
||
export const renderApp = (coreStart: CoreStart, { element }: AppMountParameters) => { | ||
const { notifications, http, featureFlags } = coreStart; | ||
ReactDOM.render( | ||
<KibanaRootContextProvider {...coreStart}> | ||
<KibanaPageTemplate> | ||
<FeatureFlagsExampleApp | ||
featureFlags={featureFlags} | ||
notifications={notifications} | ||
http={http} | ||
/> | ||
</KibanaPageTemplate> | ||
</KibanaRootContextProvider>, | ||
element | ||
); | ||
|
||
return () => ReactDOM.unmountComponentAtNode(element); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { | ||
EuiHorizontalRule, | ||
EuiPageTemplate, | ||
EuiTitle, | ||
EuiText, | ||
EuiLink, | ||
EuiListGroup, | ||
EuiListGroupItem, | ||
} from '@elastic/eui'; | ||
import type { CoreStart, FeatureFlagsStart } from '@kbn/core/public'; | ||
|
||
import useObservable from 'react-use/lib/useObservable'; | ||
import { PLUGIN_NAME } from '../../common'; | ||
|
||
interface FeatureFlagsExampleAppDeps { | ||
featureFlags: FeatureFlagsStart; | ||
notifications: CoreStart['notifications']; | ||
http: CoreStart['http']; | ||
} | ||
|
||
export const FeatureFlagsExampleApp = ({ featureFlags }: FeatureFlagsExampleAppDeps) => { | ||
// Fetching the feature flags synchronously | ||
const bool = featureFlags.getBooleanValue('example-boolean', false); | ||
const str = featureFlags.getStringValue('example-string', 'red'); | ||
const num = featureFlags.getNumberValue('example-number', 1); | ||
|
||
// Use React Hooks to observe feature flags changes | ||
const bool$ = useObservable(featureFlags.getBooleanValue$('example-boolean', false)); | ||
const str$ = useObservable(featureFlags.getStringValue$('example-string', 'red')); | ||
const num$ = useObservable(featureFlags.getNumberValue$('example-number', 1)); | ||
|
||
return ( | ||
<> | ||
<EuiPageTemplate> | ||
<EuiPageTemplate.Header> | ||
<EuiTitle size="l"> | ||
<h1>{PLUGIN_NAME}</h1> | ||
</EuiTitle> | ||
</EuiPageTemplate.Header> | ||
<EuiPageTemplate.Section> | ||
<EuiTitle> | ||
<h2>Demo of the feature flags service</h2> | ||
</EuiTitle> | ||
<EuiText> | ||
<p> | ||
To learn more, refer to{' '} | ||
<EuiLink | ||
href={'https://docs.elastic.dev/kibana-dev-docs/tutorials/feature-flags-service'} | ||
> | ||
the docs | ||
</EuiLink> | ||
. | ||
</p> | ||
<EuiHorizontalRule /> | ||
<EuiListGroup> | ||
<p> | ||
The feature flags are: | ||
<EuiListGroupItem label={`example-boolean: ${bool}`} /> | ||
<EuiListGroupItem label={`example-string: ${str}`} /> | ||
<EuiListGroupItem label={`example-number: ${num}`} /> | ||
</p> | ||
</EuiListGroup> | ||
<EuiListGroup> | ||
<p> | ||
The <strong>observed</strong> feature flags are: | ||
<EuiListGroupItem label={`example-boolean: ${bool$}`} /> | ||
<EuiListGroupItem label={`example-string: ${str$}`} /> | ||
<EuiListGroupItem label={`example-number: ${num$}`} /> | ||
</p> | ||
</EuiListGroup> | ||
</EuiText> | ||
</EuiPageTemplate.Section> | ||
</EuiPageTemplate> | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { FeatureFlagsExamplePlugin } from './plugin'; | ||
|
||
export function plugin() { | ||
return new FeatureFlagsExamplePlugin(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { AppMountParameters, CoreSetup, CoreStart, Plugin } from '@kbn/core/public'; | ||
import { AppPluginSetupDependencies } from './types'; | ||
import { PLUGIN_NAME } from '../common'; | ||
|
||
export class FeatureFlagsExamplePlugin implements Plugin { | ||
public setup(core: CoreSetup, deps: AppPluginSetupDependencies) { | ||
// Register an application into the side navigation menu | ||
core.application.register({ | ||
id: 'featureFlagsExample', | ||
title: PLUGIN_NAME, | ||
async mount(params: AppMountParameters) { | ||
// Load application bundle | ||
const { renderApp } = await import('./application'); | ||
// Get start services as specified in kibana.json | ||
const [coreStart] = await core.getStartServices(); | ||
// Render the application | ||
return renderApp(coreStart, params); | ||
}, | ||
}); | ||
|
||
deps.developerExamples.register({ | ||
appId: 'featureFlagsExample', | ||
title: PLUGIN_NAME, | ||
description: 'Plugin that shows how to make use of the feature flags core service.', | ||
}); | ||
} | ||
|
||
public start(core: CoreStart) {} | ||
|
||
public stop() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { DeveloperExamplesSetup } from '@kbn/developer-examples-plugin/public'; | ||
|
||
export interface AppPluginSetupDependencies { | ||
developerExamples: DeveloperExamplesSetup; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { FeatureFlagDefinitions } from '@kbn/core-feature-flags-server'; | ||
import type { PluginInitializerContext } from '@kbn/core-plugins-server'; | ||
|
||
export const featureFlags: FeatureFlagDefinitions = [ | ||
{ | ||
key: 'example-boolean', | ||
name: 'Example boolean', | ||
description: 'This is a demo of a boolean flag', | ||
tags: ['example', 'my-plugin'], | ||
variationType: 'boolean', | ||
variations: [ | ||
{ | ||
name: 'On', | ||
description: 'Auto-hides the bar', | ||
value: true, | ||
}, | ||
{ | ||
name: 'Off', | ||
description: 'Static always-on', | ||
value: false, | ||
}, | ||
], | ||
}, | ||
{ | ||
key: 'example-string', | ||
name: 'Example string', | ||
description: 'This is a demo of a string flag', | ||
tags: ['example', 'my-plugin'], | ||
variationType: 'string', | ||
variations: [ | ||
{ | ||
name: 'Pink', | ||
value: '#D75489', | ||
}, | ||
{ | ||
name: 'Turquoise', | ||
value: '#65BAAF', | ||
}, | ||
], | ||
}, | ||
{ | ||
key: 'example-number', | ||
name: 'Example Number', | ||
description: 'This is a demo of a number flag', | ||
tags: ['example', 'my-plugin'], | ||
variationType: 'number', | ||
variations: [ | ||
{ | ||
name: 'Five', | ||
value: 5, | ||
}, | ||
{ | ||
name: 'Ten', | ||
value: 10, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
export async function plugin(initializerContext: PluginInitializerContext) { | ||
const { FeatureFlagsExamplePlugin } = await import('./plugin'); | ||
return new FeatureFlagsExamplePlugin(initializerContext); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { | ||
PluginInitializerContext, | ||
CoreSetup, | ||
CoreStart, | ||
Plugin, | ||
Logger, | ||
} from '@kbn/core/server'; | ||
import { combineLatest } from 'rxjs'; | ||
|
||
import { defineRoutes } from './routes'; | ||
|
||
export class FeatureFlagsExamplePlugin implements Plugin { | ||
private readonly logger: Logger; | ||
|
||
constructor(initializerContext: PluginInitializerContext) { | ||
this.logger = initializerContext.logger.get(); | ||
} | ||
|
||
public setup(core: CoreSetup) { | ||
const router = core.http.createRouter(); | ||
|
||
// Register server side APIs | ||
defineRoutes(router); | ||
} | ||
|
||
public start(core: CoreStart) { | ||
// Promise form: when we need to fetch it once, like in an HTTP request | ||
void Promise.all([ | ||
core.featureFlags.getBooleanValue('example-boolean', false), | ||
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. question: what does happen if I have a typo in the flag name here? Just wondering how easy it'd be to catch bugs like this. 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. That's a great point! It will always return the fallback value. I should probably document as a good practice to define the flag key as a constant in Addressed in 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. Thanks! I'm also wondering if there's any valid use case where code tries to fetch a flag that isn't registered for some reason? 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. Technically, the flags don't need to be registered to run. That's why I am trying to decouple the gitops flow (the one requiring flags to be programmatically registered) and the actual runtime. I'm hoping this would avoid unnecessarily having extra objects in memory during runtime (to keep the registry).
I can only think about 2 edge scenarios:
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. Makes sense to me, thanks for clarifying! |
||
core.featureFlags.getStringValue('example-string', 'white'), | ||
core.featureFlags.getNumberValue('example-number', 1), | ||
]).then(([bool, str, num]) => { | ||
this.logger.info(`The feature flags are: | ||
- example-boolean: ${bool} | ||
- example-string: ${str} | ||
- example-number: ${num} | ||
`); | ||
}); | ||
|
||
// Observable form: when we need to react to the changes | ||
combineLatest([ | ||
core.featureFlags.getBooleanValue$('example-boolean', false), | ||
core.featureFlags.getStringValue$('example-string', 'red'), | ||
core.featureFlags.getNumberValue$('example-number', 1), | ||
]).subscribe(([bool, str, num]) => { | ||
this.logger.info(`The observed feature flags are: | ||
- example-boolean: ${bool} | ||
- example-string: ${str} | ||
- example-number: ${num} | ||
`); | ||
}); | ||
} | ||
|
||
public stop() {} | ||
} |
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.
We might want to update this once https://github.com/elastic/kibana-team/issues/997 is tackled.