promenv.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package env
  2. import (
  3. "fmt"
  4. "runtime"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/env"
  7. )
  8. const (
  9. PrometheusServerEndpointEnvVar = "PROMETHEUS_SERVER_ENDPOINT"
  10. PrometheusRetryOnRateLimitResponseEnvVar = "PROMETHEUS_RETRY_ON_RATE_LIMIT"
  11. PrometheusRetryOnRateLimitMaxRetriesEnvVar = "PROMETHEUS_RETRY_ON_RATE_LIMIT_MAX_RETRIES"
  12. PrometheusRetryOnRateLimitDefaultWaitEnvVar = "PROMETHEUS_RETRY_ON_RATE_LIMIT_DEFAULT_WAIT"
  13. PrometheusQueryTimeoutEnvVar = "PROMETHEUS_QUERY_TIMEOUT"
  14. PrometheusKeepAliveEnvVar = "PROMETHEUS_KEEP_ALIVE"
  15. PrometheusTLSHandshakeTimeoutEnvVar = "PROMETHEUS_TLS_HANDSHAKE_TIMEOUT"
  16. ScrapeIntervalEnvVar = "KUBECOST_SCRAPE_INTERVAL"
  17. ETLMaxPrometheusQueryDurationMinutes = "ETL_MAX_PROMETHEUS_QUERY_DURATION_MINUTES"
  18. MaxQueryConcurrencyEnvVar = "MAX_QUERY_CONCURRENCY"
  19. QueryLoggingFileEnvVar = "QUERY_LOGGING_FILE"
  20. PromClusterIDLabelEnvVar = "PROM_CLUSTER_ID_LABEL"
  21. PrometheusHeaderXScopeOrgIdEnvVar = "PROMETHEUS_HEADER_X_SCOPE_ORGID"
  22. InsecureSkipVerifyEnvVar = "INSECURE_SKIP_VERIFY"
  23. KubeRbacProxyEnabledEnvVar = "KUBE_RBAC_PROXY_ENABLED"
  24. ThanosEnabledEnvVar = "THANOS_ENABLED"
  25. ThanosQueryUrlEnvVar = "THANOS_QUERY_URL"
  26. ThanosOffsetEnvVar = "THANOS_QUERY_OFFSET"
  27. ThanosMaxSourceResEnvVar = "THANOS_MAX_SOURCE_RESOLUTION"
  28. DBBasicAuthUsername = "DB_BASIC_AUTH_USERNAME"
  29. DBBasicAuthPassword = "DB_BASIC_AUTH_PW"
  30. DBBearerToken = "DB_BEARER_TOKEN"
  31. MultiClusterBasicAuthUsername = "MC_BASIC_AUTH_USERNAME"
  32. MultiClusterBasicAuthPassword = "MC_BASIC_AUTH_PW"
  33. MultiClusterBearerToken = "MC_BEARER_TOKEN"
  34. CurrentClusterIdFilterEnabledVar = "CURRENT_CLUSTER_ID_FILTER_ENABLED"
  35. ClusterIDEnvVar = "CLUSTER_ID"
  36. KubecostJobNameEnvVar = "KUBECOST_JOB_NAME"
  37. ETLResolutionSecondsEnvVar = "ETL_RESOLUTION_SECONDS"
  38. )
  39. // IsPrometheusRetryOnRateLimitResponse will attempt to retry if a 429 response is received OR a 400 with a body containing
  40. // ThrottleException (common in AWS services like AMP)
  41. func IsPrometheusRetryOnRateLimitResponse() bool {
  42. return env.GetBool(PrometheusRetryOnRateLimitResponseEnvVar, true)
  43. }
  44. // GetPrometheusRetryOnRateLimitMaxRetries returns the maximum number of retries that should be attempted prior to failing.
  45. // Only used if IsPrometheusRetryOnRateLimitResponse() is true.
  46. func GetPrometheusRetryOnRateLimitMaxRetries() int {
  47. return env.GetInt(PrometheusRetryOnRateLimitMaxRetriesEnvVar, 5)
  48. }
  49. // GetPrometheusRetryOnRateLimitDefaultWait returns the default wait time for a retriable rate limit response without a
  50. // Retry-After header.
  51. func GetPrometheusRetryOnRateLimitDefaultWait() time.Duration {
  52. return env.GetDuration(PrometheusRetryOnRateLimitDefaultWaitEnvVar, 100*time.Millisecond)
  53. }
  54. // GetPrometheusHeaderXScopeOrgId returns the default value for X-Scope-OrgID header used for requests in Mimir/Cortex-Tenant API.
  55. // To use Mimir(or Cortex-Tenant) instead of Prometheus add variable from cluster settings:
  56. // "PROMETHEUS_HEADER_X_SCOPE_ORGID": "my-cluster-name"
  57. // Then set Prometheus URL to prometheus API endpoint:
  58. // "PROMETHEUS_SERVER_ENDPOINT": "http://mimir-url/prometheus/"
  59. func GetPrometheusHeaderXScopeOrgId() string {
  60. return env.Get(PrometheusHeaderXScopeOrgIdEnvVar, "")
  61. }
  62. // GetPrometheusServerEndpoint returns the environment variable value for PrometheusServerEndpointEnvVar which
  63. // represents the prometheus server endpoint used to execute prometheus queries.
  64. func GetPrometheusServerEndpoint() string {
  65. return env.Get(PrometheusServerEndpointEnvVar, "")
  66. }
  67. func GetScrapeInterval() time.Duration {
  68. return env.GetDuration(ScrapeIntervalEnvVar, 0)
  69. }
  70. func GetPrometheusQueryTimeout() time.Duration {
  71. return env.GetDuration(PrometheusQueryTimeoutEnvVar, 120*time.Second)
  72. }
  73. func GetPrometheusKeepAlive() time.Duration {
  74. return env.GetDuration(PrometheusKeepAliveEnvVar, 120*time.Second)
  75. }
  76. func GetPrometheusTLSHandshakeTimeout() time.Duration {
  77. return env.GetDuration(PrometheusTLSHandshakeTimeoutEnvVar, 10*time.Second)
  78. }
  79. // GetJobName returns the environment variable value for JobNameEnvVar, specifying which job name
  80. // is used for prometheus to scrape the provided metrics.
  81. func GetJobName() string {
  82. return env.Get(KubecostJobNameEnvVar, "kubecost")
  83. }
  84. func IsInsecureSkipVerify() bool {
  85. return env.GetBool(InsecureSkipVerifyEnvVar, false)
  86. }
  87. func IsKubeRbacProxyEnabled() bool {
  88. return env.GetBool(KubeRbacProxyEnabledEnvVar, false)
  89. }
  90. // GetETLResolution determines the resolution of ETL queries. The smaller the
  91. // duration, the higher the resolution; the higher the resolution, the more
  92. // accurate the query results, but the more computationally expensive.
  93. func GetETLResolution() time.Duration {
  94. // Use the configured ETL resolution, or default to
  95. // 5m (i.e. 300s)
  96. secs := time.Duration(env.GetInt64(ETLResolutionSecondsEnvVar, 300))
  97. return secs * time.Second
  98. }
  99. // IsThanosEnabled returns the environment variable value for ThanosEnabledEnvVar which represents whether
  100. // or not thanos is enabled.
  101. func IsThanosEnabled() bool {
  102. return env.GetBool(ThanosEnabledEnvVar, false)
  103. }
  104. // GetThanosQueryUrl returns the environment variable value for ThanosQueryUrlEnvVar which represents the
  105. // target query endpoint for hitting thanos.
  106. func GetThanosQueryUrl() string {
  107. return env.Get(ThanosQueryUrlEnvVar, "")
  108. }
  109. // GetThanosOffset returns the environment variable value for ThanosOffsetEnvVar which represents the total
  110. // amount of time to offset all queries made to thanos.
  111. func GetThanosOffset() string {
  112. return env.Get(ThanosOffsetEnvVar, "3h")
  113. }
  114. // GetThanosMaxSourceResolution returns the environment variable value for ThanosMaxSourceResEnvVar which represents
  115. // the max source resolution to use when querying thanos.
  116. func GetThanosMaxSourceResolution() string {
  117. res := env.Get(ThanosMaxSourceResEnvVar, "raw")
  118. switch res {
  119. case "raw":
  120. return "0s"
  121. case "0s":
  122. fallthrough
  123. case "5m":
  124. fallthrough
  125. case "1h":
  126. return res
  127. default:
  128. return "0s"
  129. }
  130. }
  131. // GetMaxQueryConcurrency returns the environment variable value for MaxQueryConcurrencyEnvVar
  132. func GetMaxQueryConcurrency() int {
  133. maxQueryConcurrency := env.GetInt(MaxQueryConcurrencyEnvVar, 5)
  134. if maxQueryConcurrency <= 0 {
  135. return runtime.GOMAXPROCS(0)
  136. }
  137. return maxQueryConcurrency
  138. }
  139. // GetQueryLoggingFile returns a file location if query logging is enabled. Otherwise, empty string
  140. func GetQueryLoggingFile() string {
  141. return env.Get(QueryLoggingFileEnvVar, "")
  142. }
  143. func GetDBBasicAuthUsername() string {
  144. return env.Get(DBBasicAuthUsername, "")
  145. }
  146. func GetDBBasicAuthUserPassword() string {
  147. return env.Get(DBBasicAuthPassword, "")
  148. }
  149. func GetDBBearerToken() string {
  150. return env.Get(DBBearerToken, "")
  151. }
  152. // GetMultiClusterBasicAuthUsername returns the environment variable value for MultiClusterBasicAuthUsername
  153. func GetMultiClusterBasicAuthUsername() string {
  154. return env.Get(MultiClusterBasicAuthUsername, "")
  155. }
  156. // GetMultiClusterBasicAuthPassword returns the environment variable value for MultiClusterBasicAuthPassword
  157. func GetMultiClusterBasicAuthPassword() string {
  158. return env.Get(MultiClusterBasicAuthPassword, "")
  159. }
  160. func GetMultiClusterBearerToken() string {
  161. return env.Get(MultiClusterBearerToken, "")
  162. }
  163. func GetETLMaxPrometheusQueryDuration() time.Duration {
  164. dayMins := 60 * 24
  165. mins := time.Duration(env.GetInt64(ETLMaxPrometheusQueryDurationMinutes, int64(dayMins)))
  166. return mins * time.Minute
  167. }
  168. // GetPromClusterLabel returns the environment variable value for PromClusterIDLabel
  169. func GetPromClusterLabel() string {
  170. return env.Get(PromClusterIDLabelEnvVar, "cluster_id")
  171. }
  172. // GetClusterID returns the environment variable value for ClusterIDEnvVar which represents the
  173. // configurable identifier used for multi-cluster metric emission.
  174. func GetClusterID() string {
  175. return env.Get(ClusterIDEnvVar, "")
  176. }
  177. // GetPromClusterFilter returns environment variable value CurrentClusterIdFilterEnabledVar which
  178. // represents additional prometheus filter for all metrics for current cluster id
  179. func GetPromClusterFilter() string {
  180. if env.GetBool(CurrentClusterIdFilterEnabledVar, false) {
  181. return fmt.Sprintf("%s=\"%s\"", GetPromClusterLabel(), GetClusterID())
  182. }
  183. return ""
  184. }