Commit ae31af1f authored by 胡凯旋's avatar 胡凯旋

fix metric family

parent 88320f5b
...@@ -75,7 +75,7 @@ class TencentExporter(Exporter): ...@@ -75,7 +75,7 @@ class TencentExporter(Exporter):
with_factor_metric_family.add_metric( with_factor_metric_family.add_metric(
['{}*{}'.format(instance_name,rds_factor), instance_type, service_provider], ['{}*{}'.format(instance_name,rds_factor), instance_type, service_provider],
value*rds_factor, timestamp) value*rds_factor, timestamp)
metrics_to_export.extend([avg_metric_family, with_factor_metricname]) metrics_to_export.extend([avg_metric_family, with_factor_metric_family])
return metrics_to_export return metrics_to_export
...@@ -139,7 +139,6 @@ class AliyunExporter(Exporter): ...@@ -139,7 +139,6 @@ class AliyunExporter(Exporter):
metrics_to_export.extend([avg_metric_family, max_metric_family, with_factor_metric_family]) metrics_to_export.extend([avg_metric_family, max_metric_family, with_factor_metric_family])
print(metrics_to_export)
return metrics_to_export return metrics_to_export
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -6,76 +6,21 @@ import math ...@@ -6,76 +6,21 @@ import math
import yaml import yaml
from prometheus_client import start_http_server from prometheus_client import start_http_server
from prometheus_client.core import GaugeMetricFamily, REGISTRY from prometheus_client.core import REGISTRY
from aliyun import CMS from exporter import TencentExporter, AliyunExporter
aliyun_cms = CMS()
with open('config.yaml', 'r') as f:
config = yaml.load(f)
# rds_instances = settings.RDS_INSTANCES
rds_instances = config['rds_instances']
queryname_metricname = config['metrics']
class RDSCollector(object): class RDSCollector(object):
def __init__(self):
self.exporter_list = [
TencentExporter(),
# AliyunExporter(),
]
def collect(self): def collect(self):
metrics = [] metrics = []
# 阿里云SDK查询字段与prometheus metric name的映射 for exporter in self.exporter_list:
# queryname_metricname = { metrics.extend(exporter.export_prometheus_data())
# 'CpuUsage': 'rds_cpu_usage_ratio',
# # 'DiskUsage': 'rds_disk_usage_ratio',
# 'MemoryUsage': 'rds_memory_usage_radio',
# 'ConnectionUsage': 'rds_connection_usage_radio',
# 'IOPSUsage': 'rds_iops_usage_radio',
# }
metrics_from_aliyun = {}
# all_rds_id = list(rds_instances.keys())
all_rds_id = rds_instances.keys()
# get metrics from aliyun
for label in queryname_metricname.keys():
metrics_from_aliyun[label] = []
for i in range(0, int(math.ceil(len(all_rds_id)/10.0))):
instance_id_list = all_rds_id[10*i : 10*(i+1)]
try:
res = aliyun_cms.get_rds_metric(instance_id_list, label)
metrics_from_aliyun[label].extend(res['Datapoints'])
except Exception as e:
print(e)
# transform to prometheus metric format
for queryname, datapoints in metrics_from_aliyun.items():
metric = queryname_metricname[queryname]
metricname = metric['name']
factor = metric.get('factor', None)
avg_metric_family = GaugeMetricFamily(metricname, metricname,
labels=['rds_name', 'type'])
max_metricname = '{}_max'.format(metricname)
max_metric_family = GaugeMetricFamily(max_metricname,
max_metricname, labels=['rds_name', 'type'])
with_factor_metricname = '{}_with_factor'.format(metricname)
with_factor_metric_family = GaugeMetricFamily(with_factor_metricname,
with_factor_metricname, labels=['rds_name', 'type'])
for point in datapoints:
# rds_name = rds_instances[point['instanceId']]
rds = rds_instances[point['instanceId']]
rds_name = rds['name']
rds_type = rds.get('type', 'master')
# 当前queryname,当前rds实例的因子,若没有配置,默认为1
rds_factor = rds.get(factor, 1)
avg_metric_family.add_metric([rds_name, rds_type],
point['Average']/100.0, point['timestamp']/1000)
max_metric_family.add_metric([rds_name, rds_type],
point['Maximum']/100.0, point['timestamp']/1000)
with_factor_metric_family.add_metric(['{}*{}'.format(rds_name,rds_factor), rds_type],
point['Average']*rds_factor/100.0, point['timestamp']/1000)
metrics.extend([avg_metric_family, max_metric_family,
with_factor_metric_family])
for m in metrics: for m in metrics:
yield m yield m
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment