Kaynağa Gözat

Merge pull request #2567 from AndrewChubatiuk/vm-support

use status/buildinfo endpoint to get status instead of status/config added variable to set static scrape interval
Matt Ray 2 yıl önce
ebeveyn
işleme
d6780d4a42
3 değiştirilmiş dosya ile 25 ekleme ve 14 silme
  1. 9 6
      pkg/cmd/agent/agent.go
  2. 11 8
      pkg/costmodel/router.go
  3. 5 0
      pkg/env/costmodelenv.go

+ 9 - 6
pkg/cmd/agent/agent.go

@@ -117,7 +117,7 @@ func newPrometheusClient() (prometheus.Client, error) {
 	}
 
 	api := prometheusAPI.NewAPI(promCli)
-	_, err = api.Config(context.Background())
+	_, err = api.Buildinfo(context.Background())
 	if err != nil {
 		log.Infof("No valid prometheus config file at %s. Error: %s . Troubleshooting help available at: %s. Ignore if using cortex/mimir/thanos here.", address, err.Error(), prom.PrometheusTroubleshootingURL)
 	} else {
@@ -132,16 +132,19 @@ func Execute(opts *AgentOpts) error {
 
 	configWatchers := watcher.NewConfigMapWatchers()
 
-	scrapeInterval := time.Minute
+	scrapeInterval := env.GetKubecostScrapeInterval()
 	promCli, err := newPrometheusClient()
 	if err != nil {
 		panic(err.Error())
 	}
 
-	// Lookup scrape interval for kubecost job, update if found
-	si, err := prom.ScrapeIntervalFor(promCli, env.GetKubecostJobName())
-	if err == nil {
-		scrapeInterval = si
+	if scrapeInterval == 0 {
+		scrapeInterval = time.Minute
+		// Lookup scrape interval for kubecost job, update if found
+		si, err := prom.ScrapeIntervalFor(promCli, env.GetKubecostJobName())
+		if err == nil {
+			scrapeInterval = si
+		}
 	}
 
 	log.Infof("Using scrape interval of %f", scrapeInterval.Seconds())

+ 11 - 8
pkg/costmodel/router.go

@@ -1412,12 +1412,12 @@ func (a *Accesses) Status(w http.ResponseWriter, r *http.Request, _ httprouter.P
 	promServer := env.GetPrometheusServerEndpoint()
 
 	api := prometheusAPI.NewAPI(a.PrometheusClient)
-	result, err := api.Config(r.Context())
+	result, err := api.Buildinfo(r.Context())
 	if err != nil {
 		fmt.Fprintf(w, "Using Prometheus at "+promServer+". Error: "+err.Error())
 	} else {
 
-		fmt.Fprintf(w, "Using Prometheus at "+promServer+". PrometheusConfig: "+result.YAML)
+		fmt.Fprintf(w, "Using Prometheus at "+promServer+". Version: "+result.Version)
 	}
 }
 
@@ -1519,7 +1519,7 @@ func Initialize(additionalConfigWatchers ...*watcher.ConfigMapWatcher) *Accesses
 	timeout := 120 * time.Second
 	keepAlive := 120 * time.Second
 	tlsHandshakeTimeout := 10 * time.Second
-	scrapeInterval := time.Minute
+	scrapeInterval := env.GetKubecostScrapeInterval()
 
 	var rateLimitRetryOpts *prom.RateLimitRetryOpts = nil
 	if env.IsPrometheusRetryOnRateLimitResponse() {
@@ -1560,17 +1560,20 @@ func Initialize(additionalConfigWatchers ...*watcher.ConfigMapWatcher) *Accesses
 	}
 
 	api := prometheusAPI.NewAPI(promCli)
-	_, err = api.Config(context.Background())
+	_, err = api.Buildinfo(context.Background())
 	if err != nil {
 		log.Infof("No valid prometheus config file at %s. Error: %s . Troubleshooting help available at: %s. Ignore if using cortex/mimir/thanos here.", address, err.Error(), prom.PrometheusTroubleshootingURL)
 	} else {
 		log.Infof("Retrieved a prometheus config file from: %s", address)
 	}
 
-	// Lookup scrape interval for kubecost job, update if found
-	si, err := prom.ScrapeIntervalFor(promCli, env.GetKubecostJobName())
-	if err == nil {
-		scrapeInterval = si
+	if scrapeInterval == 0 {
+		scrapeInterval = time.Minute
+		// Lookup scrape interval for kubecost job, update if found
+		si, err := prom.ScrapeIntervalFor(promCli, env.GetKubecostJobName())
+		if err == nil {
+			scrapeInterval = si
+		}
 	}
 
 	log.Infof("Using scrape interval of %f", scrapeInterval.Seconds())

+ 5 - 0
pkg/env/costmodelenv.go

@@ -25,6 +25,7 @@ const (
 	AzureDownloadBillingDataToDiskEnvVar = "AZURE_DOWNLOAD_BILLING_DATA_TO_DISK"
 
 	KubecostNamespaceEnvVar        = "KUBECOST_NAMESPACE"
+	KubecostScrapeIntervalEnvVar   = "KUBECOST_SCRAPE_INTERVAL"
 	PodNameEnvVar                  = "POD_NAME"
 	ClusterIDEnvVar                = "CLUSTER_ID"
 	ClusterProfileEnvVar           = "CLUSTER_PROFILE"
@@ -141,6 +142,10 @@ func GetExportCSVLabelsAll() bool {
 	return env.GetBool(ExportCSVLabelsAll, false)
 }
 
+func GetKubecostScrapeInterval() time.Duration {
+	return env.GetDuration(KubecostScrapeIntervalEnvVar, 0)
+}
+
 func GetExportCSVLabelsList() []string {
 	return env.GetList(ExportCSVLabelsList, ",")
 }