From edfe2a84e9f74da30265b1de599a0506148e8b57 Mon Sep 17 00:00:00 2001 From: Alexander Kukushkin Date: Fri, 12 Nov 2021 15:02:53 +0100 Subject: [PATCH] Fix a few issues with Patroni API (#2116) 1. The `client_address` tuple may have more than two elements in case of IPv6 2. Return `cluster_unlocked` only when the value is true and handle it respectively in the do_GET_metrics() 3. Return `cluster_unlocked` and `dcs_last_seen` even if Postgres isn't running/queries timing out Close https://github.com/zalando/patroni/issues/2113 --- patroni/api.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/patroni/api.py b/patroni/api.py index c6adbb73..82fe1042 100644 --- a/patroni/api.py +++ b/patroni/api.py @@ -282,7 +282,7 @@ class RestApiHandler(BaseHTTPRequestHandler): metrics.append("# HELP patroni_cluster_unlocked Value is 1 if the cluster is unlocked, 0 if locked.") metrics.append("# TYPE patroni_cluster_unlocked gauge") - metrics.append("patroni_cluster_unlocked{0} {1}".format(scope_label, int(postgres['cluster_unlocked']))) + metrics.append("patroni_cluster_unlocked{0} {1}".format(scope_label, int(postgres.get('cluster_unlocked', 0)))) metrics.append("# HELP patroni_postgres_timeline Postgres timeline of this node (if running), 0 otherwise.") metrics.append("# TYPE patroni_postgres_timeline counter") @@ -604,8 +604,6 @@ class RestApiHandler(BaseHTTPRequestHandler): 'postmaster_start_time': row[0], 'role': 'replica' if row[1] == 0 else 'master', 'server_version': postgresql.server_version, - 'cluster_unlocked': bool(not cluster or cluster.is_unlocked()), - 'dcs_last_seen': self.server.patroni.dcs.last_seen, 'xlog': ({ 'received_location': row[4] or row[3], 'replayed_location': row[3], @@ -627,13 +625,17 @@ class RestApiHandler(BaseHTTPRequestHandler): if row[7]: result['replication'] = row[7] - return result except (psycopg2.Error, RetryFailedError, PostgresConnectionException): state = postgresql.state if state == 'running': logger.exception('get_postgresql_status') state = 'unknown' - return {'state': state, 'role': postgresql.role} + result = {'state': state, 'role': postgresql.role} + + if not cluster or cluster.is_unlocked(): + result['cluster_unlocked'] = True + result['dcs_last_seen'] = self.server.patroni.dcs.last_seen + return result def handle_one_request(self): self.__start_time = time.time() @@ -873,6 +875,6 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread): @staticmethod def handle_error(request, client_address): - address, port = client_address - logger.warning('Exception happened during processing of request from {}:{}'.format(address, port)) + logger.warning('Exception happened during processing of request from %s:%s', + client_address[0], client_address[1]) logger.warning(traceback.format_exc())