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

Add posthog local eval support #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion feature_gate/adapters/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def features(self):
self._logger.info("Lists features {features}")
return features

def is_enabled(self, feature_key):
def is_enabled(self, feature_key, **kwargs):
for feature in self._features:
if feature["key"] == feature_key:
is_enabled = feature["gates"]["boolean"]["enabled"]
Expand Down
4 changes: 2 additions & 2 deletions feature_gate/adapters/posthog.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def features(self):
key='key'
return [item[key] for item in resp["data"] if key in item]

def is_enabled(self, feature_key):
return self.client.is_enabled(feature_key)
def is_enabled(self, feature_key, **kwargs):
return self.client.is_enabled(feature_key, **kwargs)

def enable(self, feature_key):
resp = self.client.enable_feature(feature_key)
Expand Down
4 changes: 2 additions & 2 deletions feature_gate/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def remove(self, feature):
def features(self):
return self.adapter.features()

def is_enabled(self, feature):
return self.adapter.is_enabled(feature)
def is_enabled(self, feature, **kwargs):
return self.adapter.is_enabled(feature, **kwargs)

def enable(self, feature):
return self.adapter.enable(feature)
Expand Down
41 changes: 33 additions & 8 deletions feature_gate/clients/posthog_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import requests
import structlog
from posthog import Posthog

from pathlib import Path
from feature_gate.client import FeatureNotFound
Expand All @@ -16,7 +17,7 @@ class PosthogAPIClientError(Exception):
pass

class PosthogAPIClient:
def __init__(self, api_base=None, api_key=None, project_id=None):
def __init__(self, api_base=None, api_key=None, project_id=None, poll_interval=30):
if api_base is None:
self.api_base = os.environ.get("POSTHOG_API_BASE", "https://app.posthog.com")
else:
Expand All @@ -31,6 +32,12 @@ def __init__(self, api_base=None, api_key=None, project_id=None):
self.project_id = os.environ.get('POSTHOG_PROJECT_ID')
else:
self.project_id = project_id

self.posthog_client = Posthog(project_id,
host=self.api_base,
poll_interval=poll_interval, # local eval refresh interval
personal_api_key=api_key
)

bind_contextvars(klass="PosthogAPIClient", project_id=project_id)
project_root = os.path.abspath(os.getenv('PROJECT_ROOT', '.'))
Expand Down Expand Up @@ -58,6 +65,31 @@ def api_key(self):
def project_id(self):
return self.project_id


def is_enabled(self, key, user_id=None, person_properties=None, only_evaluate_locally=False, **kwargs):
"""
This uses posthog local evaluation to check if a feature flag is enabled
for a user without making a server request.
See local eval docs here: https://posthog.com/docs/feature-flags/local-evaluation

Args:
name (String): the name/key of the feature flag
user (User): the user object

Returns:
bool: true or false
"""
return self.posthog_client.get_feature_flag(
key,
user_id,
# Include any person properties, groups, or group properties required to evaluate the flag
person_properties=person_properties,
# Optional. Defaults to False. Set to True if you don't want PostHog to make a server request if it can't evaluate locally
only_evaluate_locally=only_evaluate_locally,
**kwargs
)


def list_features(self):
path = f'/api/projects/{self.project_id}/feature_flags'
with bound_contextvars(method="list_features"):
Expand Down Expand Up @@ -96,13 +128,6 @@ def delete_feature(self, key):
response = self._patch(path, payload)
return self._map_single_response("PATCH", path, response)

def is_enabled(self, key):
feature = self.fetch_feature(key)
if feature == None:
raise FeatureNotFound(f"Feature {key} not found")
else:
return feature["active"]

def enable_feature(self, key):
feature = self.fetch_feature(key)
if feature == None:
Expand Down
72 changes: 71 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ keywords = ["feature flags", "feature gate"]
python = ">= 3.10"
structlog = "^24.1.0"
requests = "^2.31.0"
posthog = "^3.5.0"

[tool.poetry.group.test.dependencies]
pytest = "^8.0.0"
Expand Down