From 673b76aeff5c27e8e031e59da5f0ed1871d3f749 Mon Sep 17 00:00:00 2001 From: Craig Pastro Date: Tue, 19 Sep 2023 09:28:38 -0700 Subject: [PATCH] fix: erroneous warning about prop overwrite (#924) When adding flagdProperties to the context in preparation for evaluation return a new context so that it doesn't affect any references to the current context. Without this change: ``` $ make run make run-flagd cd flagd; go run main.go start -f file:../config/samples/example_flags.flagd.json ______ __ ________ _______ ______ /_____/\ /_/\ /_______/\ /______/\ /_____/\ \::::_\/_\:\ \ \::: _ \ \\::::__\/__\:::_ \ \ \:\/___/\\:\ \ \::(_) \ \\:\ /____/\\:\ \ \ \ \:::._\/ \:\ \____\:: __ \ \\:\\_ _\/ \:\ \ \ \ \:\ \ \:\/___/\\:.\ \ \ \\:\_\ \ \ \:\/.:| | \_\/ \_____\/ \__\/\__\/ \_____\/ \____/_/ 2023-09-18T19:16:39.581-0700 info cmd/start.go:117 flagd version: dev (HEAD), built at: unknown {"component": "start"} 2023-09-18T19:16:39.581-0700 info file/filepath_sync.go:37 Starting filepath sync notifier {"component": "sync", "sync": "filepath"} 2023-09-18T19:16:39.582-0700 info flag-evaluation/connect_service.go:203 metrics and probes listening at 8014 {"component": "service"} 2023-09-18T19:16:39.582-0700 info file/filepath_sync.go:66 watching filepath: ../config/samples/example_flags.flagd.json {"component": "sync", "sync": "filepath"} 2023-09-18T19:16:39.582-0700 info flag-evaluation/connect_service.go:183 Flag Evaluation listening at [::]:8013 {"component": "service"} 2023-09-18T19:16:41.258-0700 warn eval/json_evaluator.go:368 overwriting $flagd properties in the context {"component": "evaluator", "evaluator": "json"} 2023-09-18T19:16:41.259-0700 warn eval/json_evaluator.go:368 overwriting $flagd properties in the context {"component": "evaluator", "evaluator": "json"} 2023-09-18T19:16:41.259-0700 warn eval/legacy_fractional_evaluation.go:35 fractionalEvaluation is deprecated, please use fractional, see: https://flagd.dev/concepts/#migrating-from-legacy-fractionalevaluation 2023-09-18T19:16:41.259-0700 warn eval/json_evaluator.go:368 overwriting $flagd properties in the context {"component": "evaluator", "evaluator": "json"} ``` With this change: ``` $ make run make run-flagd cd flagd; go run main.go start -f file:../config/samples/example_flags.flagd.json ______ __ ________ _______ ______ /_____/\ /_/\ /_______/\ /______/\ /_____/\ \::::_\/_\:\ \ \::: _ \ \\::::__\/__\:::_ \ \ \:\/___/\\:\ \ \::(_) \ \\:\ /____/\\:\ \ \ \ \:::._\/ \:\ \____\:: __ \ \\:\\_ _\/ \:\ \ \ \ \:\ \ \:\/___/\\:.\ \ \ \\:\_\ \ \ \:\/.:| | \_\/ \_____\/ \__\/\__\/ \_____\/ \____/_/ 2023-09-18T19:20:29.011-0700 info cmd/start.go:117 flagd version: dev (HEAD), built at: unknown {"component": "start"} 2023-09-18T19:20:29.012-0700 info file/filepath_sync.go:37 Starting filepath sync notifier {"component": "sync", "sync": "filepath"} 2023-09-18T19:20:29.013-0700 info flag-evaluation/connect_service.go:203 metrics and probes listening at 8014 {"component": "service"} 2023-09-18T19:20:29.013-0700 info flag-evaluation/connect_service.go:183 Flag Evaluation listening at [::]:8013 {"component": "service"} 2023-09-18T19:20:29.014-0700 info file/filepath_sync.go:66 watching filepath: ../config/samples/example_flags.flagd.json {"component": "sync", "sync": "filepath"} 2023-09-18T19:20:32.784-0700 warn eval/legacy_fractional_evaluation.go:35 fractionalEvaluation is deprecated, please use fractional, see: https://flagd.dev/concepts/#migrating-from-legacy-fractionalevaluation ``` Signed-off-by: Craig Pastro Co-authored-by: Todd Baert --- core/pkg/eval/json_evaluator.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/core/pkg/eval/json_evaluator.go b/core/pkg/eval/json_evaluator.go index edf338bfc..153a4910f 100644 --- a/core/pkg/eval/json_evaluator.go +++ b/core/pkg/eval/json_evaluator.go @@ -22,6 +22,7 @@ import ( "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" "go.uber.org/zap" + "golang.org/x/exp/maps" ) const ( @@ -362,13 +363,15 @@ func (je *JSONEvaluator) setFlagdProperties( context = map[string]any{} } - if _, ok := context[flagdPropertiesKey]; ok { + newContext := maps.Clone(context) + + if _, ok := newContext[flagdPropertiesKey]; ok { je.Logger.Warn("overwriting $flagd properties in the context") } - context[flagdPropertiesKey] = properties + newContext[flagdPropertiesKey] = properties - return context + return newContext } func getFlagdProperties(context map[string]any) (flagdProperties, bool) {