2
0

opencost.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package env
  2. import (
  3. "time"
  4. "github.com/opencost/opencost/core/pkg/env"
  5. "github.com/opencost/opencost/core/pkg/log"
  6. "github.com/opencost/opencost/core/pkg/util/timeutil"
  7. )
  8. // Environment variables specific to the running of opencost
  9. const (
  10. DefaultAPIPort = 9003
  11. defaultOpencostNamespace = "opencost"
  12. )
  13. const (
  14. UTCOffsetEnvVar = "UTC_OFFSET"
  15. MCPQueryTimeoutSecondsEnvVar = "MCP_QUERY_TIMEOUT_SECONDS"
  16. )
  17. func GetOpencostAPIPort() int {
  18. return env.GetAPIPortWithDefault(DefaultAPIPort)
  19. }
  20. // GetOpencostNamespace returns the environment variable value that is set for the kubernetes namespace
  21. // this service is installed in.
  22. func GetOpencostNamespace() string {
  23. return env.GetInstallNamespace(defaultOpencostNamespace)
  24. }
  25. // GetUTCOffset returns the environment variable value for UTCOffset
  26. func GetUTCOffset() string {
  27. return env.Get(UTCOffsetEnvVar, "")
  28. }
  29. // GetParsedUTCOffset returns the duration of the configured UTC offset
  30. func GetParsedUTCOffset() time.Duration {
  31. offset, err := timeutil.ParseUTCOffset(GetUTCOffset())
  32. if err != nil {
  33. log.Warnf("Failed to parse UTC offset: %s", err)
  34. return time.Duration(0)
  35. }
  36. return offset
  37. }
  38. // GetMCPQueryTimeout returns the configured timeout for MCP query operations.
  39. // Default is 60 seconds, but can be configured via MCP_QUERY_TIMEOUT_SECONDS environment variable.
  40. // Minimum timeout is 1 second to prevent immediate timeouts.
  41. func GetMCPQueryTimeout() time.Duration {
  42. seconds := env.GetInt(MCPQueryTimeoutSecondsEnvVar, 60)
  43. if seconds <= 0 {
  44. seconds = 1
  45. }
  46. return time.Duration(seconds) * time.Second
  47. }