github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/errors_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  var (
    11  	emptyStackTrace = make([]uintptr, 0)
    12  )
    13  
    14  func TestErrorTraceMarshal(t *testing.T) {
    15  	he := &tracedError{
    16  		ErrorData: ErrorData{
    17  			When:  time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC),
    18  			Stack: emptyStackTrace,
    19  			Msg:   "my_msg",
    20  			Klass: "my_class",
    21  		},
    22  		TxnEvent: TxnEvent{
    23  			FinalName: "my_txn_name",
    24  			CleanURL:  "my_request_uri",
    25  			Attrs:     nil,
    26  		},
    27  	}
    28  	js, err := json.Marshal(he)
    29  	if nil != err {
    30  		t.Error(err)
    31  	}
    32  	expect := CompactJSONString(`
    33  	[
    34  		1.41713646e+12,
    35  		"my_txn_name",
    36  		"my_msg",
    37  		"my_class",
    38  		{
    39  			"agentAttributes":{},
    40  			"userAttributes":{},
    41  			"intrinsics":{},
    42  			"stack_trace":[],
    43  			"request_uri":"my_request_uri"
    44  		}
    45  	]`)
    46  	if string(js) != expect {
    47  		t.Error(string(js))
    48  	}
    49  }
    50  
    51  func TestErrorTraceAttributes(t *testing.T) {
    52  	aci := sampleAttributeConfigInput
    53  	aci.ErrorCollector.Exclude = append(aci.ErrorCollector.Exclude, "zap")
    54  	aci.ErrorCollector.Exclude = append(aci.ErrorCollector.Exclude, hostDisplayName)
    55  	cfg := CreateAttributeConfig(aci, true)
    56  	attr := NewAttributes(cfg)
    57  	attr.Agent.HostDisplayName = "exclude me"
    58  	attr.Agent.RequestMethod = "GET"
    59  	AddUserAttribute(attr, "zap", 123, DestAll)
    60  	AddUserAttribute(attr, "zip", 456, DestAll)
    61  
    62  	he := &tracedError{
    63  		ErrorData: ErrorData{
    64  			When:  time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC),
    65  			Stack: nil,
    66  			Msg:   "my_msg",
    67  			Klass: "my_class",
    68  		},
    69  		TxnEvent: TxnEvent{
    70  			FinalName: "my_txn_name",
    71  			CleanURL:  "my_request_uri",
    72  			Attrs:     attr,
    73  		},
    74  	}
    75  	js, err := json.Marshal(he)
    76  	if nil != err {
    77  		t.Error(err)
    78  	}
    79  	expect := CompactJSONString(`
    80  	[
    81  		1.41713646e+12,
    82  		"my_txn_name",
    83  		"my_msg",
    84  		"my_class",
    85  		{
    86  			"agentAttributes":{"request.method":"GET"},
    87  			"userAttributes":{"zip":456},
    88  			"intrinsics":{},
    89  			"request_uri":"my_request_uri"
    90  		}
    91  	]`)
    92  	if string(js) != expect {
    93  		t.Error(string(js))
    94  	}
    95  }
    96  
    97  func TestErrorsLifecycle(t *testing.T) {
    98  	ers := NewTxnErrors(5)
    99  
   100  	when := time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC)
   101  	ers.Add(TxnErrorFromResponseCode(when, 400))
   102  	ers.Add(TxnErrorFromPanic(when, errors.New("oh no panic")))
   103  	ers.Add(TxnErrorFromPanic(when, 123))
   104  	ers.Add(TxnErrorFromPanic(when, 123))
   105  
   106  	he := newHarvestErrors(3)
   107  	MergeTxnErrors(&he, ers, TxnEvent{
   108  		FinalName: "txnName",
   109  		CleanURL:  "requestURI",
   110  		Attrs:     nil,
   111  	})
   112  	js, err := he.Data("agentRunID", time.Now())
   113  	if nil != err {
   114  		t.Error(err)
   115  	}
   116  	expect := CompactJSONString(`
   117  [
   118     "agentRunID",
   119     [
   120        [
   121           1.41713646e+12,
   122           "txnName",
   123           "Bad Request",
   124           "400",
   125           {
   126              "agentAttributes":{},
   127              "userAttributes":{},
   128              "intrinsics":{},
   129              "request_uri":"requestURI"
   130           }
   131        ],
   132        [
   133           1.41713646e+12,
   134           "txnName",
   135           "oh no panic",
   136           "panic",
   137           {
   138              "agentAttributes":{},
   139              "userAttributes":{},
   140              "intrinsics":{},
   141              "request_uri":"requestURI"
   142           }
   143        ],
   144        [
   145           1.41713646e+12,
   146           "txnName",
   147           "123",
   148           "panic",
   149           {
   150              "agentAttributes":{},
   151              "userAttributes":{},
   152              "intrinsics":{},
   153              "request_uri":"requestURI"
   154           }
   155        ]
   156     ]
   157  ]`)
   158  	if string(js) != expect {
   159  		t.Error(string(js), expect)
   160  	}
   161  }
   162  
   163  func BenchmarkErrorsJSON(b *testing.B) {
   164  	when := time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC)
   165  	max := 20
   166  	ers := NewTxnErrors(max)
   167  
   168  	for i := 0; i < max; i++ {
   169  		ers.Add(ErrorData{
   170  			When:  time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC),
   171  			Msg:   "error message",
   172  			Klass: "error class",
   173  		})
   174  	}
   175  
   176  	cfg := CreateAttributeConfig(sampleAttributeConfigInput, true)
   177  	attr := NewAttributes(cfg)
   178  	attr.Agent.RequestMethod = "GET"
   179  	AddUserAttribute(attr, "zip", 456, DestAll)
   180  
   181  	he := newHarvestErrors(max)
   182  	MergeTxnErrors(&he, ers, TxnEvent{
   183  		FinalName: "WebTransaction/Go/hello",
   184  		CleanURL:  "/url",
   185  		Attrs:     attr,
   186  	})
   187  
   188  	b.ReportAllocs()
   189  	b.ResetTimer()
   190  
   191  	for n := 0; n < b.N; n++ {
   192  		js, err := he.Data("agentRundID", when)
   193  		if nil != err || nil == js {
   194  			b.Fatal(err, js)
   195  		}
   196  	}
   197  }