service_check.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. }
  52. // Encode returns the dogstatsd wire protocol representation for a service check.
  53. // Tags may be passed which will be added to the encoded output but not to
  54. // the Service Check's list of tags, eg. for default tags.
  55. func (sc ServiceCheck) Encode(tags ...string) (string, error) {
  56. err := sc.Check()
  57. if err != nil {
  58. return "", err
  59. }
  60. var buffer []byte
  61. buffer = appendServiceCheck(buffer, sc, tags)
  62. return string(buffer), nil
  63. }