1
0

payload.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package sessions
  2. import (
  3. "runtime"
  4. "time"
  5. "github.com/bugsnag/bugsnag-go/device"
  6. )
  7. // notifierPayload defines the .notifier subobject of the payload
  8. type notifierPayload struct {
  9. Name string `json:"name"`
  10. URL string `json:"url"`
  11. Version string `json:"version"`
  12. }
  13. // appPayload defines the .app subobject of the payload
  14. type appPayload struct {
  15. Type string `json:"type,omitempty"`
  16. ReleaseStage string `json:"releaseStage,omitempty"`
  17. Version string `json:"version,omitempty"`
  18. }
  19. // devicePayload defines the .device subobject of the payload
  20. type devicePayload struct {
  21. OsName string `json:"osName,omitempty"`
  22. Hostname string `json:"hostname,omitempty"`
  23. }
  24. // sessionCountsPayload defines the .sessionCounts subobject of the payload
  25. type sessionCountsPayload struct {
  26. StartedAt string `json:"startedAt"`
  27. SessionsStarted int `json:"sessionsStarted"`
  28. }
  29. // sessionPayload defines the top level payload object
  30. type sessionPayload struct {
  31. Notifier *notifierPayload `json:"notifier"`
  32. App *appPayload `json:"app"`
  33. Device *devicePayload `json:"device"`
  34. SessionCounts []sessionCountsPayload `json:"sessionCounts"`
  35. }
  36. // makeSessionPayload creates a sessionPayload based off of the given sessions and config
  37. func makeSessionPayload(sessions []*Session, config *SessionTrackingConfiguration) *sessionPayload {
  38. releaseStage := config.ReleaseStage
  39. if releaseStage == "" {
  40. releaseStage = "production"
  41. }
  42. hostname := config.Hostname
  43. if hostname == "" {
  44. hostname = device.GetHostname()
  45. }
  46. return &sessionPayload{
  47. Notifier: &notifierPayload{
  48. Name: "Bugsnag Go",
  49. URL: "https://github.com/bugsnag/bugsnag-go",
  50. Version: config.Version,
  51. },
  52. App: &appPayload{
  53. Type: config.AppType,
  54. Version: config.AppVersion,
  55. ReleaseStage: releaseStage,
  56. },
  57. Device: &devicePayload{
  58. OsName: runtime.GOOS,
  59. Hostname: hostname,
  60. },
  61. SessionCounts: []sessionCountsPayload{
  62. {
  63. //This timestamp assumes that we're sending these off once a minute
  64. StartedAt: sessions[0].StartedAt.UTC().Format(time.RFC3339),
  65. SessionsStarted: len(sessions),
  66. },
  67. },
  68. }
  69. }