costmodel.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/core/pkg/util/json"
  10. "github.com/opencost/opencost/pkg/cloud/models"
  11. "github.com/opencost/opencost/pkg/cloud/provider"
  12. "github.com/opencost/opencost/pkg/customcost"
  13. "github.com/prometheus/client_golang/prometheus/promhttp"
  14. "github.com/rs/cors"
  15. "github.com/opencost/opencost/core/pkg/errors"
  16. "github.com/opencost/opencost/core/pkg/log"
  17. "github.com/opencost/opencost/core/pkg/version"
  18. "github.com/opencost/opencost/pkg/costmodel"
  19. "github.com/opencost/opencost/pkg/env"
  20. "github.com/opencost/opencost/pkg/filemanager"
  21. "github.com/opencost/opencost/pkg/metrics"
  22. )
  23. // CostModelOpts contain configuration options that can be passed to the Execute() method
  24. type CostModelOpts struct {
  25. // Stubbed for future configuration
  26. }
  27. func Healthz(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
  28. w.WriteHeader(200)
  29. w.Header().Set("Content-Length", "0")
  30. w.Header().Set("Content-Type", "text/plain")
  31. }
  32. func Execute(opts *CostModelOpts) error {
  33. log.Infof("Starting cost-model version %s", version.FriendlyVersion())
  34. log.Infof("Kubernetes enabled: %t", env.IsKubernetesEnabled())
  35. router := httprouter.New()
  36. var a *costmodel.Accesses
  37. var cp models.Provider
  38. if env.IsKubernetesEnabled() {
  39. a = costmodel.Initialize(router)
  40. err := StartExportWorker(context.Background(), a.Model)
  41. if err != nil {
  42. log.Errorf("couldn't start CSV export worker: %v", err)
  43. }
  44. // Register OpenCost Specific Endpoints
  45. router.GET("/allocation", a.ComputeAllocationHandler)
  46. router.GET("/allocation/summary", a.ComputeAllocationHandlerSummary)
  47. router.GET("/assets", a.ComputeAssetsHandler)
  48. if env.IsCarbonEstimatesEnabled() {
  49. router.GET("/assets/carbon", a.ComputeAssetsCarbonHandler)
  50. }
  51. // set cloud provider for cloud cost
  52. cp = a.CloudProvider
  53. }
  54. log.Infof("Cloud Costs enabled: %t", env.IsCloudCostEnabled())
  55. if env.IsCloudCostEnabled() {
  56. var providerConfig models.ProviderConfig
  57. if cp != nil {
  58. providerConfig = provider.ExtractConfigFromProviders(cp)
  59. }
  60. costmodel.InitializeCloudCost(router, providerConfig)
  61. }
  62. log.Infof("Custom Costs enabled: %t", env.IsCustomCostEnabled())
  63. var customCostPipelineService *customcost.PipelineService
  64. if env.IsCustomCostEnabled() {
  65. customCostPipelineService = costmodel.InitializeCustomCost(router)
  66. }
  67. // this endpoint is intentionally left out of the "if env.IsCustomCostEnabled()" conditional; in the handler, it is
  68. // valid for CustomCostPipelineService to be nil
  69. router.GET("/customCost/status", customCostPipelineService.GetCustomCostStatusHandler())
  70. router.GET("/healthz", Healthz)
  71. router.GET("/logs/level", GetLogLevel)
  72. router.POST("/logs/level", SetLogLevel)
  73. if env.IsPProfEnabled() {
  74. router.HandlerFunc(http.MethodGet, "/debug/pprof/", pprof.Index)
  75. router.HandlerFunc(http.MethodGet, "/debug/pprof/cmdline", pprof.Cmdline)
  76. router.HandlerFunc(http.MethodGet, "/debug/pprof/profile", pprof.Profile)
  77. router.HandlerFunc(http.MethodGet, "/debug/pprof/symbol", pprof.Symbol)
  78. router.HandlerFunc(http.MethodGet, "/debug/pprof/trace", pprof.Trace)
  79. router.Handler(http.MethodGet, "/debug/pprof/goroutine", pprof.Handler("goroutine"))
  80. router.Handler(http.MethodGet, "/debug/pprof/heap", pprof.Handler("heap"))
  81. }
  82. rootMux := http.NewServeMux()
  83. rootMux.Handle("/", router)
  84. rootMux.Handle("/metrics", promhttp.Handler())
  85. telemetryHandler := metrics.ResponseMetricMiddleware(rootMux)
  86. handler := cors.AllowAll().Handler(telemetryHandler)
  87. return http.ListenAndServe(fmt.Sprint(":", env.GetAPIPort()), errors.PanicHandlerMiddleware(handler))
  88. }
  89. func StartExportWorker(ctx context.Context, model costmodel.AllocationModel) error {
  90. exportPath := env.GetExportCSVFile()
  91. if exportPath == "" {
  92. log.Infof("%s is not set, CSV export is disabled", env.ExportCSVFile)
  93. return nil
  94. }
  95. fm, err := filemanager.NewFileManager(exportPath)
  96. if err != nil {
  97. return fmt.Errorf("could not create file manager: %v", err)
  98. }
  99. go func() {
  100. log.Info("Starting CSV exporter worker...")
  101. // perform first update immediately
  102. nextRunAt := time.Now()
  103. for {
  104. select {
  105. case <-ctx.Done():
  106. return
  107. case <-time.After(nextRunAt.Sub(time.Now())):
  108. err := costmodel.UpdateCSV(ctx, fm, model, env.GetExportCSVLabelsAll(), env.GetExportCSVLabelsList())
  109. if err != nil {
  110. // it's background worker, log error and carry on, maybe next time it will work
  111. log.Errorf("Error updating CSV: %s", err)
  112. }
  113. now := time.Now().UTC()
  114. // next launch is at 00:10 UTC tomorrow
  115. // extra 10 minutes is to let prometheus to collect all the data for the previous day
  116. nextRunAt = time.Date(now.Year(), now.Month(), now.Day(), 0, 10, 0, 0, now.Location()).AddDate(0, 0, 1)
  117. }
  118. }
  119. }()
  120. return nil
  121. }
  122. type LogLevelRequestResponse struct {
  123. Level string `json:"level"`
  124. }
  125. func GetLogLevel(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  126. w.Header().Set("Content-Type", "application/json")
  127. w.Header().Set("Access-Control-Allow-Origin", "*")
  128. level := log.GetLogLevel()
  129. llrr := LogLevelRequestResponse{
  130. Level: level,
  131. }
  132. body, err := json.Marshal(llrr)
  133. if err != nil {
  134. http.Error(w, fmt.Sprintf("unable to retrive log level"), http.StatusInternalServerError)
  135. return
  136. }
  137. _, err = w.Write(body)
  138. if err != nil {
  139. http.Error(w, fmt.Sprintf("unable to write response: %s", body), http.StatusInternalServerError)
  140. return
  141. }
  142. }
  143. func SetLogLevel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  144. params := LogLevelRequestResponse{}
  145. err := json.NewDecoder(r.Body).Decode(&params)
  146. if err != nil {
  147. http.Error(w, fmt.Sprintf("unable to decode request body, error: %s", err), http.StatusBadRequest)
  148. return
  149. }
  150. err = log.SetLogLevel(params.Level)
  151. if err != nil {
  152. http.Error(w, fmt.Sprintf("level must be a valid log level according to zerolog; level given: %s, error: %s", params.Level, err), http.StatusBadRequest)
  153. return
  154. }
  155. w.WriteHeader(http.StatusOK)
  156. }