def test_count(http_client2: TSDBClient): http_client2.is_connected() http_client2.TEST_METRIC = Counter('test.metric.counter') metric = http_client2.TEST_METRIC.inc() assert metric['value'] == 1 metric = http_client2.TEST_METRIC.inc() assert metric['value'] == 2
def test_gauge(http_client2: TSDBClient): http_client2.is_connected() http_client2.TEST_METRIC = Gauge('test.metric.gauge') metric = http_client2.TEST_METRIC.inc() assert metric['value'] == 1 metric = http_client2.TEST_METRIC.set(10) assert metric['value'] == 10 metric = http_client2.TEST_METRIC.dec() assert metric['value'] == 9
class OpenTSDB(BaseDB): """ OpenTSDB class herited from BaseDB Allow to handle an OpenTSDB database to save PowerReport. """ def __init__(self, report_type: Type[Report], host: str, port, metric_name: str): """ :param host: host of the OpenTSDB server :param port: port of the OpenTSDB server :param metric_name: mectric name to store :param report_type: type of report handled by this database """ BaseDB.__init__(self, report_type) self.host = host self.port = port self.metric_name = metric_name self.client = None def __iter__(self): raise NotImplementedError() def connect(self): """ Override from BaseDB. Create the connection to the openTSDB database with the current configuration (hostname/port), then check if the connection has been created without failure. """ # close connection if reload if self.client is not None: self.client.close() self.client.wait() self.client = TSDBClient(host=self.host, port=self.port) if not self.client.is_connected() and not self.client.is_alive(): raise CantConnectToOpenTSDBException('connexion error') def save(self, report: PowerReport): """ Override from BaseDB :param report: Report to save """ self.client.send(self.metric_name, report.power, timestamp=int(report.timestamp.timestamp()), host=report.target) def save_many(self, reports: List[Report]): """ Save a batch of data :param reports: Batch of data. """ for report in reports: self.save(report)
def test_count_with_tag(http_client2: TSDBClient): http_client2.is_connected() http_client2.TEST_METRIC2 = Counter('test.metric2.counter', ['tag1']) metric = http_client2.TEST_METRIC2.tags('tag1').inc() assert 'tag1' in metric['tags']