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