source.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. applicationName string
  10. diagnosticService diagnostics.DiagnosticService
  11. }
  12. // NewDiagnosticSource creates a new `DiagnosticSource` instance. It accepts the `DiagnosticService` implementation
  13. // that will be used to retrieve the diagnostic results.
  14. func NewDiagnosticSource(applicationName string, diagnosticService diagnostics.DiagnosticService) *DiagnosticSource {
  15. return &DiagnosticSource{
  16. applicationName: applicationName,
  17. diagnosticService: diagnosticService,
  18. }
  19. }
  20. // Make creates a new `DiagnosticsRunReport` instance with the provided current time.
  21. func (ds *DiagnosticSource) Make(t time.Time) *diagnostics.DiagnosticsRunReport {
  22. ctx := context.Background()
  23. // returning nil will prevent export -- skip for 0 registered diagnostics
  24. if ds.diagnosticService.Total() == 0 {
  25. return nil
  26. }
  27. return &diagnostics.DiagnosticsRunReport{
  28. StartTime: t,
  29. Application: ds.applicationName,
  30. Results: ds.diagnosticService.Run(ctx),
  31. }
  32. }
  33. func (ds *DiagnosticSource) Name() string {
  34. return diagnostics.DiagnosticsEventName + "-source"
  35. }