forked from keephq/keep
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: One more attempt to return posthog (keephq#2387)
- Loading branch information
1 parent
520c842
commit e5500b2
Showing
5 changed files
with
93 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
import uuid | ||
import posthog | ||
import requests | ||
from posthog import Posthog | ||
from importlib import metadata | ||
|
||
try: | ||
KEEP_VERSION = metadata.version("keep") | ||
except metadata.PackageNotFoundError: | ||
try: | ||
KEEP_VERSION = metadata.version("keephq") | ||
except metadata.PackageNotFoundError: | ||
KEEP_VERSION = os.environ.get("KEEP_VERSION", "unknown") | ||
|
||
POSTHOG_DISABLED = os.getenv("POSTHOG_DISABLED", "false") == "true" | ||
RANDOM_TENANT_ID_PERSISTENT_WITHIN_LAUNCH = uuid.uuid4() | ||
|
||
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", | ||
) | ||
|
||
|
||
def is_posthog_reachable(): | ||
try: | ||
Posthog( | ||
api_key=POSTHOG_API_KEY, | ||
host="https://app.posthog.com", | ||
feature_flags_request_timeout_seconds=3, | ||
sync_mode=True # Explicitly to trigger exception if it's not reachable. | ||
).capture( | ||
RANDOM_TENANT_ID_PERSISTENT_WITHIN_LAUNCH, | ||
"connectivity_check", | ||
) | ||
return True | ||
except requests.exceptions.ConnectionError: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import asyncio | ||
import logging | ||
import threading | ||
from keep.api.core.posthog import ( | ||
posthog_client, | ||
is_posthog_reachable, | ||
KEEP_VERSION, | ||
POSTHOG_DISABLED, | ||
RANDOM_TENANT_ID_PERSISTENT_WITHIN_LAUNCH | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
UPTIME_REPORTING_CADENCE = 60 * 60 # 1 hour | ||
|
||
async def report_uptime_to_posthog(): | ||
""" | ||
Reports uptime and current version to PostHog every hour. | ||
Should be lunched in a separate thread. | ||
""" | ||
while True: | ||
posthog_client.capture( | ||
RANDOM_TENANT_ID_PERSISTENT_WITHIN_LAUNCH, | ||
"backend_status", | ||
properties={ | ||
"status": "up", | ||
"keep_version": KEEP_VERSION, | ||
}, | ||
) | ||
logger.info("Uptime reported to PostHog.") | ||
# Important to keep it async, otherwise will clog main gunicorn thread and cause timeouts. | ||
await asyncio.sleep(UPTIME_REPORTING_CADENCE) | ||
|
||
def launch_uptime_reporting(): | ||
""" | ||
Running async uptime reporting as a sub-thread. | ||
""" | ||
if not POSTHOG_DISABLED: | ||
if is_posthog_reachable(): | ||
thread = threading.Thread(target=asyncio.run, args=(report_uptime_to_posthog(), )) | ||
thread.start() | ||
logger.info("Uptime Reporting to Posthog launched.") | ||
else: | ||
logger.info("Reporting to Posthog not launched because it's not reachable.") | ||
else: | ||
logger.info("Posthog reporting is disabled so no uptime reporting.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters