def update(metric: dict) -> dict: db = mongodb.connect() current_time = int(time.time()) saved_metric = db.mtrc.metrics.find_one({ 'type': 'counter', 'name': metric['name'], 'labels': metric['labels'], 'average': metric['average'], }) if not saved_metric: db.mtrc.metrics.insert_one({ 'type': 'counter', 'name': metric['name'], 'labels': metric['labels'], 'average': int(metric['average']), 'description': metric['description'], 'value': float(metric['value']), 'date': current_time, }) return True db.mtrc.metrics.update_one({'_id': saved_metric['_id']}, { '$set': {'date': current_time}, '$inc': {'value': float(metric['value'])}, }) return True
def update(metric: dict) -> dict: db = mongodb.connect() current_time = int(time.time()) saved_metric = db.mtrc.metrics.find_one({ 'type': 'gauge', 'name': metric['name'], 'labels': metric['labels'], }) document = { 'type': 'gauge', 'name': metric['name'], 'labels': metric['labels'], 'value': float(metric['value']), 'date': current_time, } if 'description' in metric: document['description'] = metric['description'] if not saved_metric: db.mtrc.metrics.insert_one(document) return True db.mtrc.metrics.update_one({'_id': saved_metric['_id']}, {'$set': { 'date': current_time, 'value': float(metric['value']), }}) return True
def update(metric: dict) -> bool: db = mongodb.connect() current_time = int(time.time()) saved_metric = db.mtrc.metrics.find_one({ 'type': 'average', 'name': metric['name'], 'labels': metric['labels'], 'average': metric['average'], }) if not saved_metric: db.mtrc.metrics.insert_one({ 'type': 'average', 'name': metric['name'], 'labels': metric['labels'], 'average': int(metric['average']), 'description': metric['description'], 'values': [{ 'date': int(metric['date']), 'value': float(metric['value']), }], 'value': float(metric['value']), 'date': current_time, }) return True # update range saved_metric['values'].append({ 'value': metric['value'], 'date': metric['date'], }) # remove old # filter range by average timeshist new_values = [] for i in range(len(saved_metric['values'])): if saved_metric['values'][i]['date'] + saved_metric['average'] > current_time: new_values.append(saved_metric['values'][i]) saved_metric['values'] = new_values # count average value and actual range saved_metric['value'] = sum([ v['value'] for v in saved_metric['values'] ]) / len(saved_metric['values']) # touch date saved_metric['date'] = current_time # store new data db.mtrc.metrics.update_one({'_id': saved_metric['_id']}, { '$set': saved_metric }) return True
def get_metrics() -> str: '''get metrics for prometheus''' db = mongodb.connect() result = '' metrics = [metric for metric in db.mtrc.metrics.find()] for metric in metrics: if 'description' in metric: result += f"# HELP {metric['name']} {metric['description']}\n" result += f"# TYPE {metric['name']} {metric['type']}\n" result += f"{metric['name']}_{metric['type']}{get_labels_string(metric['labels'])} {metric['value']}\n" return result
def remove(metric_name: str) -> bool: db = mongodb.connect() db.mtrc.metrics.remove({'type': 'average', 'name': metric['name']}) return True
def drop_all_metrics() -> bool: '''drop all metrics''' db = mongodb.connect() return db.drop_database('mtrc')