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

     1  package internal
     2  
     3  import (
     4  	"encoding/json"
     5  	"math"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/lulzWill/go-agent/internal/cat"
    10  )
    11  
    12  func testTxnEventJSON(t *testing.T, e *TxnEvent, expect string) {
    13  	js, err := json.Marshal(e)
    14  	if nil != err {
    15  		t.Error(err)
    16  		return
    17  	}
    18  	expect = CompactJSONString(expect)
    19  	if string(js) != expect {
    20  		t.Error(string(js), expect)
    21  	}
    22  }
    23  
    24  func TestTxnEventMarshal(t *testing.T) {
    25  	testTxnEventJSON(t, &TxnEvent{
    26  		FinalName: "myName",
    27  		Start:     time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC),
    28  		Duration:  2 * time.Second,
    29  		Zone:      ApdexNone,
    30  		Attrs:     nil,
    31  	}, `[
    32  	{
    33  		"type":"Transaction",
    34  		"name":"myName",
    35  		"timestamp":1.41713646e+09,
    36  		"duration":2
    37  	},
    38  	{},
    39  	{}]`)
    40  	testTxnEventJSON(t, &TxnEvent{
    41  		FinalName: "myName",
    42  		Start:     time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC),
    43  		Duration:  2 * time.Second,
    44  		Zone:      ApdexFailing,
    45  		Attrs:     nil,
    46  	}, `[
    47  	{
    48  		"type":"Transaction",
    49  		"name":"myName",
    50  		"timestamp":1.41713646e+09,
    51  		"duration":2,
    52  		"nr.apdexPerfZone":"F"
    53  	},
    54  	{},
    55  	{}]`)
    56  	testTxnEventJSON(t, &TxnEvent{
    57  		FinalName: "myName",
    58  		Start:     time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC),
    59  		Duration:  2 * time.Second,
    60  		Queuing:   5 * time.Second,
    61  		Zone:      ApdexNone,
    62  		Attrs:     nil,
    63  	}, `[
    64  	{
    65  		"type":"Transaction",
    66  		"name":"myName",
    67  		"timestamp":1.41713646e+09,
    68  		"duration":2,
    69  		"queueDuration":5
    70  	},
    71  	{},
    72  	{}]`)
    73  	testTxnEventJSON(t, &TxnEvent{
    74  		FinalName: "myName",
    75  		Start:     time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC),
    76  		Duration:  2 * time.Second,
    77  		Queuing:   5 * time.Second,
    78  		Zone:      ApdexNone,
    79  		Attrs:     nil,
    80  		DatastoreExternalTotals: DatastoreExternalTotals{
    81  			externalCallCount:  22,
    82  			externalDuration:   1122334 * time.Millisecond,
    83  			datastoreCallCount: 33,
    84  			datastoreDuration:  5566778 * time.Millisecond,
    85  		},
    86  	}, `[
    87  	{
    88  		"type":"Transaction",
    89  		"name":"myName",
    90  		"timestamp":1.41713646e+09,
    91  		"duration":2,
    92  		"queueDuration":5,
    93  		"externalCallCount":22,
    94  		"externalDuration":1122.334,
    95  		"databaseCallCount":33,
    96  		"databaseDuration":5566.778
    97  	},
    98  	{},
    99  	{}]`)
   100  }
   101  
   102  func TestTxnEventAttributes(t *testing.T) {
   103  	aci := sampleAttributeConfigInput
   104  	aci.TransactionEvents.Exclude = append(aci.TransactionEvents.Exclude, "zap")
   105  	aci.TransactionEvents.Exclude = append(aci.TransactionEvents.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  	testTxnEventJSON(t, &TxnEvent{
   114  		FinalName: "myName",
   115  		Start:     time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC),
   116  		Duration:  2 * time.Second,
   117  		Zone:      ApdexNone,
   118  		Attrs:     attr,
   119  	}, `[
   120  	{
   121  		"type":"Transaction",
   122  		"name":"myName",
   123  		"timestamp":1.41713646e+09,
   124  		"duration":2
   125  	},
   126  	{
   127  		"zip":456
   128  	},
   129  	{
   130  		"request.method":"GET"
   131  	}]`)
   132  }
   133  
   134  func TestTxnEventsSynthetics(t *testing.T) {
   135  	events := newTxnEvents(1)
   136  
   137  	regular := &TxnEvent{
   138  		FinalName: "myName",
   139  		Start:     time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC),
   140  		Duration:  2 * time.Second,
   141  		Zone:      ApdexNone,
   142  		Attrs:     nil,
   143  	}
   144  
   145  	synthetics := &TxnEvent{
   146  		FinalName: "myName",
   147  		Start:     time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC),
   148  		Duration:  2 * time.Second,
   149  		Zone:      ApdexNone,
   150  		Attrs:     nil,
   151  		CrossProcess: TxnCrossProcess{
   152  			Type: txnCrossProcessSynthetics,
   153  			Synthetics: &cat.SyntheticsHeader{
   154  				ResourceID: "resource",
   155  				JobID:      "job",
   156  				MonitorID:  "monitor",
   157  			},
   158  		},
   159  	}
   160  
   161  	events.AddTxnEvent(regular)
   162  
   163  	// Check that the event was saved and that the stamp was sensible.
   164  	if saved := events.events.events[0].jsonWriter; saved != regular {
   165  		t.Errorf("unexpected saved event: expected=%v; got=%v", regular, saved)
   166  	}
   167  	if stamp := events.events.events[0].stamp; stamp < 0.0 || stamp >= 1.0 {
   168  		t.Errorf("regular event got out of range stamp: %f", stamp)
   169  	}
   170  
   171  	// Now set the regular event stamp to be the maximum possible value and add
   172  	// the synthetics event, which should evict it. Note that, although
   173  	// math.Nextafter32() would be a much cleaner way of doing this, that
   174  	// requires Go 1.4.
   175  	events.events.events[0].stamp = eventStamp(math.Float32frombits(math.Float32bits(1.0) - 1))
   176  	events.AddTxnEvent(synthetics)
   177  
   178  	// Check that the event was saved and that the stamp was sensible.
   179  	if saved := events.events.events[0].jsonWriter; saved != synthetics {
   180  		t.Errorf("unexpected saved event: expected=%v; got=%v", synthetics, saved)
   181  	}
   182  	if stamp := events.events.events[0].stamp; stamp < 1.0 || stamp >= 2.0 {
   183  		t.Errorf("synthetics event got out of range stamp: %f", stamp)
   184  	}
   185  }