diagnostics.go 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package diagnostics
  2. import (
  3. "context"
  4. "time"
  5. )
  6. // DiagnosticsEventName is used to represent the name of the diagnostics export pipeline event to categorize for storage.
  7. const DiagnosticsEventName string = "diagnostics"
  8. // DiagnosticResult represent the result of a diagnostic run, and contains basic diagnostic information and additional
  9. // custom diagnostic information appended by the specific runner.
  10. type DiagnosticResult struct {
  11. // Unique Identifier for the diagnostic run result.
  12. ID string `json:"id"`
  13. // Name of the diagnostic that ran.
  14. Name string `json:"name"`
  15. // Description of the diagnostic run, human readable description of what the diagnostic shows.
  16. Description string `json:"description"`
  17. // Category of the diagnostic run, which can be used to group similar diagnostics together.
  18. Category string `json:"category"`
  19. // Timestamp containing the time when the diagnostic run was executed.
  20. Timestamp time.Time `json:"timestamp"`
  21. // Error message if the diagnostic run failed. If this field is non-empty, the diagnostic run should be
  22. // considered a failure.
  23. Error string `json:"error,omitempty"`
  24. // Details contains additional custom information about the diagnostic run that can be added by the diagnostic
  25. // runner.
  26. Details map[string]any `json:"details,omitempty"`
  27. }
  28. // DiagnosticsRunReport is a struct that contains the start time of the diagnostics run, and all of the results.
  29. type DiagnosticsRunReport struct {
  30. // StartTime contains the time when the full diagnostics run started
  31. StartTime time.Time `json:"startTime"`
  32. // Results contains all of the results of the diagnostics run.
  33. Results []*DiagnosticResult `json:"results"`
  34. }
  35. // DiagnosticRunner is a function that executes a diagnostic and returns the result. The function should return a map containing
  36. // any additional information about the diagnostic run, and a detailed error if the run failed.
  37. type DiagnosticRunner func(context.Context) (map[string]any, error)
  38. // Diagnostic is a struct that contains the basic information about a registed diagnostic within a DiagnosticService.
  39. type Diagnostic struct {
  40. // Name of the diagnostic that is registered.
  41. Name string
  42. // Description of the diagnostic that is registered.
  43. Description string
  44. // Category of the diagnostic that is registered.
  45. Category string
  46. }
  47. // DiagnosticService is an interface that defines the basic contract for a service that registers and runs diagnostics on demand and provides
  48. // the results.
  49. type DiagnosticService interface {
  50. // Register registers a new diagnostic runner implementation with the service that will run the next time diagnostics are requested.
  51. // An error is returned if a runner failed to register. Note that category _and_ name must be a unique combination.
  52. Register(name, description, category string, runner DiagnosticRunner) error
  53. // Unregister unregisters a diagnostic runner implementation with the service. True is returned if the runner was unregistered successfully,
  54. // false otherwise.
  55. Unregister(name, category string) bool
  56. // Run executes all registered diagnostics and returns the results.
  57. Run(ctx context.Context) []*DiagnosticResult
  58. // RunCategory executes all registered diagnostics in the provided category.
  59. RunCategory(ctx context.Context, category string) []*DiagnosticResult
  60. // RunDiagnostic executes a specific diagnostic by category and name. If the diagnostic does not exist, nil is returned.
  61. RunDiagnostic(ctx context.Context, category, name string) *DiagnosticResult
  62. // Diagnostics returns a list of all registered diagnostics.
  63. Diagnostics() []Diagnostic
  64. // Total returns the total number of registered diagnostics.
  65. Total() int
  66. }