github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/error_events_test.go (about) 1 package internal 2 3 import ( 4 "encoding/json" 5 "testing" 6 "time" 7 ) 8 9 func testErrorEventJSON(t *testing.T, e *ErrorEvent, expect string) { 10 js, err := json.Marshal(e) 11 if nil != err { 12 t.Error(err) 13 return 14 } 15 expect = CompactJSONString(expect) 16 if string(js) != expect { 17 t.Error(string(js), expect) 18 } 19 } 20 21 var ( 22 sampleErrorData = ErrorData{ 23 Klass: "*errors.errorString", 24 Msg: "hello", 25 When: time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC), 26 } 27 ) 28 29 func TestErrorEventMarshal(t *testing.T) { 30 testErrorEventJSON(t, &ErrorEvent{ 31 ErrorData: sampleErrorData, 32 TxnEvent: TxnEvent{ 33 FinalName: "myName", 34 Duration: 3 * time.Second, 35 Attrs: nil, 36 }, 37 }, `[ 38 { 39 "type":"TransactionError", 40 "error.class":"*errors.errorString", 41 "error.message":"hello", 42 "timestamp":1.41713646e+09, 43 "transactionName":"myName", 44 "duration":3 45 }, 46 {}, 47 {} 48 ]`) 49 testErrorEventJSON(t, &ErrorEvent{ 50 ErrorData: sampleErrorData, 51 TxnEvent: TxnEvent{ 52 FinalName: "myName", 53 Duration: 3 * time.Second, 54 Queuing: 5 * time.Second, 55 Attrs: nil, 56 }, 57 }, `[ 58 { 59 "type":"TransactionError", 60 "error.class":"*errors.errorString", 61 "error.message":"hello", 62 "timestamp":1.41713646e+09, 63 "transactionName":"myName", 64 "duration":3, 65 "queueDuration":5 66 }, 67 {}, 68 {} 69 ]`) 70 testErrorEventJSON(t, &ErrorEvent{ 71 ErrorData: sampleErrorData, 72 TxnEvent: TxnEvent{ 73 FinalName: "myName", 74 Duration: 3 * time.Second, 75 Queuing: 5 * time.Second, 76 DatastoreExternalTotals: DatastoreExternalTotals{ 77 externalCallCount: 22, 78 externalDuration: 1122334 * time.Millisecond, 79 datastoreCallCount: 33, 80 datastoreDuration: 5566778 * time.Millisecond, 81 }, 82 }, 83 }, `[ 84 { 85 "type":"TransactionError", 86 "error.class":"*errors.errorString", 87 "error.message":"hello", 88 "timestamp":1.41713646e+09, 89 "transactionName":"myName", 90 "duration":3, 91 "queueDuration":5, 92 "externalCallCount":22, 93 "externalDuration":1122.334, 94 "databaseCallCount":33, 95 "databaseDuration":5566.778 96 }, 97 {}, 98 {} 99 ]`) 100 } 101 102 func TestErrorEventAttributes(t *testing.T) { 103 aci := sampleAttributeConfigInput 104 aci.ErrorCollector.Exclude = append(aci.ErrorCollector.Exclude, "zap") 105 aci.ErrorCollector.Exclude = append(aci.ErrorCollector.Exclude, hostDisplayName) 106 cfg := CreateAttributeConfig(aci, true) 107 attr := NewAttributes(cfg) 108 attr.Agent.HostDisplayName = "exclude me" 109 attr.Agent.RequestMethod = "GET" 110 AddUserAttribute(attr, "zap", 123, DestAll) 111 AddUserAttribute(attr, "zip", 456, DestAll) 112 113 testErrorEventJSON(t, &ErrorEvent{ 114 ErrorData: sampleErrorData, 115 TxnEvent: TxnEvent{ 116 FinalName: "myName", 117 Duration: 3 * time.Second, 118 Attrs: attr, 119 }, 120 }, `[ 121 { 122 "type":"TransactionError", 123 "error.class":"*errors.errorString", 124 "error.message":"hello", 125 "timestamp":1.41713646e+09, 126 "transactionName":"myName", 127 "duration":3 128 }, 129 { 130 "zip":456 131 }, 132 { 133 "request.method":"GET" 134 } 135 ]`) 136 }