Add query test script and test mode

* Add a mode to test all metric queries

* Add a script to run query tests against different versions of
  PostgeSQL

* Add Docker elements for query testing

* Switch to using a --config flag when specifying the config file

* Fix some metric queries

* Allow the agent address to be configured

* Allow the sslmode connection parameter to be configured
This commit is contained in:
2025-05-22 14:53:25 -04:00
parent 529bef9679
commit c0e1531083
11 changed files with 174 additions and 108 deletions

View File

@@ -85,6 +85,8 @@ class MetricVersionError(Exception):
# Default config settings
default_config = {
# The address the agent binds to
"address": "127.0.0.1",
# The port the agent listens on for requests
"port": 5400,
# Min PostgreSQL connection pool size (per database)
@@ -103,6 +105,8 @@ default_config = {
"dbport": 5432,
# Default database to connect to when none is specified for a metric
"dbname": "postgres",
# SSL connection mode
"ssl_mode": "require",
# Timeout for getting a connection slot from a pool
"pool_slot_timeout": 5,
# PostgreSQL connection timeout (seconds)
@@ -325,6 +329,7 @@ def get_pool(dbname):
# lock
if dbname not in connections:
log.info("Creating connection pool for: {}".format(dbname))
# Actually create the connection pool
connections[dbname] = ConnectionPool(
dbname,
int(config["min_pool_size"]),
@@ -334,7 +339,7 @@ def get_pool(dbname):
port=config["dbport"],
user=config["dbuser"],
connect_timeout=int(config["connect_timeout"]),
sslmode="require",
sslmode=config["ssl_mode"],
)
# Clear the unhappy indicator if present
unhappy_cooldown.pop(dbname, None)
@@ -382,10 +387,16 @@ def run_query_no_retry(pool, return_type, query, args):
res = curs.fetchall()
if return_type == "value":
if len(res) == 0:
return ""
return str(list(res[0].values())[0])
elif return_type == "row":
if len(res) == 0:
return "[]"
return json.dumps(res[0])
elif return_type == "column":
if len(res) == 0:
return "[]"
return json.dumps([list(r.values())[0] for r in res])
elif return_type == "set":
return json.dumps(res)
@@ -393,7 +404,7 @@ def run_query_no_retry(pool, return_type, query, args):
dbname = pool.name
if dbname in unhappy_cooldown:
raise UnhappyDBError()
elif conn.broken:
elif conn.closed != 0:
raise DisconnectedError()
else:
raise
@@ -505,15 +516,14 @@ def test_queries():
# We just use the default db for tests
dbname = config["dbname"]
# Loop through all defined metrics.
for metric_name in config["metrics"].keys():
# Get the actual metric definition
metric = metrics[metric_name]
for name, metric in config["metrics"].items():
# If the metric has arguments to use while testing, grab those
args = metric.get("test_args", {})
# Run the query without the ability to retry.
res = sample_metric(dbname, metric_name, args, retry=False)
res = sample_metric(dbname, name, args, retry=False)
# Compare the result to the provided sample results
# TODO
print("{} -> {}".format(name, res))
# Return the number of errors
# TODO
return 0
@@ -603,13 +613,16 @@ if __name__ == "__main__":
)
parser.add_argument(
"config_file",
"-c",
"--config_file",
default="pgmon.yml",
nargs="?",
help="The config file to read (default: %(default)s)",
)
parser.add_argument("test", action="store_true", help="Run query tests and exit")
parser.add_argument(
"-t", "--test", action="store_true", help="Run query tests and exit"
)
args = parser.parse_args()
@@ -628,7 +641,7 @@ if __name__ == "__main__":
sys.exit(0)
# Set up the http server to receive requests
server_address = ("127.0.0.1", config["port"])
server_address = (config["address"], config["port"])
httpd = ThreadingHTTPServer(server_address, SimpleHTTPRequestHandler)
# Set up the signal handler