Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return flag metadata as well #1476

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion core/pkg/evaluator/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const (
// evaluation if the user did not supply the optional bucketing property.
targetingKeyKey = "targetingKey"
Disabled = "DISABLED"
ID = "id"
VERSION = "version"
FLAGSETVERSION = "flagSetVersion"
FLAGSETID = "flagSetId"
)

var regBrace *regexp.Regexp
Expand Down Expand Up @@ -327,6 +331,20 @@ func (je *Resolver) evaluateVariant(ctx context.Context, reqID string, flagKey s
metadata[SelectorMetadataKey] = selector
}

flagMetadata := je.store.MetadataForFlag(ctx, flag)
if flagMetadata.ID != "" {
metadata[ID] = flagMetadata.ID
}
if flagMetadata.Version != "" {
metadata[VERSION] = flagMetadata.Version
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the value of these is, since we are not returning them in any responses. Maybe I'm confused.

Copy link
Author

@aasifkhan7 aasifkhan7 Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@toddbaert The idea behind this is that each flag could also have its own specific metadata. For example, a flag configuration might look like this:

{
  "flags": {
    "myBoolFlag": {
      "state": "ENABLED",
      "variants": {
        "on": true,
        "off": false
      },
      "defaultVariant": "on",
      "metadata": {
        "id": "sso/dev",
        "version": "1.0.0"
      }
    }
  },
  "metadata": {
    "id": "test",
    "version": "1"
  }
}

Here, the flag-level metadata (id, version) allows for more granular identification and versioning of individual flags, in addition to the overarching metadata. Let me know your thoughts!

if flagMetadata.FlagSetID != "" {
metadata[FLAGSETID] = flagMetadata.FlagSetID
}
if flagMetadata.FlagSetVersion != "" {
metadata[FLAGSETVERSION] = flagMetadata.FlagSetVersion
}

if flag.State == Disabled {
je.Logger.DebugWithID(reqID, fmt.Sprintf("requested flag is disabled: %s", flagKey))
return "", flag.Variants, model.ErrorReason, metadata, errors.New(model.FlagDisabledErrorCode)
Expand Down Expand Up @@ -460,11 +478,22 @@ func configToFlags(log *logger.Logger, config string, newFlags *Flags) error {
return fmt.Errorf("transposing evaluators: %w", err)
}

err = json.Unmarshal([]byte(transposedConfig), &newFlags)
var configData ConfigWithMetadata
err = json.Unmarshal([]byte(transposedConfig), &configData)
if err != nil {
return fmt.Errorf("unmarshalling provided configurations: %w", err)
}

// Assign the flags from the unmarshalled config to the newFlags struct
newFlags.Flags = configData.Flags

// Assign version and id from metadata to the flags
for key, flag := range newFlags.Flags {
flag.Metadata.FlagSetID = configData.MetaData.ID
flag.Metadata.FlagSetVersion = configData.MetaData.Version
newFlags.Flags[key] = flag
}

return validateDefaultVariants(newFlags)
}

Expand Down
10 changes: 10 additions & 0 deletions core/pkg/evaluator/json_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ type Evaluators struct {
Evaluators map[string]json.RawMessage `json:"$evaluators"`
}

type Metadata struct {
ID string `json:"id"`
Version string `json:"version"`
}

type ConfigWithMetadata struct {
Flags map[string]model.Flag `json:"flags"`
MetaData Metadata `json:"metadata"`
}

type Flags struct {
Flags map[string]model.Flag `json:"flags"`
}
8 changes: 8 additions & 0 deletions core/pkg/model/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ type Flag struct {
Targeting json.RawMessage `json:"targeting,omitempty"`
Source string `json:"source"`
Selector string `json:"selector"`
Metadata Metadata `json:"metadata"`
}

type Metadata struct {
ID string `json:"id"`
Version string `json:"version"`
FlagSetID string `json:"flagSetId"`
FlagSetVersion string `json:"flagSetVersion"`
}

type Evaluators struct {
Expand Down
9 changes: 9 additions & 0 deletions core/pkg/store/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ type IStore interface {
GetAll(ctx context.Context) (map[string]model.Flag, error)
Get(ctx context.Context, key string) (model.Flag, bool)
SelectorForFlag(ctx context.Context, flag model.Flag) string
MetadataForFlag(ctx context.Context, flag model.Flag) model.Metadata
}

type Flags struct {
mx sync.RWMutex
Flags map[string]model.Flag `json:"flags"`
FlagSources []string
SourceMetadata map[string]SourceDetails
Metadata model.Metadata `json:"metadata"`
}

type SourceDetails struct {
Expand Down Expand Up @@ -72,6 +74,13 @@ func (f *Flags) SelectorForFlag(_ context.Context, flag model.Flag) string {
return f.SourceMetadata[flag.Source].Selector
}

func (f *Flags) MetadataForFlag(_ context.Context, flag model.Flag) model.Metadata {
aasifkhan7 marked this conversation as resolved.
Show resolved Hide resolved
f.mx.RLock()
defer f.mx.RUnlock()

return flag.Metadata
}

func (f *Flags) Delete(key string) {
f.mx.Lock()
defer f.mx.Unlock()
Expand Down
Loading