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

@ -307,4 +307,5 @@ type FrontendSettingsDTO struct {
// Experimental Scope settings
ListScopesEndpoint string `json:"listScopesEndpoint"`
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,
ConnMaxLifetime: hs.Cfg.SqlDatasourceMaxConnLifetimeDefault,
},
OpenFeatureContext: hs.Cfg.OpenFeature.ContextAttrs,
}
if hs.Cfg.UnifiedAlerting.StateHistory.Enabled {

View File

@ -40,7 +40,12 @@ func InitOpenFeatureWithCfg(cfg *setting.Cfg) error {
if err != nil {
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
}

View File

@ -15,7 +15,7 @@ type OpenFeatureSettings struct {
ProviderType string
URL *url.URL
TargetingKey string
ContextAttrs map[string]any
ContextAttrs map[string]string
}
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
ctxConf := cfg.Raw.Section("feature_toggles.openfeature.context")
attrs := map[string]any{}
attrs := map[string]string{}
for _, key := range ctxConf.KeyStrings() {
attrs[key] = ctxConf.Key(key).String()
}

View File

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