【发布时间】:2016-02-20 20:49:17
【问题描述】:
我目前正在开发一个处理metrics 的用户指定列表(在文件中)的系统;度量标准是一些字符串和一些关于如何执行特定计算的逻辑的集合。 python 脚本会定期在 linux 服务器上运行并查询文件。
指标应该易于扩展,因此我正在寻找某种模块化方法来将计算代码封装到指标规范中。没有创建一个配置解析系统,这将严重限制计算的复杂性,我正在寻找一个插件系统,其中每个指标的计算都可以在 python 代码中指定。
例如,这里是指标的核心单位。
db = "database0"
mes = "measurement0"
tags = ["alpha", "beta", "delta"]
calculation: (accepts parameters a and b)
for item in a:
b.add(a)
return b
python 脚本需要从文件中获取每个指标,访问它们的字符串字段并执行它们的计算代码,将相同的参数传递给每个。
我目前有一个糟糕的方法;在一个单独的 python 文件中,我定义了一个基类,每个新指标都将对其进行扩展。就是这个样子
class Metric(object):
def __init__(self, database_name, measurement_name, classad_tags, classad_fields=[]):
"""
A specification of a metric.
Arguments:
database_name - name of the influx DB (created if doesn't exist)
measurement_name - measurement name with which to label metric in DB
classad_tags - list of classad fields (or mock ads) which will
segregate values at a time for this metric, becoming
tags in the influxDB measurement
classad_fields - any additional job classad fields that this metric will
look at (e.g. for metric value calculation).
These must be declared so that the daemon can fetch any
needed classads from condor
"""
self.db = database_name
self.mes = measurement_name
self.tags = classad_tags
self.fields = classad_fields
def calculate_at_bin(self, time_bin, jobs):
"""
"""
raise ReferenceError("")
"""
--------------------------------------------------------------------------------------
"""
class metric0(Metric):
def __init__(self):
db = 'testdb'
mes = 'testmes0'
tags = ['Owner']
fields = []
super(metric0, self).__init__(db, mes, tags, fields)
def calculate_at_bin(self, time_bin, jobs):
for job in jobs:
if job.is_idle_during(time_bin.start_time, time_bin.end_time):
time_bin.add_to_sum(1, job.get_values(self.tags))
return time_bin.get_sum()
要创建新指标,您需要创建Metric 的新子类,将字符串字段传递给超级构造函数并覆盖calculate_at_bin 方法。在主 python 脚本中,我创建了每个指标的实例,其中包含...
metrics = [metric() for metric in vars()['Metric'].__subclasses__()]
但是,这显然很糟糕。
它涉及完全任意但必须唯一的子类名称 (metric0) 和与指标无关的代码(例如类定义、调用构造函数等)。
我还可以想象另一种糟糕的方法,比如带有一些代码的配置文件会得到eval'd,但我也想避免这种情况。
请注意,无需担心安全性或代码注入;这个系统真的只会被我亲自指导的少数人使用。 所以;我该怎么办?
【问题讨论】:
-
在 stackoverflow 标题中使用“最佳实践”会使人容易受到路过投票的影响