Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(health): report the deployed commit in /_livez (#112037)

Georges-Antoine Assidb1a0c4ba2a1
diff --git a/posthog/git.py b/posthog/git.py
index d418c3ab7dad..f6107ebe79d0 100644
--- a/posthog/git.py
+++ b/posthog/git.py
@@ -32,6 +32,16 @@ def get_git_commit_short() -> Optional[str]:
return None
+@cache
+def get_git_commit_full() -> Optional[str]:
+ if _git_commit_baked_in:
+ return _git_commit_baked_in
+ try:
+ return subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("utf-8").strip()
+ except Exception:
+ return None
+
+
@cache
def get_git_branch() -> Optional[str]:
"""Returns the symbolic name of the current active branch. Will return None in case of failure.
diff --git a/posthog/health.py b/posthog/health.py
index 1c741b709522..84971b9a7a19 100644
--- a/posthog/health.py
+++ b/posthog/health.py
@@ -33,6 +33,7 @@ from structlog import get_logger
from posthog.celery import app
from posthog.database_healthcheck import DATABASE_FOR_FLAG_MATCHING
+from posthog.git import get_git_commit_full
from posthog.kafka_client.profiles import KafkaClusterProfile
from posthog.kafka_client.routing import get_producer
from posthog.security.outbound_proxy import internal_requests
@@ -95,7 +96,7 @@ def livez(request: HttpRequest):
than readyz but we can hit this harder such that we can take obviously
broken pods out asap.
"""
- return JsonResponse({"http": True})
+ return JsonResponse({"http": True, "commit": get_git_commit_full()})
def is_shutting_down() -> bool:
diff --git a/posthog/test/test_git.py b/posthog/test/test_git.py
index 6ebf41890a29..16a0e1699f96 100644
--- a/posthog/test/test_git.py
+++ b/posthog/test/test_git.py
@@ -1,6 +1,11 @@
+import subprocess
+
+import pytest
+from unittest.mock import patch
+
from parameterized import parameterized
-from posthog.git import extract_explicit_repo, extract_linked_repo, extract_repo_from_scopes
+from posthog.git import extract_explicit_repo, extract_linked_repo, extract_repo_from_scopes, get_git_commit_full
REPOS = ["posthog/posthog", "posthog/posthog-js", "posthog/posthog.com"]
@@ -121,3 +126,37 @@ class TestExtractRepoFromScopes:
)
def test_first_scope_to_name_a_repo_answers(self, _name: str, scopes: list[str], expected: str | None):
assert extract_repo_from_scopes(scopes, REPOS) == expected
+
+
+FULL_SHA = "0123456789abcdef0123456789abcdef01234567"
+
+
+class TestGetGitCommitFull:
+ @pytest.fixture(autouse=True)
+ def clear_cache(self):
+ get_git_commit_full.cache_clear()
+ yield
+ get_git_commit_full.cache_clear()
+
+ def test_returns_the_full_baked_in_commit(self):
+ with (
+ patch("posthog.git._git_commit_baked_in", FULL_SHA),
+ patch("posthog.git.subprocess.check_output") as check_output,
+ ):
+ assert get_git_commit_full() == FULL_SHA
+ check_output.assert_not_called()
+
+ def test_falls_back_to_git_when_nothing_is_baked_in(self):
+ with (
+ patch("posthog.git._git_commit_baked_in", None),
+ patch("posthog.git.subprocess.check_output", return_value=f"{FULL_SHA}\n".encode()) as check_output,
+ ):
+ assert get_git_commit_full() == FULL_SHA
+ check_output.assert_called_once_with(["git", "rev-parse", "HEAD"])
+
+ def test_returns_none_when_the_commit_is_unknown(self):
+ with (
+ patch("posthog.git._git_commit_baked_in", None),
+ patch("posthog.git.subprocess.check_output", side_effect=subprocess.CalledProcessError(128, "git")),
+ ):
+ assert get_git_commit_full() is None
diff --git a/posthog/test/test_health.py b/posthog/test/test_health.py
index 7a53b6262029..858dc532a001 100644
--- a/posthog/test/test_health.py
+++ b/posthog/test/test_health.py
@@ -78,12 +78,21 @@ def test_livez_returns_200_and_doesnt_require_any_dependencies(client: Client):
simulate_clickhouse_cannot_connect(),
simulate_celery_cannot_connect(),
simulate_cache_cannot_connect(),
+ patch("posthog.health.get_git_commit_full", return_value="a" * 40),
):
resp = get_livez(client)
assert resp.status_code == 200, resp.content
data = resp.json()
- assert data == {"http": True}
+ assert data == {"http": True, "commit": "a" * 40}
+
+
+def test_livez_returns_null_commit_when_unknown(client: Client):
+ with patch("posthog.health.get_git_commit_full", return_value=None):
+ resp = get_livez(client)
+
+ assert resp.status_code == 200, resp.content
+ assert resp.json() == {"http": True, "commit": None}
# Role based tests