From 84aa5269e01e917085b29f4e7d558ed910514831 Mon Sep 17 00:00:00 2001 From: Matvey Kukuy Date: Mon, 6 Jan 2025 15:22:23 +0100 Subject: [PATCH] fix: posthog to None if disabled (#2989) --- keep/api/core/posthog.py | 16 +++++++++------- keep/cli/cli.py | 19 ++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/keep/api/core/posthog.py b/keep/api/core/posthog.py index 143157029..077b25726 100644 --- a/keep/api/core/posthog.py +++ b/keep/api/core/posthog.py @@ -16,17 +16,19 @@ POSTHOG_DISABLED = os.getenv("POSTHOG_DISABLED", "false").lower() == "true" -if POSTHOG_DISABLED: - posthog.disabled = True - POSTHOG_API_KEY = ( os.getenv("POSTHOG_API_KEY") or "phc_muk9qE3TfZsX3SZ9XxX52kCGJBclrjhkP9JxAQcm1PZ" # It's a public PH API key, not a leaked secret ;) ) -posthog_client = Posthog( - api_key=POSTHOG_API_KEY, - host="https://app.posthog.com", -) + +if POSTHOG_DISABLED: + posthog.disabled = True + posthog_client = None +else: + posthog_client = Posthog( + api_key=POSTHOG_API_KEY, + host="https://app.posthog.com", + ) def is_posthog_reachable(): diff --git a/keep/cli/cli.py b/keep/cli/cli.py index 9d12e96ac..81a29c777 100644 --- a/keep/cli/cli.py +++ b/keep/cli/cli.py @@ -187,14 +187,15 @@ def cli(ctx, info: Info, verbose: int, json: bool, keep_config: str): # https://posthog.com/tutorials/identifying-users-guide#identifying-and-setting-user-ids-for-every-other-library # random user id info.set_config(keep_config) - posthog_client.capture( - info.random_user_id, - "keep-cli-started", - properties={ - "args": sys.argv, - "keep_version": KEEP_VERSION, - }, - ) + if posthog_client is not None: + posthog_client.capture( + info.random_user_id, + "keep-cli-started", + properties={ + "args": sys.argv, + "keep_version": KEEP_VERSION, + }, + ) # Use the verbosity count to determine the logging level... if verbose > 0: # set the verbosity level to debug @@ -208,7 +209,7 @@ def cli(ctx, info: Info, verbose: int, json: bool, keep_config: str): @ctx.call_on_close def cleanup(): - if posthog_client: + if posthog_client is not None: posthog_client.flush()