mirror of
https://github.com/grafana/grafana.git
synced 2025-08-03 06:12:20 +08:00
Extend OpenFeature service (#106707)
This commit is contained in:
51
pkg/setting/setting_openfeature.go
Normal file
51
pkg/setting/setting_openfeature.go
Normal file
@ -0,0 +1,51 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
StaticProviderType = "static"
|
||||
GOFFProviderType = "goff"
|
||||
)
|
||||
|
||||
type OpenFeatureSettings struct {
|
||||
ProviderType string
|
||||
URL *url.URL
|
||||
TargetingKey string
|
||||
ContextAttrs map[string]any
|
||||
}
|
||||
|
||||
func (cfg *Cfg) readOpenFeatureSettings() error {
|
||||
cfg.OpenFeature = OpenFeatureSettings{}
|
||||
|
||||
config := cfg.Raw.Section("feature_toggles.openfeature")
|
||||
cfg.OpenFeature.ProviderType = config.Key("provider").MustString(StaticProviderType)
|
||||
cfg.OpenFeature.TargetingKey = config.Key("targetingKey").MustString(cfg.AppURL)
|
||||
|
||||
strURL := config.Key("url").MustString("")
|
||||
|
||||
if strURL != "" && cfg.OpenFeature.ProviderType == GOFFProviderType {
|
||||
u, err := url.Parse(strURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid feature provider url: %w", err)
|
||||
}
|
||||
cfg.OpenFeature.URL = u
|
||||
}
|
||||
|
||||
// build the eval context attributes using [feature_toggles.openfeature.context] section
|
||||
ctxConf := cfg.Raw.Section("feature_toggles.openfeature.context")
|
||||
attrs := map[string]any{}
|
||||
for _, key := range ctxConf.KeyStrings() {
|
||||
attrs[key] = ctxConf.Key(key).String()
|
||||
}
|
||||
|
||||
// Some default attributes
|
||||
if _, ok := attrs["grafana_version"]; !ok {
|
||||
attrs["grafana_version"] = BuildVersion
|
||||
}
|
||||
|
||||
cfg.OpenFeature.ContextAttrs = attrs
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user