Files
grafana/pkg/setting/setting_openfeature.go
2025-06-13 17:30:53 +02:00

52 lines
1.2 KiB
Go

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
}