From 73aa26590b6ea4c9624350576c8e7504ef097ca3 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Thu, 3 Nov 2016 10:48:37 +0100 Subject: [PATCH] Make the data gather singleton support multiple instances Due to we use pgbouncer, we could share the same instance of the metrics gatherer singleton in multiples requests. Added support to have multiple instances identified by transaction id --- .../cartodb_services/metrics/log.py | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/metrics/log.py b/server/lib/python/cartodb_services/cartodb_services/metrics/log.py index 51bdd7c..dd25ce1 100644 --- a/server/lib/python/cartodb_services/cartodb_services/metrics/log.py +++ b/server/lib/python/cartodb_services/cartodb_services/metrics/log.py @@ -1,9 +1,10 @@ -from datetime import datetime import abc import json import re import time import uuid +import plpy +from datetime import datetime from contextlib import contextmanager from urlparse import urlparse @@ -78,7 +79,9 @@ class MetricsDataGatherer: def clean(self): self.data = {} - __instance = None + + # We use pgbouncer so we need to have multiples instances per request id + __instance = {} @classmethod def add(self, key, value): @@ -98,10 +101,16 @@ class MetricsDataGatherer: @classmethod def instance(self): - if not MetricsDataGatherer.__instance: - MetricsDataGatherer.__instance = MetricsDataGatherer.__MetricsDataGatherer() + txid = MetricsDataGatherer._get_txid() + if txid not in MetricsDataGatherer.__instance: + MetricsDataGatherer.__instance[txid] = MetricsDataGatherer.__MetricsDataGatherer() - return MetricsDataGatherer.__instance + return MetricsDataGatherer.__instance[txid] + + @classmethod + def _get_txid(self): + result = plpy.execute('select txid_current() as txid') + return result[0]['txid'] class MetricsServiceLoggerFactory: @@ -168,7 +177,6 @@ class MetricsLogger(object): json.dump(r, log_file) log_file.write('\n') - @property def service_config(self): return self._service_config @@ -237,9 +245,7 @@ class MetricsIsolinesLogger(MetricsLogger): def collect_data(self, data): dump_data = super(MetricsIsolinesLogger, self).collect_data(data) - dump_data.update({ "isolines_generated": data.get('isolines_generated', 0) }) - return dump_data