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

     1  package internal
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestCreateFinalMetrics(t *testing.T) {
     9  	now := time.Now()
    10  
    11  	h := NewHarvest(now)
    12  	h.CreateFinalMetrics()
    13  	ExpectMetrics(t, h.Metrics, []WantMetric{
    14  		{instanceReporting, "", true, []float64{1, 0, 0, 0, 0, 0}},
    15  		{customEventsSeen, "", true, []float64{0, 0, 0, 0, 0, 0}},
    16  		{customEventsSent, "", true, []float64{0, 0, 0, 0, 0, 0}},
    17  		{txnEventsSeen, "", true, []float64{0, 0, 0, 0, 0, 0}},
    18  		{txnEventsSent, "", true, []float64{0, 0, 0, 0, 0, 0}},
    19  		{errorEventsSeen, "", true, []float64{0, 0, 0, 0, 0, 0}},
    20  		{errorEventsSent, "", true, []float64{0, 0, 0, 0, 0, 0}},
    21  	})
    22  
    23  	h = NewHarvest(now)
    24  	h.Metrics = newMetricTable(0, now)
    25  	h.CustomEvents = newCustomEvents(1)
    26  	h.TxnEvents = newTxnEvents(1)
    27  	h.ErrorEvents = newErrorEvents(1)
    28  
    29  	h.Metrics.addSingleCount("drop me!", unforced)
    30  
    31  	customE, err := CreateCustomEvent("my event type", map[string]interface{}{"zip": 1}, time.Now())
    32  	if nil != err {
    33  		t.Fatal(err)
    34  	}
    35  	h.CustomEvents.Add(customE)
    36  	h.CustomEvents.Add(customE)
    37  
    38  	txnE := &TxnEvent{}
    39  	h.TxnEvents.AddTxnEvent(txnE)
    40  	h.TxnEvents.AddTxnEvent(txnE)
    41  
    42  	h.ErrorEvents.Add(&ErrorEvent{})
    43  	h.ErrorEvents.Add(&ErrorEvent{})
    44  
    45  	h.CreateFinalMetrics()
    46  	ExpectMetrics(t, h.Metrics, []WantMetric{
    47  		{instanceReporting, "", true, []float64{1, 0, 0, 0, 0, 0}},
    48  		{customEventsSeen, "", true, []float64{2, 0, 0, 0, 0, 0}},
    49  		{customEventsSent, "", true, []float64{1, 0, 0, 0, 0, 0}},
    50  		{txnEventsSeen, "", true, []float64{2, 0, 0, 0, 0, 0}},
    51  		{txnEventsSent, "", true, []float64{1, 0, 0, 0, 0, 0}},
    52  		{errorEventsSeen, "", true, []float64{2, 0, 0, 0, 0, 0}},
    53  		{errorEventsSent, "", true, []float64{1, 0, 0, 0, 0, 0}},
    54  		{supportabilityDropped, "", true, []float64{1, 0, 0, 0, 0, 0}},
    55  	})
    56  }
    57  
    58  func TestEmptyPayloads(t *testing.T) {
    59  	h := NewHarvest(time.Now())
    60  	payloads := h.Payloads()
    61  	for _, p := range payloads {
    62  		d, err := p.Data("agentRunID", time.Now())
    63  		if d != nil || err != nil {
    64  			t.Error(d, err)
    65  		}
    66  	}
    67  }
    68  
    69  func TestMergeFailedHarvest(t *testing.T) {
    70  	start1 := time.Now()
    71  	start2 := start1.Add(1 * time.Minute)
    72  	h := NewHarvest(start1)
    73  	h.Metrics.addCount("zip", 1, forced)
    74  	h.TxnEvents.AddTxnEvent(&TxnEvent{
    75  		FinalName: "finalName",
    76  		Start:     time.Now(),
    77  		Duration:  1 * time.Second,
    78  	})
    79  	customEventParams := map[string]interface{}{"zip": 1}
    80  	ce, err := CreateCustomEvent("myEvent", customEventParams, time.Now())
    81  	if nil != err {
    82  		t.Fatal(err)
    83  	}
    84  	h.CustomEvents.Add(ce)
    85  	h.ErrorEvents.Add(&ErrorEvent{
    86  		ErrorData: ErrorData{
    87  			Klass: "klass",
    88  			Msg:   "msg",
    89  			When:  time.Now(),
    90  		},
    91  		TxnEvent: TxnEvent{
    92  			FinalName: "finalName",
    93  			Duration:  1 * time.Second,
    94  		},
    95  	})
    96  
    97  	ers := NewTxnErrors(10)
    98  	ers.Add(ErrorData{
    99  		When:  time.Now(),
   100  		Msg:   "msg",
   101  		Klass: "klass",
   102  		Stack: GetStackTrace(0),
   103  	})
   104  	MergeTxnErrors(&h.ErrorTraces, ers, TxnEvent{
   105  		FinalName: "finalName",
   106  		CleanURL:  "requestURI",
   107  		Attrs:     nil,
   108  	})
   109  
   110  	if start1 != h.Metrics.metricPeriodStart {
   111  		t.Error(h.Metrics.metricPeriodStart)
   112  	}
   113  	if 0 != h.Metrics.failedHarvests {
   114  		t.Error(h.Metrics.failedHarvests)
   115  	}
   116  	if 0 != h.CustomEvents.events.failedHarvests {
   117  		t.Error(h.CustomEvents.events.failedHarvests)
   118  	}
   119  	if 0 != h.TxnEvents.events.failedHarvests {
   120  		t.Error(h.TxnEvents.events.failedHarvests)
   121  	}
   122  	if 0 != h.ErrorEvents.events.failedHarvests {
   123  		t.Error(h.ErrorEvents.events.failedHarvests)
   124  	}
   125  	ExpectMetrics(t, h.Metrics, []WantMetric{
   126  		{"zip", "", true, []float64{1, 0, 0, 0, 0, 0}},
   127  	})
   128  	ExpectCustomEvents(t, h.CustomEvents, []WantEvent{{
   129  		Intrinsics: map[string]interface{}{
   130  			"type":      "myEvent",
   131  			"timestamp": MatchAnything,
   132  		},
   133  		UserAttributes: customEventParams,
   134  	}})
   135  	ExpectErrorEvents(t, h.ErrorEvents, []WantEvent{{
   136  		Intrinsics: map[string]interface{}{
   137  			"error.class":     "klass",
   138  			"error.message":   "msg",
   139  			"transactionName": "finalName",
   140  		},
   141  	}})
   142  	ExpectTxnEvents(t, h.TxnEvents, []WantEvent{{
   143  		Intrinsics: map[string]interface{}{
   144  			"name": "finalName",
   145  		},
   146  	}})
   147  	ExpectErrors(t, h.ErrorTraces, []WantError{{
   148  		TxnName: "finalName",
   149  		Msg:     "msg",
   150  		Klass:   "klass",
   151  		Caller:  "internal.TestMergeFailedHarvest",
   152  		URL:     "requestURI",
   153  	}})
   154  
   155  	nextHarvest := NewHarvest(start2)
   156  	if start2 != nextHarvest.Metrics.metricPeriodStart {
   157  		t.Error(nextHarvest.Metrics.metricPeriodStart)
   158  	}
   159  	payloads := h.Payloads()
   160  	for _, p := range payloads {
   161  		p.MergeIntoHarvest(nextHarvest)
   162  	}
   163  
   164  	if start1 != nextHarvest.Metrics.metricPeriodStart {
   165  		t.Error(nextHarvest.Metrics.metricPeriodStart)
   166  	}
   167  	if 1 != nextHarvest.Metrics.failedHarvests {
   168  		t.Error(nextHarvest.Metrics.failedHarvests)
   169  	}
   170  	if 1 != nextHarvest.CustomEvents.events.failedHarvests {
   171  		t.Error(nextHarvest.CustomEvents.events.failedHarvests)
   172  	}
   173  	if 1 != nextHarvest.TxnEvents.events.failedHarvests {
   174  		t.Error(nextHarvest.TxnEvents.events.failedHarvests)
   175  	}
   176  	if 1 != nextHarvest.ErrorEvents.events.failedHarvests {
   177  		t.Error(nextHarvest.ErrorEvents.events.failedHarvests)
   178  	}
   179  	ExpectMetrics(t, nextHarvest.Metrics, []WantMetric{
   180  		{"zip", "", true, []float64{1, 0, 0, 0, 0, 0}},
   181  	})
   182  	ExpectCustomEvents(t, nextHarvest.CustomEvents, []WantEvent{{
   183  		Intrinsics: map[string]interface{}{
   184  			"type":      "myEvent",
   185  			"timestamp": MatchAnything,
   186  		},
   187  		UserAttributes: customEventParams,
   188  	}})
   189  	ExpectErrorEvents(t, nextHarvest.ErrorEvents, []WantEvent{{
   190  		Intrinsics: map[string]interface{}{
   191  			"error.class":     "klass",
   192  			"error.message":   "msg",
   193  			"transactionName": "finalName",
   194  		},
   195  	}})
   196  	ExpectTxnEvents(t, nextHarvest.TxnEvents, []WantEvent{{
   197  		Intrinsics: map[string]interface{}{
   198  			"name": "finalName",
   199  		},
   200  	}})
   201  	ExpectErrors(t, nextHarvest.ErrorTraces, []WantError{})
   202  }
   203  
   204  func TestCreateTxnMetrics(t *testing.T) {
   205  	txnErr := &ErrorData{}
   206  	txnErrors := []*ErrorData{txnErr}
   207  	webName := "WebTransaction/zip/zap"
   208  	backgroundName := "OtherTransaction/zip/zap"
   209  	args := &TxnData{}
   210  	args.Duration = 123 * time.Second
   211  	args.Exclusive = 109 * time.Second
   212  	args.ApdexThreshold = 2 * time.Second
   213  
   214  	args.FinalName = webName
   215  	args.IsWeb = true
   216  	args.Errors = txnErrors
   217  	args.Zone = ApdexTolerating
   218  	metrics := newMetricTable(100, time.Now())
   219  	CreateTxnMetrics(args, metrics)
   220  	ExpectMetrics(t, metrics, []WantMetric{
   221  		{webName, "", true, []float64{1, 123, 109, 123, 123, 123 * 123}},
   222  		{webRollup, "", true, []float64{1, 123, 109, 123, 123, 123 * 123}},
   223  		{dispatcherMetric, "", true, []float64{1, 123, 0, 123, 123, 123 * 123}},
   224  		{"Errors/all", "", true, []float64{1, 0, 0, 0, 0, 0}},
   225  		{"Errors/allWeb", "", true, []float64{1, 0, 0, 0, 0, 0}},
   226  		{"Errors/" + webName, "", true, []float64{1, 0, 0, 0, 0, 0}},
   227  		{apdexRollup, "", true, []float64{0, 1, 0, 2, 2, 0}},
   228  		{"Apdex/zip/zap", "", false, []float64{0, 1, 0, 2, 2, 0}},
   229  	})
   230  
   231  	args.FinalName = webName
   232  	args.IsWeb = true
   233  	args.Errors = nil
   234  	args.Zone = ApdexTolerating
   235  	metrics = newMetricTable(100, time.Now())
   236  	CreateTxnMetrics(args, metrics)
   237  	ExpectMetrics(t, metrics, []WantMetric{
   238  		{webName, "", true, []float64{1, 123, 109, 123, 123, 123 * 123}},
   239  		{webRollup, "", true, []float64{1, 123, 109, 123, 123, 123 * 123}},
   240  		{dispatcherMetric, "", true, []float64{1, 123, 0, 123, 123, 123 * 123}},
   241  		{apdexRollup, "", true, []float64{0, 1, 0, 2, 2, 0}},
   242  		{"Apdex/zip/zap", "", false, []float64{0, 1, 0, 2, 2, 0}},
   243  	})
   244  
   245  	args.FinalName = backgroundName
   246  	args.IsWeb = false
   247  	args.Errors = txnErrors
   248  	args.Zone = ApdexNone
   249  	metrics = newMetricTable(100, time.Now())
   250  	CreateTxnMetrics(args, metrics)
   251  	ExpectMetrics(t, metrics, []WantMetric{
   252  		{backgroundName, "", true, []float64{1, 123, 109, 123, 123, 123 * 123}},
   253  		{backgroundRollup, "", true, []float64{1, 123, 109, 123, 123, 123 * 123}},
   254  		{"Errors/all", "", true, []float64{1, 0, 0, 0, 0, 0}},
   255  		{"Errors/allOther", "", true, []float64{1, 0, 0, 0, 0, 0}},
   256  		{"Errors/" + backgroundName, "", true, []float64{1, 0, 0, 0, 0, 0}},
   257  	})
   258  
   259  	args.FinalName = backgroundName
   260  	args.IsWeb = false
   261  	args.Errors = nil
   262  	args.Zone = ApdexNone
   263  	metrics = newMetricTable(100, time.Now())
   264  	CreateTxnMetrics(args, metrics)
   265  	ExpectMetrics(t, metrics, []WantMetric{
   266  		{backgroundName, "", true, []float64{1, 123, 109, 123, 123, 123 * 123}},
   267  		{backgroundRollup, "", true, []float64{1, 123, 109, 123, 123, 123 * 123}},
   268  	})
   269  
   270  }