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

fix: logging uptime for posthog reporting #3354

Merged
merged 2 commits into from
Feb 10, 2025
Merged
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
16 changes: 11 additions & 5 deletions keep/api/core/report_uptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
from keep.api.core.posthog import (
posthog_client,
is_posthog_reachable,
KEEP_VERSION,
POSTHOG_DISABLED,
KEEP_VERSION,
POSTHOG_DISABLED,
)

logger = logging.getLogger(__name__)
UPTIME_REPORTING_CADENCE = 60 * 60 # 1 hour

LAUNCH_TIME = datetime.now()


async def report_uptime_to_posthog():
"""
Reports uptime and current version to PostHog every hour.
Expand All @@ -32,7 +33,9 @@ async def report_uptime_to_posthog():
end_time = time.time()

properties["db_request_duration_ms"] = int((end_time - start_time) * 1000)
properties["uptime_hours"] = round(((datetime.now() - LAUNCH_TIME).total_seconds()) / 3600)
properties["uptime_hours"] = round(
((datetime.now() - LAUNCH_TIME).total_seconds()) / 3600
)

ee_enabled = os.environ.get("EE_ENABLED", "false").lower() == "true"
if ee_enabled:
Expand All @@ -44,17 +47,20 @@ async def report_uptime_to_posthog():
properties=properties,
)
posthog_client.flush()
logger.info("Uptime reported to PostHog.")
logger.info("Uptime reported to PostHog.", extra=properties)

await asyncio.sleep(UPTIME_REPORTING_CADENCE)


def launch_uptime_reporting_thread() -> threading.Thread | None:
"""
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 = threading.Thread(
target=asyncio.run, args=(report_uptime_to_posthog(),)
)
thread.start()
logger.info("Uptime Reporting to Posthog launched.")
return thread
Expand Down
Loading