多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] PrometheusMeasurement它是一个抽象结构,任何依赖于Prometheus的Measurement都要基于这个结构体。 它的定义如下: ``` type prometheusMeasurement struct { gatherer Gatherer startTime time.Time } ``` Gather是一个interface,定义如下: ``` // Gatherer is an interface for measurements based on Prometheus metrics. Those measurments don't require any preparation. // It's assumed Prometheus is up, running and instructed to scrape required metrics in the test cluster // (please see clusterloader2/pkg/prometheus/manifests). type Gatherer interface { Gather(executor QueryExecutor, startTime, endTime time.Time, config *measurement.Config) ([]measurement.Summary, error) IsEnabled(config *measurement.Config) bool String() string } ``` PrometheusMeasurement的Excute函数如下: ``` func (m *prometheusMeasurement) Execute(config *measurement.Config) ([]measurement.Summary, error) { if config.PrometheusFramework == nil { klog.Warningf("%s: Prometheus is disabled, skipping the measurement!", config.Identifier) return nil, nil } if !m.gatherer.IsEnabled(config) { klog.Warningf("%s: disabled, skipping the measuerment!", config.Identifier) return nil, nil } action, err := util.GetString(config.Params, "action") if err != nil { return nil, err } switch action { case "start": klog.V(2).Infof("%s has started", config.Identifier) // start action 只是记录起始时间,没有其它任何操作 m.startTime = time.Now() return nil, nil case "gather": klog.V(2).Infof("%s gathering results", config.Identifier) enableViolations, err := util.GetBoolOrDefault(config.Params, "enableViolations", false) if err != nil { return nil, err } // 获取prometheus的client和executor,它们封装了Promql的查询 c := config.PrometheusFramework.GetClientSets().GetClient() executor := measurementutil.NewQueryExecutor(c) // gather action调用Gather()函数进行统计,实际上每个基于Prometheus的Measurement需要实现Gather接口 summary, err := m.gatherer.Gather(executor, m.startTime, time.Now(), config) if err != nil { if !errors.IsMetricViolationError(err) { klog.Errorf("%s gathering error: %v", config.Identifier, err) return nil, err } if !enableViolations { err = nil } } return summary, err default: return nil, fmt.Errorf("unknown action: %v", action) } } ``` 可以看出,start action阶段只是记录了一个时间点,没有做任何操作。gather action阶段,调用m.gather.Gather()函数。m.gather是一个Interface类型的对象,**也就是说,每个基于Prometheus的Measurement实际上要提供一个Gather对象,实现它的接口。**