func()

in clusterloader2/pkg/measurement/common/prometheus_measurement.go [59:123]


func (m *prometheusMeasurement) Execute(config *measurement.Config) ([]measurement.Summary, error) {
	prometheusClient, err := util.GetStringOrDefault(config.Params, "prometheusClient", "inCluster")
	if err != nil {
		return nil, err
	}
	if prometheusClient != "inCluster" && prometheusClient != "managed" {
		return nil, fmt.Errorf("unknown Prometheus client")
	}
	if prometheusClient == "inCluster" && 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":
		if err := m.gatherer.Configure(config); err != nil {
			return nil, err
		}
		klog.V(2).Infof("%s has started", config.Identifier)
		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
		}

		var pc prom.Client
		switch prometheusClient {
		case "inCluster":
			pc = prom.NewInClusterPrometheusClient(config.PrometheusFramework.GetClientSets().GetClient())
		case "managed":
			pc, err = config.CloudProvider.GetManagedPrometheusClient()
			if err != nil {
				return nil, fmt.Errorf("error while creating managed Prometheus client: %w", err)
			}
		}
		executor := measurementutil.NewQueryExecutor(pc)

		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)
	}
}