thanos.go 1.1 KB

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