appdata.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package cat
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "github.com/newrelic/go-agent/internal/jsonx"
  7. )
  8. // AppDataHeader represents a decoded AppData header.
  9. type AppDataHeader struct {
  10. CrossProcessID string
  11. TransactionName string
  12. QueueTimeInSeconds float64
  13. ResponseTimeInSeconds float64
  14. ContentLength int64
  15. TransactionGUID string
  16. }
  17. var (
  18. errInvalidAppDataJSON = errors.New("invalid transaction data JSON")
  19. errInvalidAppDataCrossProcessID = errors.New("cross process ID is not a string")
  20. errInvalidAppDataTransactionName = errors.New("transaction name is not a string")
  21. errInvalidAppDataQueueTimeInSeconds = errors.New("queue time is not a float64")
  22. errInvalidAppDataResponseTimeInSeconds = errors.New("response time is not a float64")
  23. errInvalidAppDataContentLength = errors.New("content length is not a float64")
  24. errInvalidAppDataTransactionGUID = errors.New("transaction GUID is not a string")
  25. )
  26. // MarshalJSON marshalls an AppDataHeader as raw JSON.
  27. func (appData *AppDataHeader) MarshalJSON() ([]byte, error) {
  28. buf := bytes.NewBufferString("[")
  29. jsonx.AppendString(buf, appData.CrossProcessID)
  30. buf.WriteString(",")
  31. jsonx.AppendString(buf, appData.TransactionName)
  32. buf.WriteString(",")
  33. jsonx.AppendFloat(buf, appData.QueueTimeInSeconds)
  34. buf.WriteString(",")
  35. jsonx.AppendFloat(buf, appData.ResponseTimeInSeconds)
  36. buf.WriteString(",")
  37. jsonx.AppendInt(buf, appData.ContentLength)
  38. buf.WriteString(",")
  39. jsonx.AppendString(buf, appData.TransactionGUID)
  40. // The mysterious unused field. We don't need to round trip this, so we'll
  41. // just hardcode it to false.
  42. buf.WriteString(",false]")
  43. return buf.Bytes(), nil
  44. }
  45. // UnmarshalJSON unmarshalls an AppDataHeader from raw JSON.
  46. func (appData *AppDataHeader) UnmarshalJSON(data []byte) error {
  47. var ok bool
  48. var v interface{}
  49. if err := json.Unmarshal(data, &v); err != nil {
  50. return err
  51. }
  52. arr, ok := v.([]interface{})
  53. if !ok {
  54. return errInvalidAppDataJSON
  55. }
  56. if len(arr) < 7 {
  57. return errUnexpectedArraySize{
  58. label: "unexpected number of application data elements",
  59. expected: 7,
  60. actual: len(arr),
  61. }
  62. }
  63. if appData.CrossProcessID, ok = arr[0].(string); !ok {
  64. return errInvalidAppDataCrossProcessID
  65. }
  66. if appData.TransactionName, ok = arr[1].(string); !ok {
  67. return errInvalidAppDataTransactionName
  68. }
  69. if appData.QueueTimeInSeconds, ok = arr[2].(float64); !ok {
  70. return errInvalidAppDataQueueTimeInSeconds
  71. }
  72. if appData.ResponseTimeInSeconds, ok = arr[3].(float64); !ok {
  73. return errInvalidAppDataResponseTimeInSeconds
  74. }
  75. cl, ok := arr[4].(float64)
  76. if !ok {
  77. return errInvalidAppDataContentLength
  78. }
  79. // Content length is specced as int32, but not all agents are consistent on
  80. // this in practice. Let's handle it as int64 to maximise compatibility.
  81. appData.ContentLength = int64(cl)
  82. if appData.TransactionGUID, ok = arr[5].(string); !ok {
  83. return errInvalidAppDataTransactionGUID
  84. }
  85. // As above, we don't bother decoding the unused field here. It just has to
  86. // be present (which was checked earlier with the length check).
  87. return nil
  88. }