PostHog / posthog-go

Official PostHog Go library
MIT License
20 stars 17 forks source link

GetAllFlags result doesn't match IsFeatureEnabled or GetFeatureFlag #14

Closed AChelikani closed 1 year ago

AChelikani commented 1 year ago

I'm trying to use feature flags with groups since I have many anonymous users within an organization for my use case. I created a flag with key test and tried variations where it was on for all, on for just my desired organization, and off for all. The results of GetAllFlags appears to be always correct, while the results for IsFeatureEnabled and GetFeatureFlag are not consistent with GetAllFlags and sometimes wrong.

I'm using the latest SDK.

Code:

func (f *FeatureFlagAccessor) Test(orgID string) {
    flags, err := f.client.GetAllFlags(posthog.FeatureFlagPayloadNoKey{
        DistinctId: "-",
        Groups:     map[string]interface{}{"organization": orgID},
    })
    utils.PrettyPrintStruct(err)
    utils.PrettyPrintStruct(flags)

    singleFlag, err := f.client.GetFeatureFlag(posthog.FeatureFlagPayload{
        Key:        "test",
        DistinctId: "-",
        Groups:     map[string]interface{}{"organization": orgID},
    })
    utils.PrettyPrintStruct(err)
    utils.PrettyPrintStruct(singleFlag)

    isFlagEnabled, err := f.client.IsFeatureEnabled(posthog.FeatureFlagPayload{
        Key:        "test",
        DistinctId: "-",
        Groups:     map[string]interface{}{"organization": orgID},
    })
    utils.PrettyPrintStruct(err)
    utils.PrettyPrintStruct(isFlagEnabled)
}

Feature flag set to enabled with no release conditions:

null
{
    "test": true
}

null
true

null
true

Feature flag turned on for just my orgID:

posthog 2022/12/19 23:33:33 ERROR: Unable to compute flag locally - Can't determine if feature flag is enabled or not with given properties
null
{
    "test": true
}

posthog 2022/12/19 23:33:33 ERROR: Unable to compute flag locally - Can't determine if feature flag is enabled or not with given properties
null
false

posthog 2022/12/19 23:33:33 ERROR: Unable to compute flag locally - Can't determine if feature flag is enabled or not with given properties
null
false

Feature flag set to disabled with no release conditions:

null
{
    "test": false
}

null
false

null
false
neilkakkar commented 1 year ago

Hey @AChelikani , what conditions do you add when you turn on the flag for your org?

🤔

neilkakkar commented 1 year ago

Ah, great catch, I see it. We pass in nil to groups instead of the correct group when evaluating flags. This affects anyone using go for feature flags with groups.

https://github.com/PostHog/posthog-go/blob/master/featureflags.go#L780

AChelikani commented 1 year ago

Thanks for the quick response - I opened up a PR if you don't mind taking a look.

neilkakkar commented 1 year ago

Nice, I was going to get to it tomorrow, but this is even better :)