costmodel.go 2.7 KB

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