promenv.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. DBBasicAuthUsername = "DB_BASIC_AUTH_USERNAME"
  25. DBBasicAuthPassword = "DB_BASIC_AUTH_PW"
  26. DBBearerToken = "DB_BEARER_TOKEN"
  27. MultiClusterBasicAuthUsername = "MC_BASIC_AUTH_USERNAME"
  28. MultiClusterBasicAuthPassword = "MC_BASIC_AUTH_PW"
  29. MultiClusterBearerToken = "MC_BEARER_TOKEN"
  30. CurrentClusterIdFilterEnabledVar = "CURRENT_CLUSTER_ID_FILTER_ENABLED"
  31. ClusterIDEnvVar = "CLUSTER_ID"
  32. KubecostJobNameEnvVar = "KUBECOST_JOB_NAME"
  33. ETLResolutionSecondsEnvVar = "ETL_RESOLUTION_SECONDS"
  34. )
  35. // IsPrometheusRetryOnRateLimitResponse will attempt to retry if a 429 response is received OR a 400 with a body containing
  36. // ThrottleException (common in AWS services like AMP)
  37. func IsPrometheusRetryOnRateLimitResponse() bool {
  38. return env.GetBool(PrometheusRetryOnRateLimitResponseEnvVar, true)
  39. }
  40. // GetPrometheusRetryOnRateLimitMaxRetries returns the maximum number of retries that should be attempted prior to failing.
  41. // Only used if IsPrometheusRetryOnRateLimitResponse() is true.
  42. func GetPrometheusRetryOnRateLimitMaxRetries() int {
  43. return env.GetInt(PrometheusRetryOnRateLimitMaxRetriesEnvVar, 5)
  44. }
  45. // GetPrometheusRetryOnRateLimitDefaultWait returns the default wait time for a retriable rate limit response without a
  46. // Retry-After header.
  47. func GetPrometheusRetryOnRateLimitDefaultWait() time.Duration {
  48. return env.GetDuration(PrometheusRetryOnRateLimitDefaultWaitEnvVar, 100*time.Millisecond)
  49. }
  50. // GetPrometheusHeaderXScopeOrgId returns the default value for X-Scope-OrgID header used for requests in Mimir/Cortex-Tenant API.
  51. // To use Mimir(or Cortex-Tenant) instead of Prometheus add variable from cluster settings:
  52. // "PROMETHEUS_HEADER_X_SCOPE_ORGID": "my-cluster-name"
  53. // Then set Prometheus URL to prometheus API endpoint:
  54. // "PROMETHEUS_SERVER_ENDPOINT": "http://mimir-url/prometheus/"
  55. func GetPrometheusHeaderXScopeOrgId() string {
  56. return env.Get(PrometheusHeaderXScopeOrgIdEnvVar, "")
  57. }
  58. // GetPrometheusServerEndpoint returns the environment variable value for PrometheusServerEndpointEnvVar which
  59. // represents the prometheus server endpoint used to execute prometheus queries.
  60. func GetPrometheusServerEndpoint() string {
  61. return env.Get(PrometheusServerEndpointEnvVar, "")
  62. }
  63. func GetScrapeInterval() time.Duration {
  64. return env.GetDuration(ScrapeIntervalEnvVar, 0)
  65. }
  66. func GetPrometheusQueryTimeout() time.Duration {
  67. return env.GetDuration(PrometheusQueryTimeoutEnvVar, 120*time.Second)
  68. }
  69. func GetPrometheusKeepAlive() time.Duration {
  70. return env.GetDuration(PrometheusKeepAliveEnvVar, 120*time.Second)
  71. }
  72. func GetPrometheusTLSHandshakeTimeout() time.Duration {
  73. return env.GetDuration(PrometheusTLSHandshakeTimeoutEnvVar, 10*time.Second)
  74. }
  75. // GetJobName returns the environment variable value for JobNameEnvVar, specifying which job name
  76. // is used for prometheus to scrape the provided metrics.
  77. func GetJobName() string {
  78. return env.Get(KubecostJobNameEnvVar, "kubecost")
  79. }
  80. func IsInsecureSkipVerify() bool {
  81. return env.GetBool(InsecureSkipVerifyEnvVar, false)
  82. }
  83. func IsKubeRbacProxyEnabled() bool {
  84. return env.GetBool(KubeRbacProxyEnabledEnvVar, false)
  85. }
  86. // GetETLResolution determines the resolution of ETL queries. The smaller the
  87. // duration, the higher the resolution; the higher the resolution, the more
  88. // accurate the query results, but the more computationally expensive.
  89. func GetETLResolution() time.Duration {
  90. // Use the configured ETL resolution, or default to
  91. // 5m (i.e. 300s)
  92. secs := time.Duration(env.GetInt64(ETLResolutionSecondsEnvVar, 300))
  93. return secs * time.Second
  94. }
  95. // GetMaxQueryConcurrency returns the environment variable value for MaxQueryConcurrencyEnvVar
  96. func GetMaxQueryConcurrency() int {
  97. maxQueryConcurrency := env.GetInt(MaxQueryConcurrencyEnvVar, 5)
  98. if maxQueryConcurrency <= 0 {
  99. return runtime.GOMAXPROCS(0)
  100. }
  101. return maxQueryConcurrency
  102. }
  103. // GetQueryLoggingFile returns a file location if query logging is enabled. Otherwise, empty string
  104. func GetQueryLoggingFile() string {
  105. return env.Get(QueryLoggingFileEnvVar, "")
  106. }
  107. func GetDBBasicAuthUsername() string {
  108. return env.Get(DBBasicAuthUsername, "")
  109. }
  110. func GetDBBasicAuthUserPassword() string {
  111. return env.Get(DBBasicAuthPassword, "")
  112. }
  113. func GetDBBearerToken() string {
  114. return env.Get(DBBearerToken, "")
  115. }
  116. // GetMultiClusterBasicAuthUsername returns the environment variable value for MultiClusterBasicAuthUsername
  117. func GetMultiClusterBasicAuthUsername() string {
  118. return env.Get(MultiClusterBasicAuthUsername, "")
  119. }
  120. // GetMultiClusterBasicAuthPassword returns the environment variable value for MultiClusterBasicAuthPassword
  121. func GetMultiClusterBasicAuthPassword() string {
  122. return env.Get(MultiClusterBasicAuthPassword, "")
  123. }
  124. func GetMultiClusterBearerToken() string {
  125. return env.Get(MultiClusterBearerToken, "")
  126. }
  127. func GetETLMaxPrometheusQueryDuration() time.Duration {
  128. dayMins := 60 * 24
  129. mins := time.Duration(env.GetInt64(ETLMaxPrometheusQueryDurationMinutes, int64(dayMins)))
  130. return mins * time.Minute
  131. }
  132. // GetPromClusterLabel returns the environment variable value for PromClusterIDLabel
  133. func GetPromClusterLabel() string {
  134. return env.Get(PromClusterIDLabelEnvVar, "cluster_id")
  135. }
  136. // GetClusterID returns the environment variable value for ClusterIDEnvVar which represents the
  137. // configurable identifier used for multi-cluster metric emission.
  138. func GetClusterID() string {
  139. return env.Get(ClusterIDEnvVar, "")
  140. }
  141. // GetPromClusterFilter returns environment variable value CurrentClusterIdFilterEnabledVar which
  142. // represents additional prometheus filter for all metrics for current cluster id
  143. func GetPromClusterFilter() string {
  144. if env.GetBool(CurrentClusterIdFilterEnabledVar, false) {
  145. return fmt.Sprintf("%s=\"%s\"", GetPromClusterLabel(), GetClusterID())
  146. }
  147. return ""
  148. }