فهرست منبع

Allow passing max resolution

(cherry picked from commit 545efd5e97582733d2fb56e7ec6bb59633420772)
Matt Bolt 5 سال پیش
والد
کامیت
8668373580
2فایلهای تغییر یافته به همراه29 افزوده شده و 4 حذف شده
  1. 23 3
      pkg/env/costmodelenv.go
  2. 6 1
      pkg/thanos/thanos.go

+ 23 - 3
pkg/env/costmodelenv.go

@@ -22,9 +22,10 @@ const (
 	ConfigPathEnvVar               = "CONFIG_PATH"
 	CloudProviderAPIKeyEnvVar      = "CLOUD_PROVIDER_API_KEY"
 
-	ThanosEnabledEnvVar  = "THANOS_ENABLED"
-	ThanosQueryUrlEnvVar = "THANOS_QUERY_URL"
-	ThanosOffsetEnvVar   = "THANOS_QUERY_OFFSET"
+	ThanosEnabledEnvVar      = "THANOS_ENABLED"
+	ThanosQueryUrlEnvVar     = "THANOS_QUERY_URL"
+	ThanosOffsetEnvVar       = "THANOS_QUERY_OFFSET"
+	ThanosMaxSourceResEnvVar = "THANOS_MAX_SOURCE_RESOLUTION"
 
 	LogCollectionEnabledEnvVar    = "LOG_COLLECTION_ENABLED"
 	ProductAnalyticsEnabledEnvVar = "PRODUCT_ANALYTICS_ENABLED"
@@ -168,6 +169,25 @@ func GetThanosOffset() string {
 	return Get(ThanosOffsetEnvVar, "3h")
 }
 
+// GetThanosMaxSourceResolution returns the environment variable value for ThanosMaxSourceResEnvVar which represents
+// the max source resolution to use when querying thanos.
+func GetThanosMaxSourceResolution() string {
+	res := Get(ThanosMaxSourceResEnvVar, "raw")
+
+	switch res {
+	case "raw":
+		return "0s"
+	case "0s":
+		fallthrough
+	case "5m":
+		fallthrough
+	case "1h":
+		return res
+	default:
+		return "0s"
+	}
+}
+
 // IsLogCollectionEnabled returns the environment variable value for LogCollectionEnabledEnvVar which represents
 // whether or not log collection has been enabled for kubecost deployments.
 func IsLogCollectionEnabled() bool {

+ 6 - 1
pkg/thanos/thanos.go

@@ -16,11 +16,16 @@ import (
 	prometheus "github.com/prometheus/client_golang/api"
 )
 
+// MaxSourceResulution is the query parameter key used to designate the resolution
+// to use when executing a query.
+const MaxSourceResulution = "max_source_resolution"
+
 var (
 	lock           = new(sync.Mutex)
 	enabled        = env.IsThanosEnabled()
 	queryUrl       = env.GetThanosQueryUrl()
 	offset         = env.GetThanosOffset()
+	maxSourceRes   = env.GetThanosMaxSourceResolution()
 	offsetDuration *time.Duration
 	queryOffset    = fmt.Sprintf(" offset %s", offset)
 )
@@ -87,7 +92,7 @@ func NewThanosClient(address string, timeout, keepAlive time.Duration, queryConc
 	// max source resolution decorator
 	maxSourceDecorator := func(path string, queryParams url.Values) url.Values {
 		if strings.Contains(path, "query") {
-			queryParams.Set("max_source_resolution", "5m")
+			queryParams.Set(MaxSourceResulution, maxSourceRes)
 		}
 		return queryParams
 	}