Skip to content

Commit

Permalink
fix: erroneous warning about prop overwrite (#924)
Browse files Browse the repository at this point in the history
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 <pastro.craig@gmail.com>
Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
Craig Pastro and toddbaert committed Sep 19, 2023
1 parent af4c7ad commit 673b76a
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions core/pkg/eval/json_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 673b76a

Please sign in to comment.