costmodel.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package costmodel
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/julienschmidt/httprouter"
  8. "github.com/opencost/opencost/pkg/cloudcost"
  9. "github.com/prometheus/client_golang/prometheus/promhttp"
  10. "github.com/rs/cors"
  11. "github.com/opencost/opencost/pkg/costmodel"
  12. "github.com/opencost/opencost/pkg/env"
  13. "github.com/opencost/opencost/pkg/errors"
  14. "github.com/opencost/opencost/pkg/filemanager"
  15. "github.com/opencost/opencost/pkg/log"
  16. "github.com/opencost/opencost/pkg/metrics"
  17. "github.com/opencost/opencost/pkg/version"
  18. )
  19. // CostModelOpts contain configuration options that can be passed to the Execute() method
  20. type CostModelOpts struct {
  21. // Stubbed for future configuration
  22. }
  23. func Healthz(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
  24. w.WriteHeader(200)
  25. w.Header().Set("Content-Length", "0")
  26. w.Header().Set("Content-Type", "text/plain")
  27. }
  28. func Execute(opts *CostModelOpts) error {
  29. log.Infof("Starting cost-model version %s", version.FriendlyVersion())
  30. a := costmodel.Initialize()
  31. err := StartExportWorker(context.Background(), a.Model)
  32. if err != nil {
  33. log.Errorf("couldn't start CSV export worker: %v", err)
  34. }
  35. if env.IsCloudCostEnabled() {
  36. repo := cloudcost.NewMemoryRepository()
  37. a.CloudCostPipelineService = cloudcost.NewPipelineService(repo, a.CloudConfigController, cloudcost.DefaultIngestorConfiguration())
  38. repoQuerier := cloudcost.NewRepositoryQuerier(repo)
  39. a.CloudCostQueryService = cloudcost.NewQueryService(repoQuerier, repoQuerier)
  40. }
  41. rootMux := http.NewServeMux()
  42. a.Router.GET("/healthz", Healthz)
  43. a.Router.GET("/allocation", a.ComputeAllocationHandler)
  44. a.Router.GET("/allocation/summary", a.ComputeAllocationHandlerSummary)
  45. a.Router.GET("/assets", a.ComputeAssetsHandler)
  46. a.Router.GET("/cloudCost", a.CloudCostQueryService.GetCloudCostHandler())
  47. a.Router.GET("/cloudCost/view/graph", a.CloudCostQueryService.GetCloudCostViewGraphHandler())
  48. a.Router.GET("/cloudCost/view/totals", a.CloudCostQueryService.GetCloudCostViewTotalsHandler())
  49. a.Router.GET("/cloudCost/view/table", a.CloudCostQueryService.GetCloudCostViewTableHandler())
  50. a.Router.GET("/cloudCost/status", a.CloudCostPipelineService.GetCloudCostStatusHandler())
  51. a.Router.GET("/cloudCost/rebuild", a.CloudCostPipelineService.GetCloudCostRebuildHandler())
  52. a.Router.GET("/cloudCost/repair", a.CloudCostPipelineService.GetCloudCostRepairHandler())
  53. rootMux.Handle("/", a.Router)
  54. rootMux.Handle("/metrics", promhttp.Handler())
  55. telemetryHandler := metrics.ResponseMetricMiddleware(rootMux)
  56. handler := cors.AllowAll().Handler(telemetryHandler)
  57. return http.ListenAndServe(":9003", errors.PanicHandlerMiddleware(handler))
  58. }
  59. func StartExportWorker(ctx context.Context, model costmodel.AllocationModel) error {
  60. exportPath := env.GetExportCSVFile()
  61. if exportPath == "" {
  62. log.Infof("%s is not set, CSV export is disabled", env.ExportCSVFile)
  63. return nil
  64. }
  65. fm, err := filemanager.NewFileManager(exportPath)
  66. if err != nil {
  67. return fmt.Errorf("could not create file manager: %v", err)
  68. }
  69. go func() {
  70. log.Info("Starting CSV exporter worker...")
  71. // perform first update immediately
  72. nextRunAt := time.Now()
  73. for {
  74. select {
  75. case <-ctx.Done():
  76. return
  77. case <-time.After(nextRunAt.Sub(time.Now())):
  78. err := costmodel.UpdateCSV(ctx, fm, model, env.GetExportCSVLabelsAll(), env.GetExportCSVLabelsList())
  79. if err != nil {
  80. // it's background worker, log error and carry on, maybe next time it will work
  81. log.Errorf("Error updating CSV: %s", err)
  82. }
  83. now := time.Now().UTC()
  84. // next launch is at 00:10 UTC tomorrow
  85. // extra 10 minutes is to let prometheus to collect all the data for the previous day
  86. nextRunAt = time.Date(now.Year(), now.Month(), now.Day(), 0, 10, 0, 0, now.Location()).AddDate(0, 0, 1)
  87. }
  88. }
  89. }()
  90. return nil
  91. }