-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature flags from environment variable (#722)
* Add featuretoggles package * Change featuretoggles to experimental package * Update folder.golden.txt --------- Co-authored-by: Giuseppe Guerra <[email protected]>
- Loading branch information
1 parent
976bbdf
commit d4c32f3
Showing
3 changed files
with
95 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package featuretoggles | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
// envFeatureTogglesEnable is the environment variable set by Grafana containing the list of enabled feature toggles. | ||
const envFeatureTogglesEnable = "GF_INSTANCE_FEATURE_TOGGLES_ENABLE" | ||
|
||
// FeatureToggles can check if feature toggles are enabled on the Grafana instance. | ||
type FeatureToggles interface { | ||
// IsEnabled returns true if the provided feature flag is set. | ||
IsEnabled(flag string) bool | ||
} | ||
|
||
// featureToggles implements a FeatureToggles that returns true if a flag is present in the flags map. | ||
type featureToggles struct { | ||
// flags is a set-like map of feature flags that are enabled. | ||
flags map[string]struct{} | ||
} | ||
|
||
// IsEnabled returns true if flag is contained in f.flags. | ||
func (f featureToggles) IsEnabled(flag string) bool { | ||
_, ok := f.flags[flag] | ||
return ok | ||
} | ||
|
||
// newFeatureTogglesFromEnv returns a new featureToggles instance with its flags set from environment variables. | ||
func newFeatureTogglesFromEnv() featureToggles { | ||
return featureToggles{flags: flagsMapFromEnv()} | ||
} | ||
|
||
// flagsMapFromEnv returns a new set-like map[string]struct{}, where the keys are the space-separated names in | ||
// the `envFeatureTogglesEnable` env var. | ||
func flagsMapFromEnv() map[string]struct{} { | ||
flags := strings.Split(os.Getenv(envFeatureTogglesEnable), ",") | ||
r := make(map[string]struct{}, len(flags)) | ||
for _, flag := range flags { | ||
r[flag] = struct{}{} | ||
} | ||
return r | ||
} | ||
|
||
// DefaultFeatureToggles is the default feature toggles implementation. | ||
// It contains the same feature toggles as the Grafana instance where the plugin is running. | ||
var DefaultFeatureToggles FeatureToggles = newFeatureTogglesFromEnv() |
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,30 @@ | ||
package featuretoggles | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEnvFeatureToggles(t *testing.T) { | ||
t.Run("should work when flag is provided", func(t *testing.T) { | ||
t.Setenv(envFeatureTogglesEnable, "") | ||
flags := newFeatureTogglesFromEnv() | ||
require.False(t, flags.IsEnabled("abc")) | ||
}) | ||
|
||
t.Run("should work when single flag is provided", func(t *testing.T) { | ||
t.Setenv(envFeatureTogglesEnable, "abc") | ||
flags := newFeatureTogglesFromEnv() | ||
require.True(t, flags.IsEnabled("abc")) | ||
require.False(t, flags.IsEnabled("def")) | ||
}) | ||
|
||
t.Run("should work when multiple flags are provided", func(t *testing.T) { | ||
t.Setenv(envFeatureTogglesEnable, "abc,def") | ||
flags := newFeatureTogglesFromEnv() | ||
require.True(t, flags.IsEnabled("abc")) | ||
require.True(t, flags.IsEnabled("def")) | ||
require.False(t, flags.IsEnabled("ghi")) | ||
}) | ||
} |
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