errors.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package internal
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "github.com/newrelic/go-agent/internal/jsonx"
  9. )
  10. const (
  11. // PanicErrorKlass is the error klass used for errors generated by
  12. // recovering panics in txn.End.
  13. PanicErrorKlass = "panic"
  14. )
  15. func panicValueMsg(v interface{}) string {
  16. switch val := v.(type) {
  17. case error:
  18. return val.Error()
  19. default:
  20. return fmt.Sprintf("%v", v)
  21. }
  22. }
  23. // TxnErrorFromPanic creates a new TxnError from a panic.
  24. func TxnErrorFromPanic(now time.Time, v interface{}) ErrorData {
  25. return ErrorData{
  26. When: now,
  27. Msg: panicValueMsg(v),
  28. Klass: PanicErrorKlass,
  29. }
  30. }
  31. // TxnErrorFromResponseCode creates a new TxnError from an http response code.
  32. func TxnErrorFromResponseCode(now time.Time, code int) ErrorData {
  33. return ErrorData{
  34. When: now,
  35. Msg: http.StatusText(code),
  36. Klass: strconv.Itoa(code),
  37. }
  38. }
  39. // ErrorData contains the information about a recorded error.
  40. type ErrorData struct {
  41. When time.Time
  42. Stack StackTrace
  43. ExtraAttributes map[string]interface{}
  44. Msg string
  45. Klass string
  46. }
  47. // TxnError combines error data with information about a transaction. TxnError is used for
  48. // both error events and traced errors.
  49. type TxnError struct {
  50. ErrorData
  51. TxnEvent
  52. }
  53. // ErrorEvent and tracedError are separate types so that error events and traced errors can have
  54. // different WriteJSON methods.
  55. type ErrorEvent TxnError
  56. type tracedError TxnError
  57. // TxnErrors is a set of errors captured in a Transaction.
  58. type TxnErrors []*ErrorData
  59. // NewTxnErrors returns a new empty TxnErrors.
  60. func NewTxnErrors(max int) TxnErrors {
  61. return make([]*ErrorData, 0, max)
  62. }
  63. // Add adds a TxnError.
  64. func (errors *TxnErrors) Add(e ErrorData) {
  65. if len(*errors) < cap(*errors) {
  66. *errors = append(*errors, &e)
  67. }
  68. }
  69. func (h *tracedError) WriteJSON(buf *bytes.Buffer) {
  70. buf.WriteByte('[')
  71. jsonx.AppendFloat(buf, timeToFloatMilliseconds(h.When))
  72. buf.WriteByte(',')
  73. jsonx.AppendString(buf, h.FinalName)
  74. buf.WriteByte(',')
  75. jsonx.AppendString(buf, h.Msg)
  76. buf.WriteByte(',')
  77. jsonx.AppendString(buf, h.Klass)
  78. buf.WriteByte(',')
  79. buf.WriteByte('{')
  80. buf.WriteString(`"agentAttributes"`)
  81. buf.WriteByte(':')
  82. agentAttributesJSON(h.Attrs, buf, destError)
  83. buf.WriteByte(',')
  84. buf.WriteString(`"userAttributes"`)
  85. buf.WriteByte(':')
  86. userAttributesJSON(h.Attrs, buf, destError, h.ErrorData.ExtraAttributes)
  87. buf.WriteByte(',')
  88. buf.WriteString(`"intrinsics"`)
  89. buf.WriteByte(':')
  90. intrinsicsJSON(&h.TxnEvent, buf)
  91. if nil != h.Stack {
  92. buf.WriteByte(',')
  93. buf.WriteString(`"stack_trace"`)
  94. buf.WriteByte(':')
  95. h.Stack.WriteJSON(buf)
  96. }
  97. if h.CleanURL != "" {
  98. buf.WriteByte(',')
  99. buf.WriteString(`"request_uri"`)
  100. buf.WriteByte(':')
  101. jsonx.AppendString(buf, h.CleanURL)
  102. }
  103. buf.WriteByte('}')
  104. buf.WriteByte(']')
  105. }
  106. // MarshalJSON is used for testing.
  107. func (h *tracedError) MarshalJSON() ([]byte, error) {
  108. buf := &bytes.Buffer{}
  109. h.WriteJSON(buf)
  110. return buf.Bytes(), nil
  111. }
  112. type harvestErrors []*tracedError
  113. func newHarvestErrors(max int) harvestErrors {
  114. return make([]*tracedError, 0, max)
  115. }
  116. // MergeTxnErrors merges a transaction's errors into the harvest's errors.
  117. func MergeTxnErrors(errors *harvestErrors, errs TxnErrors, txnEvent TxnEvent) {
  118. for _, e := range errs {
  119. if len(*errors) == cap(*errors) {
  120. return
  121. }
  122. *errors = append(*errors, &tracedError{
  123. TxnEvent: txnEvent,
  124. ErrorData: *e,
  125. })
  126. }
  127. }
  128. func (errors harvestErrors) Data(agentRunID string, harvestStart time.Time) ([]byte, error) {
  129. if 0 == len(errors) {
  130. return nil, nil
  131. }
  132. estimate := 1024 * len(errors)
  133. buf := bytes.NewBuffer(make([]byte, 0, estimate))
  134. buf.WriteByte('[')
  135. jsonx.AppendString(buf, agentRunID)
  136. buf.WriteByte(',')
  137. buf.WriteByte('[')
  138. for i, e := range errors {
  139. if i > 0 {
  140. buf.WriteByte(',')
  141. }
  142. e.WriteJSON(buf)
  143. }
  144. buf.WriteByte(']')
  145. buf.WriteByte(']')
  146. return buf.Bytes(), nil
  147. }
  148. func (errors harvestErrors) MergeIntoHarvest(h *Harvest) {}
  149. func (errors harvestErrors) EndpointMethod() string {
  150. return cmdErrorData
  151. }