diagnostics.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // Application contains the name of the application that the diagnostics run belongs to.
  31. Application string `json:"application"`
  32. // StartTime contains the time when the full diagnostics run started
  33. StartTime time.Time `json:"startTime"`
  34. // Results contains all of the results of the diagnostics run.
  35. Results []*DiagnosticResult `json:"results"`
  36. }
  37. // DiagnosticRunner is a function that executes a diagnostic and returns the result. The function should return a map containing
  38. // any additional information about the diagnostic run, and a detailed error if the run failed.
  39. type DiagnosticRunner func(context.Context) (map[string]any, error)
  40. // Diagnostic is a struct that contains the basic information about a registed diagnostic within a DiagnosticService.
  41. type Diagnostic struct {
  42. // Name of the diagnostic that is registered.
  43. Name string
  44. // Description of the diagnostic that is registered.
  45. Description string
  46. // Category of the diagnostic that is registered.
  47. Category string
  48. }
  49. // DiagnosticService is an interface that defines the basic contract for a service that registers and runs diagnostics on demand and provides
  50. // the results.
  51. type DiagnosticService interface {
  52. // Register registers a new diagnostic runner implementation with the service that will run the next time diagnostics are requested.
  53. // An error is returned if a runner failed to register. Note that category _and_ name must be a unique combination.
  54. Register(name, description, category string, runner DiagnosticRunner) error
  55. // Unregister unregisters a diagnostic runner implementation with the service. True is returned if the runner was unregistered successfully,
  56. // false otherwise.
  57. Unregister(name, category string) bool
  58. // Run executes all registered diagnostics and returns the results.
  59. Run(ctx context.Context) []*DiagnosticResult
  60. // RunCategory executes all registered diagnostics in the provided category.
  61. RunCategory(ctx context.Context, category string) []*DiagnosticResult
  62. // RunDiagnostic executes a specific diagnostic by category and name. If the diagnostic does not exist, nil is returned.
  63. RunDiagnostic(ctx context.Context, category, name string) *DiagnosticResult
  64. // Diagnostics returns a list of all registered diagnostics.
  65. Diagnostics() []Diagnostic
  66. // Total returns the total number of registered diagnostics.
  67. Total() int
  68. }