From 0c82ff3d989e592f9c6ea848e2d3538c42feac7a Mon Sep 17 00:00:00 2001 From: Alexander Bayandin Date: Fri, 14 Apr 2023 11:46:47 +0100 Subject: [PATCH] test_runner: add Timeline Inspector to Grafana links (#4021) --- test_runner/fixtures/neon_fixtures.py | 17 ++++++++++++--- test_runner/fixtures/utils.py | 30 ++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/test_runner/fixtures/neon_fixtures.py b/test_runner/fixtures/neon_fixtures.py index fb12752d3c..c6610ba062 100644 --- a/test_runner/fixtures/neon_fixtures.py +++ b/test_runner/fixtures/neon_fixtures.py @@ -1913,15 +1913,26 @@ def remote_pg( connstr = os.getenv("BENCHMARK_CONNSTR") if connstr is None: raise ValueError("no connstr provided, use BENCHMARK_CONNSTR environment variable") + + host = parse_dsn(connstr).get("host", "") + is_neon = host.endswith(".neon.build") + start_ms = int(datetime.utcnow().timestamp() * 1000) with RemotePostgres(pg_bin, connstr) as remote_pg: + if is_neon: + timeline_id = TimelineId(remote_pg.safe_psql("SHOW neon.timeline_id")[0][0]) + yield remote_pg end_ms = int(datetime.utcnow().timestamp() * 1000) - host = parse_dsn(connstr).get("host", "") - if host.endswith(".neon.build"): + if is_neon: # Add 10s margin to the start and end times - allure_add_grafana_links(host, start_ms - 10_000, end_ms + 10_000) + allure_add_grafana_links( + host, + timeline_id, + start_ms - 10_000, + end_ms + 10_000, + ) class PSQL: diff --git a/test_runner/fixtures/utils.py b/test_runner/fixtures/utils.py index 71df74dfba..30acd3f637 100644 --- a/test_runner/fixtures/utils.py +++ b/test_runner/fixtures/utils.py @@ -13,6 +13,7 @@ import allure from psycopg2.extensions import cursor from fixtures.log_helper import log +from fixtures.types import TimelineId Fn = TypeVar("Fn", bound=Callable[..., Any]) @@ -186,11 +187,15 @@ def allure_attach_from_dir(dir: Path): allure.attach.file(source, name, attachment_type, extension) -DATASOURCE_ID = "xHHYY0dVz" +GRAFANA_URL = "https://neonprod.grafana.net" +GRAFANA_EXPLORE_URL = f"{GRAFANA_URL}/explore" +GRAFANA_TIMELINE_INSPECTOR_DASHBOARD_URL = f"{GRAFANA_URL}/d/8G011dlnk/timeline-inspector" +LOGS_STAGING_DATASOURCE_ID = "xHHYY0dVz" -def allure_add_grafana_links(host: str, start_ms: int, end_ms: int): +def allure_add_grafana_links(host: str, timeline_id: TimelineId, start_ms: int, end_ms: int): """Add links to server logs in Grafana to Allure report""" + links = {} # We expect host to be in format like ep-divine-night-159320.us-east-2.aws.neon.build endpoint_id, region_id, _ = host.split(".", 2) @@ -202,12 +207,12 @@ def allure_add_grafana_links(host: str, start_ms: int, end_ms: int): } params: Dict[str, Any] = { - "datasource": DATASOURCE_ID, + "datasource": LOGS_STAGING_DATASOURCE_ID, "queries": [ { "expr": "", "refId": "A", - "datasource": {"type": "loki", "uid": DATASOURCE_ID}, + "datasource": {"type": "loki", "uid": LOGS_STAGING_DATASOURCE_ID}, "editorMode": "code", "queryType": "range", } @@ -220,8 +225,23 @@ def allure_add_grafana_links(host: str, start_ms: int, end_ms: int): for name, expr in expressions.items(): params["queries"][0]["expr"] = expr query_string = urlencode({"orgId": 1, "left": json.dumps(params)}) - link = f"https://neonprod.grafana.net/explore?{query_string}" + links[name] = f"{GRAFANA_EXPLORE_URL}?{query_string}" + timeline_qs = urlencode( + { + "orgId": 1, + "var-environment": "victoria-metrics-aws-dev", + "var-timeline_id": timeline_id, + "var-endpoint_id": endpoint_id, + "var-log_datasource": "grafanacloud-neonstaging-logs", + "from": start_ms, + "to": end_ms, + } + ) + link = f"{GRAFANA_TIMELINE_INSPECTOR_DASHBOARD_URL}?{timeline_qs}" + links["Timeline Inspector"] = link + + for name, link in links.items(): allure.dynamic.link(link, name=name) log.info(f"{name}: {link}")