2
0

thanos.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package thanos
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. "github.com/kubecost/cost-model/pkg/env"
  7. )
  8. var (
  9. lock = new(sync.Mutex)
  10. enabled = env.IsThanosEnabled()
  11. queryUrl = env.GetThanosQueryUrl()
  12. offset = env.GetThanosOffset()
  13. offsetDuration *time.Duration
  14. queryOffset = fmt.Sprintf(" offset %s", offset)
  15. )
  16. // IsEnabled returns true if Thanos is enabled.
  17. func IsEnabled() bool {
  18. return enabled
  19. }
  20. // QueryURL returns true if Thanos is enabled.
  21. func QueryURL() string {
  22. return queryUrl
  23. }
  24. // Offset returns the duration string for the query offset that should be applied to thanos
  25. func Offset() string {
  26. return offset
  27. }
  28. // OffsetDuration returns the Offset as a parsed duration
  29. func OffsetDuration() time.Duration {
  30. lock.Lock()
  31. defer lock.Unlock()
  32. if offsetDuration == nil {
  33. d, err := time.ParseDuration(offset)
  34. if err != nil {
  35. d = 0
  36. }
  37. offsetDuration = &d
  38. }
  39. return *offsetDuration
  40. }
  41. // QueryOffset returns a string in the format: " offset %s" substituting in the Offset() string.
  42. func QueryOffset() string {
  43. return queryOffset
  44. }