source.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package exporter
  2. import (
  3. "context"
  4. "time"
  5. "github.com/opencost/opencost/core/pkg/diagnostics"
  6. )
  7. // DiagnosticSource is an `export.ExportSource` implementation that provides the basic data for a `DiagnosticResult` payload.
  8. type DiagnosticSource struct {
  9. diagnosticService diagnostics.DiagnosticService
  10. }
  11. // NewDiagnosticSource creates a new `DiagnosticSource` instance. It accepts the `DiagnosticService` implementation
  12. // that will be used to retrieve the diagnostic results.
  13. func NewDiagnosticSource(diagnosticService diagnostics.DiagnosticService) *DiagnosticSource {
  14. return &DiagnosticSource{
  15. diagnosticService: diagnosticService,
  16. }
  17. }
  18. // Make creates a new `DiagnosticsRunReport` instance with the provided current time.
  19. func (ds *DiagnosticSource) Make(t time.Time) *diagnostics.DiagnosticsRunReport {
  20. ctx := context.Background()
  21. // returning nil will prevent export -- skip for 0 registered diagnostics
  22. if ds.diagnosticService.Total() == 0 {
  23. return nil
  24. }
  25. return &diagnostics.DiagnosticsRunReport{
  26. StartTime: t,
  27. Results: ds.diagnosticService.Run(ctx),
  28. }
  29. }
  30. func (ds *DiagnosticSource) Name() string {
  31. return diagnostics.DiagnosticsEventName + "-source"
  32. }