gnocchi --debug measures add -m 2016-04-16T14:33:[email protected] --resource-id 3068f5ec-0d37-419a-929d-0505efaf908c vm_meteric_001
源代码位于: gnocchi/rest/__init__.py中NamedMetricController:
在lookup接口中,根据传入的meter的name以及resource,在索引数据库中查找到对应的metric信息,并返回该metric的数据处理方式
class NamedMetricController(rest.RestController):
def __init__(self, resource_id, resource_type):
self.resource_id = resource_id
self.resource_type = resource_type
@pecan.expose()
def _lookup(self, name, *remainder):
details = True ifpecan.request.method == 'GET'else False
m = pecan.request.indexer.list_metrics(details=details,
name=name,
resource_id=self.resource_id)
if m:
return MetricController(m[0]), remainder
resource = pecan.request.indexer.get_resource(self.resource_type,self.resource_id)
if resource:
abort(404, indexer.NoSuchMetric(name))
else:
abort(404, indexer.NoSuchResource(self.resource_id))
源代码位于: gnocchi/rest/__init__.py中MeticController:
根据pecan.request.storage定义的后端存储类型,调用对应的add_measures接口,本文主要以本地存储file进行分析。
@pecan.expose()
def post_measures(self):
self.enforce_metric("post measures")
params = deserialize()
if not isinstance(params,list):
abort(400, "Invalid input for measures")
if params:
# gnocchi.storage.file.FileStorage
pecan.request.storage.add_measures(
self.metric, six.moves.map(MeasureSchema, params))
pecan.response.status = 202
该方法的父类源码位于gnocchi/storage/_carbonara.py的CarbnonaraBaseStorage,其他的存储后端实现add_measures中的_store_measures()方法
def add_measures(self, metric, measures):
self._store_measures(metric, msgpackutils.dumps(
list(map(tuple, measures))))
该源码位于 gnocchi/storage/file.py的FileStorage类中:
def _store_measures(self, metric, data):
tmpfile = self._get_tempfile()
#临时文件路径为/var/lib/gnocchi/tmp
tmpfile.write(data)
tmpfile.close()
path = self._build_measure_path(metric.id,True)
#该路径为/var/lib/gnocchi/measure/metric.id/+随机uuid+时间戳
while True:
try:
os.rename(tmpfile.name, path)
break
except OSErroras e:
if e.errno != errno.ENOENT:
raise
try:
os.mkdir(self._build_measure_path(metric.id))
except OSErroras e:
# NOTE(jd) It's possible that another process created the
# path just before us! In this case, good for us, let's do
# nothing then! (see bug #1475684)
ife.errno != errno.EEXIST:
raise
具体如下: