Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend): pass secret_key to the PostHog client (#15142)

Reinier van der Leera18cd95be106
diff --git a/autogpt_platform/backend/.env.default b/autogpt_platform/backend/.env.default
index e3186321f8..acbcaa8be6 100644
--- a/autogpt_platform/backend/.env.default
+++ b/autogpt_platform/backend/.env.default
@@ -366,9 +366,10 @@ ZEROBOUNCE_API_KEY=
# Get API key from https://posthog.com - Project Settings > Project API Key
POSTHOG_API_KEY=
POSTHOG_HOST=https://eu.i.posthog.com
-# Personal API key, only for local feature-flag evaluation (Settings > Personal
-# API keys, scope: feature_flag:read). Without it flag reads go over the wire.
-POSTHOG_PERSONAL_API_KEY=
+# Feature Flags Secure API Key, only for local feature-flag evaluation (project
+# settings > Feature Flags). A personal API key also works, but PostHog is
+# deprecating that use. Without it flag reads go over the wire.
+POSTHOG_SECRET_KEY=
# Where flag definitions are shared between processes: redis (default; one
# elected refresher, every other process reads its copy), memory (process-local),
# or none (every process polls PostHog itself). PostHog bills one definitions
diff --git a/autogpt_platform/backend/backend/util/feature_flag/__init__.py b/autogpt_platform/backend/backend/util/feature_flag/__init__.py
index 28f41dc7b9..18936f41a8 100644
--- a/autogpt_platform/backend/backend/util/feature_flag/__init__.py
+++ b/autogpt_platform/backend/backend/util/feature_flag/__init__.py
@@ -252,10 +252,10 @@ def initialize_feature_flags() -> None:
if backend is not FeatureFlagBackend.POSTHOG:
initialize_launchdarkly()
if backend is not FeatureFlagBackend.LAUNCHDARKLY:
- if not settings.secrets.posthog_personal_api_key:
+ if not settings.secrets.posthog_secret_key:
logger.warning(
f"Feature flag backend is {backend.value} without "
- "POSTHOG_PERSONAL_API_KEY: every flag read becomes a remote "
+ "POSTHOG_SECRET_KEY: every flag read becomes a remote "
"/flags call instead of an in-process evaluation"
)
posthog.initialize_posthog_flags()
@@ -583,7 +583,7 @@ def _probe_posthog(
The shadow answer is never served, so awaiting it would only add PostHog's
latency to the request path — up to a 3s remote ``/flags`` call wherever
- ``POSTHOG_PERSONAL_API_KEY`` is unset and local evaluation is off.
+ ``POSTHOG_SECRET_KEY`` is unset and local evaluation is off.
"""
if _shadow_evaluations_stopped:
return
diff --git a/autogpt_platform/backend/backend/util/feature_flag/definition_cache_test.py b/autogpt_platform/backend/backend/util/feature_flag/definition_cache_test.py
index 3dcd7227b0..cc6381f3ff 100644
--- a/autogpt_platform/backend/backend/util/feature_flag/definition_cache_test.py
+++ b/autogpt_platform/backend/backend/util/feature_flag/definition_cache_test.py
@@ -366,9 +366,7 @@ class TestClientWiring:
mocker.patch.object(ph, "_client", None)
mocker.patch.object(ph, "_init_attempted", False)
mocker.patch.object(ph.settings.secrets, "posthog_api_key", "phc_test")
- mocker.patch.object(
- ph.settings.secrets, "posthog_personal_api_key", "phx_personal"
- )
+ mocker.patch.object(ph.settings.secrets, "posthog_secret_key", "phx_personal")
def test_the_client_shares_its_definitions_through_the_cache(self, mocker):
posthog = mocker.patch.object(ph, "Posthog")
@@ -381,8 +379,8 @@ class TestClientWiring:
)
assert kwargs["poll_interval"] == REFRESH
- def test_no_personal_key_means_no_poller_to_share(self, mocker):
- mocker.patch.object(ph.settings.secrets, "posthog_personal_api_key", "")
+ def test_no_secret_key_means_no_poller_to_share(self, mocker):
+ mocker.patch.object(ph.settings.secrets, "posthog_secret_key", "")
posthog = mocker.patch.object(ph, "Posthog")
ph.get_flag_client()
@@ -440,7 +438,7 @@ class TestAgainstTheRealSDK:
def client(self, provider_impl) -> Posthog:
return Posthog(
"phc_test",
- personal_api_key="phx_personal",
+ secret_key="phx_personal",
enable_local_evaluation=True,
sync_mode=True,
poll_interval=REFRESH,
diff --git a/autogpt_platform/backend/backend/util/feature_flag/posthog.py b/autogpt_platform/backend/backend/util/feature_flag/posthog.py
index 24625e21b4..0b830977c9 100644
--- a/autogpt_platform/backend/backend/util/feature_flag/posthog.py
+++ b/autogpt_platform/backend/backend/util/feature_flag/posthog.py
@@ -21,7 +21,7 @@ logger = logging.getLogger(__name__)
settings = Settings()
-# Without the personal key every read is a remote /flags call; on the default
+# Without the secret key every read is a remote /flags call; on the default
# executor a burst of those starves every other `asyncio.to_thread` caller.
FLAG_READ_WORKERS = 8
_read_executor = ThreadPoolExecutor(
@@ -124,7 +124,7 @@ def get_flag_client() -> Posthog | None:
"""The flag client singleton, or None when PostHog is unconfigured.
Separate from ``copilot.tracking``'s analytics client because only this
- one carries the personal API key that enables local evaluation, and
+ one carries the secret key that enables local evaluation, and
shutting one down must not silence the other.
"""
global _client, _init_attempted
@@ -144,22 +144,22 @@ def _build_client() -> Posthog | None:
logger.warning("PostHog API key not configured; flag reads will not resolve")
return None
- personal_api_key = settings.secrets.posthog_personal_api_key
+ secret_key = settings.secrets.posthog_secret_key
# One shared refresher, so the definitions bill stops scaling with replica
# count. Without a provider the SDK polls PostHog once per process.
- definition_cache = get_flag_definition_cache() if personal_api_key else None
+ definition_cache = get_flag_definition_cache() if secret_key else None
_client = Posthog(
settings.secrets.posthog_api_key,
host=settings.secrets.posthog_host,
- personal_api_key=personal_api_key or None,
- enable_local_evaluation=bool(personal_api_key),
+ secret_key=secret_key or None,
+ enable_local_evaluation=bool(secret_key),
poll_interval=refresh_interval_seconds(),
flag_definition_cache_provider=definition_cache,
)
_init_attempted = True
logger.info(
"PostHog feature flag client initialized "
- f"(local evaluation: {'on' if personal_api_key else 'off'}, "
+ f"(local evaluation: {'on' if secret_key else 'off'}, "
f"definition cache: {type(definition_cache).__name__ if definition_cache else 'off'})"
)
return _client
diff --git a/autogpt_platform/backend/backend/util/feature_flag/posthog_test.py b/autogpt_platform/backend/backend/util/feature_flag/posthog_test.py
index 73299c781a..051eece9bd 100644
--- a/autogpt_platform/backend/backend/util/feature_flag/posthog_test.py
+++ b/autogpt_platform/backend/backend/util/feature_flag/posthog_test.py
@@ -18,11 +18,9 @@ def fresh_client(mocker):
mocker.patch.object(ph, "_shut_down", False)
-def configure(mocker, *, api_key="phc_test", personal_api_key=""):
+def configure(mocker, *, api_key="phc_test", secret_key=""):
mocker.patch.object(ph.settings.secrets, "posthog_api_key", api_key)
- mocker.patch.object(
- ph.settings.secrets, "posthog_personal_api_key", personal_api_key
- )
+ mocker.patch.object(ph.settings.secrets, "posthog_secret_key", secret_key)
def snapshot(mocker, *, value, payload=None):
@@ -37,14 +35,14 @@ class TestClientConstruction:
configure(mocker, api_key="")
assert ph.get_flag_client() is None
- def test_the_personal_key_turns_on_local_evaluation(self, mocker):
- configure(mocker, personal_api_key="phx_personal")
+ def test_the_secret_key_turns_on_local_evaluation(self, mocker):
+ configure(mocker, secret_key="phx_personal")
posthog = mocker.patch.object(ph, "Posthog")
ph.get_flag_client()
_, kwargs = posthog.call_args
- assert kwargs["personal_api_key"] == "phx_personal"
+ assert kwargs["secret_key"] == "phx_personal"
assert kwargs["enable_local_evaluation"] is True
def test_without_it_evaluation_stays_remote(self, mocker):
@@ -54,7 +52,7 @@ class TestClientConstruction:
ph.get_flag_client()
_, kwargs = posthog.call_args
- assert kwargs["personal_api_key"] is None
+ assert kwargs["secret_key"] is None
assert kwargs["enable_local_evaluation"] is False
def test_the_client_is_built_once(self, mocker):
diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.py
index 5247276b66..593ba36db1 100644
--- a/autogpt_platform/backend/backend/util/settings.py
+++ b/autogpt_platform/backend/backend/util/settings.py
@@ -1255,10 +1255,12 @@ class Secrets(UpdateTrackingModel["Secrets"], BaseSettings):
posthog_host: str = Field(
default="https://eu.i.posthog.com", description="PostHog host URL"
)
- posthog_personal_api_key: str = Field(
+ posthog_secret_key: str = Field(
default="",
- description="PostHog personal API key. Only used for local feature-flag "
- "evaluation; without it flag reads fall back to a remote /flags call.",
+ description="PostHog Feature Flags Secure API Key (project settings > "
+ "Feature Flags), for local flag evaluation; without it flag reads fall back "
+ "to a remote /flags call. A personal API key also works, but PostHog is "
+ "deprecating that use.",
)
# Add more secret fields as needed
diff --git a/docs/platform/feature-flags.md b/docs/platform/feature-flags.md
index 3d48cf3d05..738cf9ef89 100644
--- a/docs/platform/feature-flags.md
+++ b/docs/platform/feature-flags.md
@@ -33,7 +33,7 @@ Redis.
| `POSTHOG_FLAG_DEFINITION_CACHE_TTL_SECONDS` | `600` | How long the shared copy stays readable after the last refresh. |
None of them is read unless `FEATURE_FLAG_BACKEND` is `posthog` or `dual` **and**
-`POSTHOG_PERSONAL_API_KEY` is set — without that key the SDK evaluates over the
+`POSTHOG_SECRET_KEY` is set — without that key the SDK evaluates over the
wire and has no definitions to share.
**Per environment.** Leave the defaults on `dev` and `prod`: both run several