From 131d7fad14575a6123f6ecca45e47d65f7370d8b Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 22 Feb 2016 10:17:16 +0100 Subject: [PATCH] setup changes --- bigchaindb/__init__.py | 10 ++++++++-- bigchaindb/block.py | 5 +++-- bigchaindb/commands/bigchain.py | 4 ++++ bigchaindb/core.py | 9 ++++----- bigchaindb/monitor.py | 20 ++++++++++++++++++++ 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/bigchaindb/__init__.py b/bigchaindb/__init__.py index aec03314..4bb76c44 100644 --- a/bigchaindb/__init__.py +++ b/bigchaindb/__init__.py @@ -1,7 +1,6 @@ import os import copy -from bigchaindb.core import Bigchain # noqa def e(key, default=None, conv=None): @@ -35,10 +34,17 @@ config = { 'private': e('BIGCHAIN_KEYPAIR_PRIVATE') }, 'keyring': [ - ] + ], + 'statsd': { + 'host': e('BIGCHAIN_STATSD_HOST', default='localhost'), + 'port': e('BIGCHAIN_STATSD_PORT', default=8125), + 'rate': e('BIGCHAIN_STATSD_SAMPLERATE', default=0.01) + } } # We need to maintain a backup copy of the original config dict in case # the user wants to reconfigure the node. Check ``bigchaindb.config_utils`` # for more info. _config = copy.deepcopy(config) +from bigchaindb.core import Bigchain # noqa + diff --git a/bigchaindb/block.py b/bigchaindb/block.py index 144ebef8..c4e7e17a 100644 --- a/bigchaindb/block.py +++ b/bigchaindb/block.py @@ -4,6 +4,7 @@ import queue import rethinkdb as r +import bigchaindb from bigchaindb import Bigchain from bigchaindb.monitor import Monitor @@ -13,7 +14,7 @@ logger = logging.getLogger(__name__) # obviously the hostname should come from an environment variable or setting # http://i.imgur.com/qciaOed.jpg -c = Monitor(host='statsd_1') +c = Monitor() class Block(object): @@ -38,7 +39,7 @@ class Block(object): b = Bigchain() while True: - c.gauge('tx_queue_gauge', self.q_tx_to_validate.qsize(), rate=0.01) + c.gauge('tx_queue_gauge', self.q_tx_to_validate.qsize(), rate=bigchaindb.config['statsd']['rate']) tx = self.q_new_transaction.get() # poison pill diff --git a/bigchaindb/commands/bigchain.py b/bigchaindb/commands/bigchain.py index 7ea95851..21d157e3 100644 --- a/bigchaindb/commands/bigchain.py +++ b/bigchaindb/commands/bigchain.py @@ -56,6 +56,10 @@ def run_configure(args, skip_if_exists=False): val = conf['database'][key] conf['database'][key] = input('Database {}? (default `{}`): '.format(key, val)) or val + for key in ('host', 'port', 'rate'): + val = conf['statsd'][key] + conf['statsd'][key] = input('Statsd {}? (default `{}`): '.format(key, val)) or val + bigchaindb.config_utils.write_config(conf, config_path) print('Ready to go!') diff --git a/bigchaindb/core.py b/bigchaindb/core.py index c5221c26..8a962a1b 100644 --- a/bigchaindb/core.py +++ b/bigchaindb/core.py @@ -12,9 +12,8 @@ from bigchaindb import exceptions from bigchaindb.crypto import hash_data, PublicKey, PrivateKey, generate_key_pair from bigchaindb.monitor import Monitor -# obviously the hostname should come from an environment variable or setting +c = Monitor() -c = Monitor(host='statsd_1') class GenesisBlockAlreadyExistsError(Exception): pass @@ -72,7 +71,7 @@ class Bigchain(object): def reconnect(self): return r.connect(host=self.host, port=self.port, db=self.dbname) - @c.timer('create_transaction', rate=0.01) + @c.timer('create_transaction', rate=bigchaindb.config['statsd']['rate']) def create_transaction(self, current_owner, new_owner, tx_input, operation, payload=None): """Create a new transaction @@ -182,7 +181,7 @@ class Bigchain(object): public_key = PublicKey(public_key_base58) return public_key.verify(self.serialize(data), signature) - @c.timer('write_transaction', rate=0.01) + @c.timer('write_transaction', rate=bigchaindb.config['statsd']['rate']) def write_transaction(self, signed_transaction): """Write the transaction to bigchain. @@ -319,7 +318,7 @@ class Bigchain(object): return owned - @c.timer('validate_transaction')#, rate=0.01) + @c.timer('validate_transaction', rate=bigchaindb.config['statsd']['rate']) def validate_transaction(self, transaction): """Validate a transaction. diff --git a/bigchaindb/monitor.py b/bigchaindb/monitor.py index d3a33472..be5c27cd 100644 --- a/bigchaindb/monitor.py +++ b/bigchaindb/monitor.py @@ -1,10 +1,30 @@ import statsd from platform import node +import bigchaindb +from bigchaindb import config_utils + class Monitor(statsd.StatsClient): + """Set up statsd monitoring + + """ def __init__(self, *args, **kwargs): + """Overrides statsd client, fixing prefix to messages and loading configuration + + Args: + *args: arguments (identical to Statsclient) + **kwargs: keyword arguments (identical to Statsclient) + """ + config_utils.autoconfigure() + if not kwargs: kwargs = {} + + # set prefix, parameters from configuration file if not kwargs.get('prefix'): kwargs['prefix'] = '{hostname}.'.format(hostname=node()) + if not kwargs.get('host'): + kwargs['host'] = bigchaindb.config['statsd']['host'] + if not kwargs.get('port'): + kwargs['port'] = bigchaindb.config['statsd']['port'] super().__init__(*args, **kwargs)