service_check.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package statsd
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. // ServiceCheckStatus support
  7. type ServiceCheckStatus byte
  8. const (
  9. // Ok is the "ok" ServiceCheck status
  10. Ok ServiceCheckStatus = 0
  11. // Warn is the "warning" ServiceCheck status
  12. Warn ServiceCheckStatus = 1
  13. // Critical is the "critical" ServiceCheck status
  14. Critical ServiceCheckStatus = 2
  15. // Unknown is the "unknown" ServiceCheck status
  16. Unknown ServiceCheckStatus = 3
  17. )
  18. // A ServiceCheck is an object that contains status of DataDog service check.
  19. type ServiceCheck struct {
  20. // Name of the service check. Required.
  21. Name string
  22. // Status of service check. Required.
  23. Status ServiceCheckStatus
  24. // Timestamp is a timestamp for the serviceCheck. If not provided, the dogstatsd
  25. // server will set this to the current time.
  26. Timestamp time.Time
  27. // Hostname for the serviceCheck.
  28. Hostname string
  29. // A message describing the current state of the serviceCheck.
  30. Message string
  31. // Tags for the serviceCheck.
  32. Tags []string
  33. }
  34. // NewServiceCheck creates a new serviceCheck with the given name and status. Error checking
  35. // against these values is done at send-time, or upon running sc.Check.
  36. func NewServiceCheck(name string, status ServiceCheckStatus) *ServiceCheck {
  37. return &ServiceCheck{
  38. Name: name,
  39. Status: status,
  40. }
  41. }
  42. // Check verifies that a service check is valid.
  43. func (sc *ServiceCheck) Check() error {
  44. if len(sc.Name) == 0 {
  45. return fmt.Errorf("statsd.ServiceCheck name is required")
  46. }
  47. if byte(sc.Status) < 0 || byte(sc.Status) > 3 {
  48. return fmt.Errorf("statsd.ServiceCheck status has invalid value")
  49. }
  50. return nil
  51. }