diagnostic.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package kubemodel
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/opencost/opencost/core/pkg/log"
  6. )
  7. // @bingen:generate:DiagnosticLevel
  8. type DiagnosticLevel int
  9. const (
  10. DiagnosticLevelTrace DiagnosticLevel = iota
  11. DiagnosticLevelDebug
  12. DiagnosticLevelInfo
  13. DiagnosticLevelWarning
  14. DiagnosticLevelError
  15. )
  16. const DefaultDiagnosticLevel = DiagnosticLevelInfo
  17. // @bingen:generate:Diagnostic
  18. type Diagnostic struct {
  19. Timestamp time.Time `json:"timestamp"` // @bingen:field[version=1]
  20. Level DiagnosticLevel `json:"level"` // @bingen:field[version=1]
  21. Message string `json:"message"` // @bingen:field[version=1]
  22. Details map[string]string `json:"details,omitempty"` // @bingen:field[version=1]
  23. }
  24. func (kms *KubeModelSet) RegisterDiagnostic(d Diagnostic) {
  25. kms.Metadata.Diagnostics = append(kms.Metadata.Diagnostics, d)
  26. }
  27. func (kms *KubeModelSet) GetErrors() []Diagnostic {
  28. ds := []Diagnostic{}
  29. for _, d := range kms.Metadata.Diagnostics {
  30. if d.Level == DiagnosticLevelError {
  31. ds = append(ds, d)
  32. }
  33. }
  34. return ds
  35. }
  36. func (kms *KubeModelSet) Errorf(msg string, a ...any) {
  37. kms.Error(fmt.Errorf(msg, a...))
  38. }
  39. func (kms *KubeModelSet) Error(err error) {
  40. if err == nil {
  41. return
  42. }
  43. log.Error(fmt.Sprintf("KubeModel: %s", err))
  44. kms.RegisterDiagnostic(Diagnostic{
  45. Timestamp: time.Now().UTC(),
  46. Level: DiagnosticLevelError,
  47. Message: err.Error(),
  48. })
  49. }
  50. func (kms *KubeModelSet) GetWarnings() []Diagnostic {
  51. ds := []Diagnostic{}
  52. for _, d := range kms.Metadata.Diagnostics {
  53. if d.Level == DiagnosticLevelWarning {
  54. ds = append(ds, d)
  55. }
  56. }
  57. return ds
  58. }
  59. func (kms *KubeModelSet) Warnf(msg string, a ...any) {
  60. kms.Warn(fmt.Sprintf(msg, a...))
  61. }
  62. func (kms *KubeModelSet) Warn(msg string) {
  63. if kms.Metadata.DiagnosticLevel > DiagnosticLevelWarning {
  64. return
  65. }
  66. log.Warn(fmt.Sprintf("KubeModel: %s", msg))
  67. kms.RegisterDiagnostic(Diagnostic{
  68. Timestamp: time.Now().UTC(),
  69. Level: DiagnosticLevelWarning,
  70. Message: msg,
  71. })
  72. }
  73. func (kms *KubeModelSet) GetInfos() []Diagnostic {
  74. ds := []Diagnostic{}
  75. for _, d := range kms.Metadata.Diagnostics {
  76. if d.Level == DiagnosticLevelInfo {
  77. ds = append(ds, d)
  78. }
  79. }
  80. return ds
  81. }
  82. func (kms *KubeModelSet) Infof(msg string, a ...any) {
  83. kms.Info(fmt.Sprintf(msg, a...))
  84. }
  85. func (kms *KubeModelSet) Info(msg string) {
  86. if kms.Metadata.DiagnosticLevel > DiagnosticLevelInfo {
  87. return
  88. }
  89. log.Info(fmt.Sprintf("KubeModel: %s", msg))
  90. kms.RegisterDiagnostic(Diagnostic{
  91. Timestamp: time.Now().UTC(),
  92. Level: DiagnosticLevelInfo,
  93. Message: msg,
  94. })
  95. }
  96. func (kms *KubeModelSet) GetDebugs() []Diagnostic {
  97. ds := []Diagnostic{}
  98. for _, d := range kms.Metadata.Diagnostics {
  99. if d.Level == DiagnosticLevelDebug {
  100. ds = append(ds, d)
  101. }
  102. }
  103. return ds
  104. }
  105. func (kms *KubeModelSet) Debugf(msg string, a ...any) {
  106. kms.Debug(fmt.Sprintf(msg, a...))
  107. }
  108. func (kms *KubeModelSet) Debug(msg string) {
  109. if kms.Metadata.DiagnosticLevel > DiagnosticLevelDebug {
  110. return
  111. }
  112. log.Debug(fmt.Sprintf("KubeModel: %s", msg))
  113. kms.RegisterDiagnostic(Diagnostic{
  114. Timestamp: time.Now().UTC(),
  115. Level: DiagnosticLevelDebug,
  116. Message: msg,
  117. })
  118. }
  119. func (kms *KubeModelSet) GetTraces() []Diagnostic {
  120. ds := []Diagnostic{}
  121. for _, d := range kms.Metadata.Diagnostics {
  122. if d.Level == DiagnosticLevelTrace {
  123. ds = append(ds, d)
  124. }
  125. }
  126. return ds
  127. }
  128. func (kms *KubeModelSet) Tracef(msg string, a ...any) {
  129. kms.Trace(fmt.Sprintf(msg, a...))
  130. }
  131. func (kms *KubeModelSet) Trace(msg string) {
  132. if kms.Metadata.DiagnosticLevel > DiagnosticLevelTrace {
  133. return
  134. }
  135. log.Trace(fmt.Sprintf("KubeModel: %s", msg))
  136. kms.RegisterDiagnostic(Diagnostic{
  137. Timestamp: time.Now().UTC(),
  138. Level: DiagnosticLevelTrace,
  139. Message: msg,
  140. })
  141. }