Просмотр исходного кода

Include Thanos as possible remote endpoint for querying metrics.

Matt Bolt 6 лет назад
Родитель
Сommit
32276b95cc
2 измененных файлов с 49 добавлено и 4 удалено
  1. 4 2
      costmodel/costmodel.go
  2. 45 2
      costmodel/router.go

+ 4 - 2
costmodel/costmodel.go

@@ -38,8 +38,10 @@ const (
 	epConfig          = apiPrefix + "/status/config"
 	epFlags           = apiPrefix + "/status/flags"
 
-	clusterIDKey  = "CLUSTER_ID"
-	remoteEnabled = "REMOTE_WRITE_ENABLED"
+	clusterIDKey   = "CLUSTER_ID"
+	remoteEnabled  = "REMOTE_WRITE_ENABLED"
+	thanosEnabled  = "THANOS_ENABLED"
+	thanosQueryUrl = "THANOS_QUERY_URL"
 )
 
 type CostModel struct {

+ 45 - 2
costmodel/router.go

@@ -42,6 +42,7 @@ var A Accesses
 
 type Accesses struct {
 	PrometheusClient              prometheusClient.Client
+	ThanosClient                  prometheusClient.Client
 	KubeClientSet                 kubernetes.Interface
 	Cloud                         costAnalyzerCloud.Provider
 	CPUPriceRecorder              *prometheus.GaugeVec
@@ -340,7 +341,15 @@ func (a *Accesses) AggregateCostModel(w http.ResponseWriter, r *http.Request, ps
 	}
 	klog.Infof("REMOTE ENABLED: %t", remoteEnabled)
 
-	data, err := a.Model.ComputeCostDataRange(a.PrometheusClient, a.KubeClientSet, a.Cloud, start, end, "1h", namespace, cluster, remoteEnabled)
+	// Use Thanos Client if it exists and remote != false
+	var pClient prometheusClient.Client
+	if remote != "false" && a.ThanosClient != nil {
+		pClient = a.ThanosClient
+	} else {
+		pClient = a.PrometheusClient
+	}
+
+	data, err := a.Model.ComputeCostDataRange(pClient, a.KubeClientSet, a.Cloud, start, end, "1h", namespace, cluster, remoteEnabled)
 	if err != nil {
 		w.Write(wrapData(nil, err))
 		return
@@ -411,7 +420,16 @@ func (a *Accesses) CostDataModelRange(w http.ResponseWriter, r *http.Request, ps
 	if remoteAvailable == "true" && remote != "false" {
 		remoteEnabled = true
 	}
-	data, err := a.Model.ComputeCostDataRange(a.PrometheusClient, a.KubeClientSet, a.Cloud, start, end, window, namespace, cluster, remoteEnabled)
+
+	// Use Thanos Client if it exists and remote != false
+	var pClient prometheusClient.Client
+	if remote != "false" && a.ThanosClient != nil {
+		pClient = a.ThanosClient
+	} else {
+		pClient = a.PrometheusClient
+	}
+
+	data, err := a.Model.ComputeCostDataRange(pClient, a.KubeClientSet, a.Cloud, start, end, window, namespace, cluster, remoteEnabled)
 	if err != nil {
 		w.Write(wrapData(nil, err))
 	}
@@ -955,6 +973,31 @@ func init() {
 		}
 	}
 
+	// Thanos Client
+	if os.Getenv(thanosEnabled) == "true" {
+		thanosUrl := os.Getenv(thanosQueryUrl)
+		if thanosUrl != "" {
+			var thanosRT http.RoundTripper = &http.Transport{
+				Proxy: http.ProxyFromEnvironment,
+				DialContext: (&net.Dialer{
+					Timeout:   120 * time.Second,
+					KeepAlive: 120 * time.Second,
+				}).DialContext,
+				TLSHandshakeTimeout: 10 * time.Second,
+			}
+
+			thanosConfig := prometheusClient.Config{
+				Address:      thanosUrl,
+				RoundTripper: thanosRT,
+			}
+			thanosCli, _ := prometheusClient.NewClient(thanosConfig)
+
+			A.ThanosClient = thanosCli
+		} else {
+			klog.Infof("Error resolving environment variable: $%s", thanosQueryUrl)
+		}
+	}
+
 	err = A.Cloud.DownloadPricingData()
 	if err != nil {
 		klog.V(1).Info("Failed to download pricing data: " + err.Error())