costmodel.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package costmodel
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/http/pprof"
  7. "time"
  8. "github.com/julienschmidt/httprouter"
  9. "github.com/opencost/opencost/pkg/cloudcost"
  10. "github.com/opencost/opencost/pkg/customcost"
  11. "github.com/prometheus/client_golang/prometheus/promhttp"
  12. "github.com/rs/cors"
  13. "github.com/opencost/opencost/core/pkg/log"
  14. "github.com/opencost/opencost/core/pkg/version"
  15. "github.com/opencost/opencost/pkg/costmodel"
  16. "github.com/opencost/opencost/pkg/env"
  17. "github.com/opencost/opencost/pkg/errors"
  18. "github.com/opencost/opencost/pkg/filemanager"
  19. "github.com/opencost/opencost/pkg/metrics"
  20. )
  21. // CostModelOpts contain configuration options that can be passed to the Execute() method
  22. type CostModelOpts struct {
  23. // Stubbed for future configuration
  24. }
  25. func Healthz(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
  26. w.WriteHeader(200)
  27. w.Header().Set("Content-Length", "0")
  28. w.Header().Set("Content-Type", "text/plain")
  29. }
  30. func Execute(opts *CostModelOpts) error {
  31. log.Infof("Starting cost-model version %s", version.FriendlyVersion())
  32. log.Infof("Kubernetes enabled: %t", env.IsKubernetesEnabled())
  33. var a *costmodel.Accesses
  34. if env.IsKubernetesEnabled() {
  35. a = costmodel.Initialize()
  36. err := StartExportWorker(context.Background(), a.Model)
  37. if err != nil {
  38. log.Errorf("couldn't start CSV export worker: %v", err)
  39. }
  40. } else {
  41. a = costmodel.InitializeWithoutKubernetes()
  42. log.Debugf("Cloud Cost config path: %s", env.GetCloudCostConfigPath())
  43. }
  44. log.Infof("Cloud Costs enabled: %t", env.IsCloudCostEnabled())
  45. if env.IsCloudCostEnabled() {
  46. repo := cloudcost.NewMemoryRepository()
  47. a.CloudCostPipelineService = cloudcost.NewPipelineService(repo, a.CloudConfigController, cloudcost.DefaultIngestorConfiguration())
  48. repoQuerier := cloudcost.NewRepositoryQuerier(repo)
  49. a.CloudCostQueryService = cloudcost.NewQueryService(repoQuerier, repoQuerier)
  50. }
  51. log.Infof("Custom Costs enabled: %t", env.IsCustomCostEnabled())
  52. if env.IsCustomCostEnabled() {
  53. hourlyRepo := customcost.NewMemoryRepository()
  54. dailyRepo := customcost.NewMemoryRepository()
  55. ingConfig := customcost.DefaultIngestorConfiguration()
  56. var err error
  57. a.CustomCostPipelineService, err = customcost.NewPipelineService(hourlyRepo, dailyRepo, ingConfig)
  58. if err != nil {
  59. return fmt.Errorf("error instantiating custom cost pipeline service: %v", err)
  60. }
  61. //repoQuerier := cloudcost.NewRepositoryQuerier(repo)
  62. //a.CloudCostQueryService = cloudcost.NewQueryService(repoQuerier, repoQuerier)
  63. }
  64. rootMux := http.NewServeMux()
  65. a.Router.GET("/healthz", Healthz)
  66. if env.IsKubernetesEnabled() {
  67. a.Router.GET("/allocation", a.ComputeAllocationHandler)
  68. a.Router.GET("/allocation/summary", a.ComputeAllocationHandlerSummary)
  69. a.Router.GET("/assets", a.ComputeAssetsHandler)
  70. }
  71. a.Router.GET("/cloudCost", a.CloudCostQueryService.GetCloudCostHandler())
  72. a.Router.GET("/cloudCost/view/graph", a.CloudCostQueryService.GetCloudCostViewGraphHandler())
  73. a.Router.GET("/cloudCost/view/totals", a.CloudCostQueryService.GetCloudCostViewTotalsHandler())
  74. a.Router.GET("/cloudCost/view/table", a.CloudCostQueryService.GetCloudCostViewTableHandler())
  75. a.Router.GET("/cloudCost/status", a.CloudCostPipelineService.GetCloudCostStatusHandler())
  76. a.Router.GET("/cloudCost/rebuild", a.CloudCostPipelineService.GetCloudCostRebuildHandler())
  77. a.Router.GET("/cloudCost/repair", a.CloudCostPipelineService.GetCloudCostRepairHandler())
  78. if env.IsPProfEnabled() {
  79. a.Router.HandlerFunc(http.MethodGet, "/debug/pprof/", pprof.Index)
  80. a.Router.HandlerFunc(http.MethodGet, "/debug/pprof/cmdline", pprof.Cmdline)
  81. a.Router.HandlerFunc(http.MethodGet, "/debug/pprof/profile", pprof.Profile)
  82. a.Router.HandlerFunc(http.MethodGet, "/debug/pprof/symbol", pprof.Symbol)
  83. a.Router.HandlerFunc(http.MethodGet, "/debug/pprof/trace", pprof.Trace)
  84. a.Router.Handler(http.MethodGet, "/debug/pprof/goroutine", pprof.Handler("goroutine"))
  85. a.Router.Handler(http.MethodGet, "/debug/pprof/heap", pprof.Handler("heap"))
  86. }
  87. rootMux.Handle("/", a.Router)
  88. rootMux.Handle("/metrics", promhttp.Handler())
  89. telemetryHandler := metrics.ResponseMetricMiddleware(rootMux)
  90. handler := cors.AllowAll().Handler(telemetryHandler)
  91. return http.ListenAndServe(fmt.Sprint(":", env.GetAPIPort()), errors.PanicHandlerMiddleware(handler))
  92. }
  93. func StartExportWorker(ctx context.Context, model costmodel.AllocationModel) error {
  94. exportPath := env.GetExportCSVFile()
  95. if exportPath == "" {
  96. log.Infof("%s is not set, CSV export is disabled", env.ExportCSVFile)
  97. return nil
  98. }
  99. fm, err := filemanager.NewFileManager(exportPath)
  100. if err != nil {
  101. return fmt.Errorf("could not create file manager: %v", err)
  102. }
  103. go func() {
  104. log.Info("Starting CSV exporter worker...")
  105. // perform first update immediately
  106. nextRunAt := time.Now()
  107. for {
  108. select {
  109. case <-ctx.Done():
  110. return
  111. case <-time.After(nextRunAt.Sub(time.Now())):
  112. err := costmodel.UpdateCSV(ctx, fm, model, env.GetExportCSVLabelsAll(), env.GetExportCSVLabelsList())
  113. if err != nil {
  114. // it's background worker, log error and carry on, maybe next time it will work
  115. log.Errorf("Error updating CSV: %s", err)
  116. }
  117. now := time.Now().UTC()
  118. // next launch is at 00:10 UTC tomorrow
  119. // extra 10 minutes is to let prometheus to collect all the data for the previous day
  120. nextRunAt = time.Date(now.Year(), now.Month(), now.Day(), 0, 10, 0, 0, now.Location()).AddDate(0, 0, 1)
  121. }
  122. }
  123. }()
  124. return nil
  125. }