Skip to content

Commit

Permalink
Fix #1422: add BigQuery check to heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Dec 3, 2024
1 parent ba8b8ac commit 52c1de3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions telescope/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ async def lbheartbeat(request):
@routes.get("/__heartbeat__")
async def heartbeat(request):
checks = {}

# Check that `curl` has HTTP2 and HTTP3 for `checks.core.http_versions`
curl_cmd = subprocess.run(
[config.CURL_BINARY_PATH, "--version"],
Expand All @@ -228,8 +229,17 @@ async def heartbeat(request):
if not missing_features
else f"missing features {', '.join(missing_features)}"
)

# Bugzilla
bz_ping = await request.app["telescope.tracker"].ping()
checks["bugzilla"] = "ok" if bz_ping else "Bugzilla ping failed"

# Big Query
try:
checks["bigquery"] = (await utils.fetch_bigquery("SELECT 'ok';"))[0]
except Exception as exc:
checks["bigquery"] = str(exc)

status = 200 if all(v == "ok" for v in checks.values()) else 503
return web.json_response(checks, status=status)

Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from typing import List, Union
from unittest import mock

import pytest
import responses
Expand Down Expand Up @@ -58,6 +59,12 @@ def mock_aioresponses(cli):
yield m


@pytest.fixture
def mock_bigquery_client():
with mock.patch("telescope.utils.bigquery.Client") as mocked:
yield mocked


class ResponsesWrapper:
"""A tiny wrapper to mimic the aioresponses API."""

Expand Down
4 changes: 3 additions & 1 deletion tests/test_basic_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ async def test_lbheartbeat(cli):
assert response.status == 200


async def test_heartbeat(cli, config, mock_aioresponses):
async def test_heartbeat(cli, config, mock_aioresponses, mock_bigquery_client):
config.BUGTRACKER_URL = "http://bugzilla.local"
mock_aioresponses.get(
config.BUGTRACKER_URL + "/rest/whoami", payload={"name": "foo"}
)
mock_bigquery_client.return_value.query.side_effect = ValueError("bad credentials")

response = await cli.get("/__heartbeat__")
body = await response.json()

assert body["bugzilla"] == "ok"
assert body["bigquery"] == "bad credentials"
assert body["curl"] == "ok"
assert response.status == 200

Expand Down

0 comments on commit 52c1de3

Please sign in to comment.