Pass eval ctx attributes to frontend

This commit is contained in:
Tania B. 2025-10-06 15:59:25 +02:00
parent a595f4d489
commit 4d8e56debe
No known key found for this signature in database
GPG Key ID: 59C8B1BB068FA8A4
5 changed files with 16 additions and 9 deletions

View File

@ -305,6 +305,7 @@ type FrontendSettingsDTO struct {
LocalFileSystemAvailable bool `json:"localFileSystemAvailable"` LocalFileSystemAvailable bool `json:"localFileSystemAvailable"`
// Experimental Scope settings // Experimental Scope settings
ListScopesEndpoint string `json:"listScopesEndpoint"` ListScopesEndpoint string `json:"listScopesEndpoint"`
ListDashboardScopesEndpoint string `json:"listDashboardScopesEndpoint"` ListDashboardScopesEndpoint string `json:"listDashboardScopesEndpoint"`
OpenFeatureContext map[string]string `json:"openFeatureContext"`
} }

View File

@ -350,6 +350,7 @@ func (hs *HTTPServer) getFrontendSettings(c *contextmodel.ReqContext) (*dtos.Fro
MaxIdleConns: hs.Cfg.SqlDatasourceMaxIdleConnsDefault, MaxIdleConns: hs.Cfg.SqlDatasourceMaxIdleConnsDefault,
ConnMaxLifetime: hs.Cfg.SqlDatasourceMaxConnLifetimeDefault, ConnMaxLifetime: hs.Cfg.SqlDatasourceMaxConnLifetimeDefault,
}, },
OpenFeatureContext: hs.Cfg.OpenFeature.ContextAttrs,
} }
if hs.Cfg.UnifiedAlerting.StateHistory.Enabled { if hs.Cfg.UnifiedAlerting.StateHistory.Enabled {

View File

@ -40,7 +40,12 @@ func InitOpenFeatureWithCfg(cfg *setting.Cfg) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to initialize OpenFeature: %w", err) return fmt.Errorf("failed to initialize OpenFeature: %w", err)
} }
openfeature.SetEvaluationContext(openfeature.NewEvaluationContext(cfg.OpenFeature.TargetingKey, cfg.OpenFeature.ContextAttrs)) // Convert map[string]string to map[string]any for OpenFeature SDK
contextAttrs := make(map[string]any)
for k, v := range cfg.OpenFeature.ContextAttrs {
contextAttrs[k] = v
}
openfeature.SetEvaluationContext(openfeature.NewEvaluationContext(cfg.OpenFeature.TargetingKey, contextAttrs))
return nil return nil
} }

View File

@ -15,7 +15,7 @@ type OpenFeatureSettings struct {
ProviderType string ProviderType string
URL *url.URL URL *url.URL
TargetingKey string TargetingKey string
ContextAttrs map[string]any ContextAttrs map[string]string
} }
func (cfg *Cfg) readOpenFeatureSettings() error { func (cfg *Cfg) readOpenFeatureSettings() error {
@ -38,7 +38,7 @@ func (cfg *Cfg) readOpenFeatureSettings() error {
// build the eval context attributes using [feature_toggles.openfeature.context] section // build the eval context attributes using [feature_toggles.openfeature.context] section
ctxConf := cfg.Raw.Section("feature_toggles.openfeature.context") ctxConf := cfg.Raw.Section("feature_toggles.openfeature.context")
attrs := map[string]any{} attrs := map[string]string{}
for _, key := range ctxConf.KeyStrings() { for _, key := range ctxConf.KeyStrings() {
attrs[key] = ctxConf.Key(key).String() attrs[key] = ctxConf.Key(key).String()
} }

View File

@ -11,11 +11,11 @@ func Test_CtxAttrs(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
conf string conf string
expected map[string]any expected map[string]string
}{ }{
{ {
name: "empty config - only default attributes should be present", name: "empty config - only default attributes should be present",
expected: map[string]any{ expected: map[string]string{
"grafana_version": "", "grafana_version": "",
}, },
}, },
@ -26,7 +26,7 @@ func Test_CtxAttrs(t *testing.T) {
foo = bar foo = bar
baz = qux baz = qux
quux = corge`, quux = corge`,
expected: map[string]any{ expected: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "qux", "baz": "qux",
"quux": "corge", "quux": "corge",
@ -39,7 +39,7 @@ quux = corge`,
[feature_toggles.openfeature.context] [feature_toggles.openfeature.context]
grafana_version = 10.0.0 grafana_version = 10.0.0
foo = bar`, foo = bar`,
expected: map[string]any{ expected: map[string]string{
"grafana_version": "10.0.0", "grafana_version": "10.0.0",
"foo": "bar", "foo": "bar",
}, },