github.com/newrelic/go-agent@v3.26.0+incompatible/internal/error_events_test.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package internal 5 6 import ( 7 "encoding/json" 8 "testing" 9 "time" 10 ) 11 12 func testErrorEventJSON(t testing.TB, e *ErrorEvent, expect string) { 13 js, err := json.Marshal(e) 14 if nil != err { 15 t.Error(err) 16 return 17 } 18 expect = CompactJSONString(expect) 19 // Type assertion to support early Go versions. 20 if h, ok := t.(interface { 21 Helper() 22 }); ok { 23 h.Helper() 24 } 25 actual := string(js) 26 if expect != actual { 27 t.Errorf("\nexpect=%s\nactual=%s\n", expect, actual) 28 } 29 } 30 31 var ( 32 sampleErrorData = ErrorData{ 33 Klass: "*errors.errorString", 34 Msg: "hello", 35 When: time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC), 36 } 37 ) 38 39 func TestErrorEventMarshal(t *testing.T) { 40 testErrorEventJSON(t, &ErrorEvent{ 41 ErrorData: sampleErrorData, 42 TxnEvent: TxnEvent{ 43 FinalName: "myName", 44 Duration: 3 * time.Second, 45 Attrs: nil, 46 BetterCAT: BetterCAT{ 47 Enabled: true, 48 Priority: 0.5, 49 ID: "txn-guid-id", 50 }, 51 }, 52 }, `[ 53 { 54 "type":"TransactionError", 55 "error.class":"*errors.errorString", 56 "error.message":"hello", 57 "timestamp":1.41713646e+09, 58 "transactionName":"myName", 59 "duration":3, 60 "guid":"txn-guid-id", 61 "traceId":"txn-guid-id", 62 "priority":0.500000, 63 "sampled":false 64 }, 65 {}, 66 {} 67 ]`) 68 69 // Many error event intrinsics are shared with txn events using sharedEventIntrinsics: See 70 // the txn event tests. 71 } 72 73 func TestErrorEventMarshalOldCAT(t *testing.T) { 74 testErrorEventJSON(t, &ErrorEvent{ 75 ErrorData: sampleErrorData, 76 TxnEvent: TxnEvent{ 77 FinalName: "myName", 78 Duration: 3 * time.Second, 79 Attrs: nil, 80 BetterCAT: BetterCAT{ 81 Enabled: false, 82 }, 83 }, 84 }, `[ 85 { 86 "type":"TransactionError", 87 "error.class":"*errors.errorString", 88 "error.message":"hello", 89 "timestamp":1.41713646e+09, 90 "transactionName":"myName", 91 "duration":3 92 }, 93 {}, 94 {} 95 ]`) 96 97 // Many error event intrinsics are shared with txn events using sharedEventIntrinsics: See 98 // the txn event tests. 99 } 100 101 func TestErrorEventAttributes(t *testing.T) { 102 aci := sampleAttributeConfigInput 103 aci.ErrorCollector.Exclude = append(aci.ErrorCollector.Exclude, "zap") 104 aci.ErrorCollector.Exclude = append(aci.ErrorCollector.Exclude, AttributeHostDisplayName.name()) 105 cfg := CreateAttributeConfig(aci, true) 106 attr := NewAttributes(cfg) 107 attr.Agent.Add(AttributeHostDisplayName, "exclude me", nil) 108 attr.Agent.Add(attributeRequestMethod, "GET", nil) 109 AddUserAttribute(attr, "zap", 123, DestAll) 110 AddUserAttribute(attr, "zip", 456, DestAll) 111 112 testErrorEventJSON(t, &ErrorEvent{ 113 ErrorData: sampleErrorData, 114 TxnEvent: TxnEvent{ 115 FinalName: "myName", 116 Duration: 3 * time.Second, 117 Attrs: attr, 118 BetterCAT: BetterCAT{ 119 Enabled: true, 120 Priority: 0.5, 121 ID: "txn-guid-id", 122 }, 123 }, 124 }, `[ 125 { 126 "type":"TransactionError", 127 "error.class":"*errors.errorString", 128 "error.message":"hello", 129 "timestamp":1.41713646e+09, 130 "transactionName":"myName", 131 "duration":3, 132 "guid":"txn-guid-id", 133 "traceId":"txn-guid-id", 134 "priority":0.500000, 135 "sampled":false 136 }, 137 { 138 "zip":456 139 }, 140 { 141 "request.method":"GET" 142 } 143 ]`) 144 } 145 146 func TestErrorEventAttributesOldCAT(t *testing.T) { 147 aci := sampleAttributeConfigInput 148 aci.ErrorCollector.Exclude = append(aci.ErrorCollector.Exclude, "zap") 149 aci.ErrorCollector.Exclude = append(aci.ErrorCollector.Exclude, AttributeHostDisplayName.name()) 150 cfg := CreateAttributeConfig(aci, true) 151 attr := NewAttributes(cfg) 152 attr.Agent.Add(AttributeHostDisplayName, "exclude me", nil) 153 attr.Agent.Add(attributeRequestMethod, "GET", nil) 154 AddUserAttribute(attr, "zap", 123, DestAll) 155 AddUserAttribute(attr, "zip", 456, DestAll) 156 157 testErrorEventJSON(t, &ErrorEvent{ 158 ErrorData: sampleErrorData, 159 TxnEvent: TxnEvent{ 160 FinalName: "myName", 161 Duration: 3 * time.Second, 162 Attrs: attr, 163 BetterCAT: BetterCAT{ 164 Enabled: false, 165 }, 166 }, 167 }, `[ 168 { 169 "type":"TransactionError", 170 "error.class":"*errors.errorString", 171 "error.message":"hello", 172 "timestamp":1.41713646e+09, 173 "transactionName":"myName", 174 "duration":3 175 }, 176 { 177 "zip":456 178 }, 179 { 180 "request.method":"GET" 181 } 182 ]`) 183 } 184 185 func TestErrorEventMarshalWithInboundCaller(t *testing.T) { 186 e := TxnEvent{ 187 FinalName: "myName", 188 Duration: 3 * time.Second, 189 Attrs: nil, 190 } 191 192 e.BetterCAT.Enabled = true 193 e.BetterCAT.Inbound = &Payload{ 194 payloadCaller: payloadCaller{ 195 TransportType: "HTTP", 196 Type: "Browser", 197 App: "caller-app", 198 Account: "caller-account", 199 }, 200 ID: "caller-id", 201 TransactionID: "caller-parent-id", 202 TracedID: "trip-id", 203 TransportDuration: 2 * time.Second, 204 } 205 206 testErrorEventJSON(t, &ErrorEvent{ 207 ErrorData: sampleErrorData, 208 TxnEvent: e, 209 }, `[ 210 { 211 "type":"TransactionError", 212 "error.class":"*errors.errorString", 213 "error.message":"hello", 214 "timestamp":1.41713646e+09, 215 "transactionName":"myName", 216 "duration":3, 217 "parent.type": "Browser", 218 "parent.app": "caller-app", 219 "parent.account": "caller-account", 220 "parent.transportType": "HTTP", 221 "parent.transportDuration": 2, 222 "guid":"", 223 "traceId":"trip-id", 224 "priority":0.000000, 225 "sampled":false 226 }, 227 {}, 228 {} 229 ]`) 230 }