2019-12-14 16:40:50 +00:00
|
|
|
import re
|
2020-01-24 09:16:34 +00:00
|
|
|
import time
|
2019-12-14 16:40:50 +00:00
|
|
|
|
2020-09-16 04:26:10 +00:00
|
|
|
import pytest
|
|
|
|
import requests
|
2024-09-27 10:19:39 +00:00
|
|
|
|
2019-12-14 16:40:50 +00:00
|
|
|
from helpers.cluster import ClickHouseCluster
|
|
|
|
|
|
|
|
cluster = ClickHouseCluster(__file__)
|
2022-03-22 16:39:58 +00:00
|
|
|
node = cluster.add_instance("node", main_configs=["configs/prom_conf.xml"])
|
2019-12-14 16:40:50 +00:00
|
|
|
|
2020-09-16 04:26:10 +00:00
|
|
|
|
2019-12-14 16:40:50 +00:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def start_cluster():
|
|
|
|
try:
|
|
|
|
cluster.start()
|
|
|
|
yield cluster
|
|
|
|
finally:
|
|
|
|
cluster.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
def parse_response_line(line):
|
|
|
|
allowed_prefixes = [
|
|
|
|
"ClickHouse",
|
|
|
|
"# HELP",
|
|
|
|
"# TYPE",
|
|
|
|
]
|
2020-01-24 09:16:34 +00:00
|
|
|
assert any(line.startswith(prefix) for prefix in allowed_prefixes)
|
2019-12-14 16:40:50 +00:00
|
|
|
|
|
|
|
if line.startswith("#"):
|
|
|
|
return {}
|
2024-06-05 07:50:39 +00:00
|
|
|
match = re.match(r"^([a-zA-Z_:][a-zA-Z0-9_:]+)(\{.*\})? -?(\d)", line)
|
2019-12-14 16:40:50 +00:00
|
|
|
assert match, line
|
|
|
|
name, _, val = match.groups()
|
|
|
|
return {name: int(val)}
|
|
|
|
|
|
|
|
|
2020-01-24 09:16:34 +00:00
|
|
|
def get_and_check_metrics(retries):
|
|
|
|
while True:
|
|
|
|
try:
|
2022-03-22 16:39:58 +00:00
|
|
|
response = requests.get(
|
|
|
|
"http://{host}:{port}/metrics".format(host=node.ip_address, port=8001),
|
|
|
|
allow_redirects=False,
|
2024-01-19 10:17:28 +00:00
|
|
|
# less then default keep-alive timeout (10 seconds)
|
|
|
|
timeout=5,
|
2022-03-22 16:39:58 +00:00
|
|
|
)
|
2019-12-14 16:40:50 +00:00
|
|
|
|
2020-01-24 09:16:34 +00:00
|
|
|
if response.status_code != 200:
|
|
|
|
response.raise_for_status()
|
|
|
|
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
if retries >= 0:
|
|
|
|
retries -= 1
|
|
|
|
time.sleep(0.5)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
raise
|
2019-12-14 16:40:50 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
assert response.headers["content-type"].startswith("text/plain")
|
2019-12-14 16:40:50 +00:00
|
|
|
|
|
|
|
results = {}
|
2022-03-22 16:39:58 +00:00
|
|
|
for resp_line in response.text.split("\n"):
|
2019-12-14 16:40:50 +00:00
|
|
|
resp_line = resp_line.rstrip()
|
|
|
|
if not resp_line:
|
|
|
|
continue
|
|
|
|
res = parse_response_line(resp_line)
|
|
|
|
results.update(res)
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
def test_prometheus_endpoint(start_cluster):
|
2020-01-24 09:16:34 +00:00
|
|
|
metrics_dict = get_and_check_metrics(10)
|
2022-03-22 16:39:58 +00:00
|
|
|
assert metrics_dict["ClickHouseProfileEvents_Query"] >= 0
|
|
|
|
prev_query_count = metrics_dict["ClickHouseProfileEvents_Query"]
|
2019-12-14 16:40:50 +00:00
|
|
|
|
2020-01-24 09:16:34 +00:00
|
|
|
node.query("SELECT 1")
|
|
|
|
node.query("SELECT 2")
|
|
|
|
node.query("SELECT 3")
|
2019-12-14 16:40:50 +00:00
|
|
|
|
2020-01-24 09:16:34 +00:00
|
|
|
metrics_dict = get_and_check_metrics(10)
|
2022-03-22 16:39:58 +00:00
|
|
|
assert metrics_dict["ClickHouseProfileEvents_Query"] >= prev_query_count + 3
|
2023-12-07 19:09:44 +00:00
|
|
|
|
2023-12-07 19:42:33 +00:00
|
|
|
node.query_and_get_error(
|
|
|
|
"SELECT throwIf(1, 'test', toInt16(42)) SETTINGS allow_custom_error_code_in_throwif=1"
|
|
|
|
)
|
2023-12-07 19:09:44 +00:00
|
|
|
metrics_dict = get_and_check_metrics(10)
|
|
|
|
|
|
|
|
assert metrics_dict["ClickHouseErrorMetric_NUMBER_OF_ARGUMENTS_DOESNT_MATCH"] >= 1
|
|
|
|
assert metrics_dict["ClickHouseErrorMetric_ALL"] >= 1
|