promenv.go 5.6 KB

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