@@ -95,22 +95,26 @@ func TestShouldShip_ReasonAge(t *testing.T) {
95
95
func TestLifecycle (t * testing.T ) {
96
96
reqID := "test-req-id"
97
97
fnARN := "test-fn-arn"
98
- txnID := "023d90ff77f13b9f"
99
98
lambdaData := `{"log":{"message":"this is log"}}`
100
- txnData := fmt .Sprintf (`{"transaction":{"id":"%s"}}` , txnID )
99
+ txnData := fmt .Sprintf (`{"transaction":{"id":"%s"}}` , "023d90ff77f13b9f" )
101
100
ts := time .Date (2022 , time .October , 1 , 1 , 1 , 1 , 0 , time .UTC )
102
101
txnDur := time .Second
103
102
103
+ type agentInit struct {
104
+ init bool
105
+ withMetadata bool
106
+ }
107
+
104
108
for _ , tc := range []struct {
105
109
name string
106
- agentInit bool
110
+ agentInit agentInit
107
111
receiveAgentRootTxn bool
108
112
receiveLambdaLogRuntime bool
109
113
expected string
110
114
}{
111
115
{
112
116
name : "without_agent_init_without_root_txn" ,
113
- agentInit : false ,
117
+ agentInit : agentInit { init : false , withMetadata : false } ,
114
118
receiveAgentRootTxn : false ,
115
119
receiveLambdaLogRuntime : false ,
116
120
// Without agent init no proxy txn is created if root txn is not reported
@@ -122,7 +126,7 @@ func TestLifecycle(t *testing.T) {
122
126
},
123
127
{
124
128
name : "without_agent_init_with_root_txn" ,
125
- agentInit : false ,
129
+ agentInit : agentInit { init : false , withMetadata : false } ,
126
130
receiveAgentRootTxn : true ,
127
131
receiveLambdaLogRuntime : false ,
128
132
expected : fmt .Sprintf (
@@ -133,8 +137,8 @@ func TestLifecycle(t *testing.T) {
133
137
),
134
138
},
135
139
{
136
- name : "with_agent_init_with_root_txn " ,
137
- agentInit : true ,
140
+ name : "with_no_meta_agent_init_with_root_txn " ,
141
+ agentInit : agentInit { init : true , withMetadata : false } ,
138
142
receiveAgentRootTxn : true ,
139
143
receiveLambdaLogRuntime : false ,
140
144
expected : fmt .Sprintf (
@@ -145,8 +149,20 @@ func TestLifecycle(t *testing.T) {
145
149
),
146
150
},
147
151
{
148
- name : "with_agent_init_without_root_txn_with_runtimeDone" ,
149
- agentInit : true ,
152
+ name : "with_meta_agent_init_with_root_txn" ,
153
+ agentInit : agentInit {init : true , withMetadata : true },
154
+ receiveAgentRootTxn : true ,
155
+ receiveLambdaLogRuntime : false ,
156
+ expected : fmt .Sprintf (
157
+ "%s\n %s\n %s" ,
158
+ metadata ,
159
+ generateCompleteTxn (t , txnData , "success" , "" , txnDur ),
160
+ lambdaData ,
161
+ ),
162
+ },
163
+ {
164
+ name : "with_no_meta_agent_init_without_root_txn_with_runtimeDone" ,
165
+ agentInit : agentInit {init : true , withMetadata : false },
150
166
receiveAgentRootTxn : false ,
151
167
receiveLambdaLogRuntime : true ,
152
168
// With agent init proxy txn is created if root txn is not reported.
@@ -159,8 +175,37 @@ func TestLifecycle(t *testing.T) {
159
175
),
160
176
},
161
177
{
162
- name : "with_agent_init_without_root_txn" ,
163
- agentInit : true ,
178
+ name : "with_meta_agent_init_without_root_txn_with_runtimeDone" ,
179
+ agentInit : agentInit {init : true , withMetadata : true },
180
+ receiveAgentRootTxn : false ,
181
+ receiveLambdaLogRuntime : true ,
182
+ // With agent init proxy txn is created if root txn is not reported.
183
+ // Details in runtimeDone event is used to find the result of the txn.
184
+ expected : fmt .Sprintf (
185
+ "%s\n %s\n %s" ,
186
+ metadata ,
187
+ lambdaData ,
188
+ generateCompleteTxn (t , txnData , "failure" , "failure" , txnDur ),
189
+ ),
190
+ },
191
+ {
192
+ name : "with_no_meta_agent_init_without_root_txn" ,
193
+ agentInit : agentInit {init : true , withMetadata : false },
194
+ receiveAgentRootTxn : false ,
195
+ receiveLambdaLogRuntime : false ,
196
+ // With agent init proxy txn is created if root txn is not reported.
197
+ // If runtimeDone event is not available `timeout` is used as the
198
+ // result of the transaction.
199
+ expected : fmt .Sprintf (
200
+ "%s\n %s\n %s" ,
201
+ metadata ,
202
+ lambdaData ,
203
+ generateCompleteTxn (t , txnData , "timeout" , "failure" , txnDur ),
204
+ ),
205
+ },
206
+ {
207
+ name : "with_meta_agent_init_without_root_txn" ,
208
+ agentInit : agentInit {init : true , withMetadata : true },
164
209
receiveAgentRootTxn : false ,
165
210
receiveLambdaLogRuntime : false ,
166
211
// With agent init proxy txn is created if root txn is not reported.
@@ -179,8 +224,12 @@ func TestLifecycle(t *testing.T) {
179
224
// NEXT API response creates a new invocation cache
180
225
b .RegisterInvocation (reqID , fnARN , ts .Add (txnDur ).UnixMilli (), ts )
181
226
// Agent creates and registers a partial transaction in the extn
182
- if tc .agentInit {
183
- require .NoError (t , b .OnAgentInit (reqID , txnID , []byte (txnData )))
227
+ if tc .agentInit .init {
228
+ initData := txnData
229
+ if tc .agentInit .withMetadata {
230
+ initData = fmt .Sprintf ("%s\n %s" , metadata , txnData )
231
+ }
232
+ require .NoError (t , b .OnAgentInit (reqID , "" , []byte (initData )))
184
233
}
185
234
// Agent sends a request with metadata
186
235
require .NoError (t , b .AddAgentData (APMData {
@@ -209,17 +258,18 @@ func TestLifecycle(t *testing.T) {
209
258
}
210
259
}
211
260
212
- func TestIsTransactionEvent (t * testing.T ) {
261
+ func TestFindEventType (t * testing.T ) {
213
262
for _ , tc := range []struct {
214
263
body []byte
215
- expected bool
264
+ expected eventType
216
265
}{
217
- {body : []byte (`{}` ), expected : false },
218
- {body : []byte (`{"tran":{}}` ), expected : false },
219
- {body : []byte (`{"span":{}}` ), expected : false },
220
- {body : []byte (`{"transaction":{}}` ), expected : true },
266
+ {body : []byte (`{}` ), expected : otherEvent },
267
+ {body : []byte (`{"tran":{}}` ), expected : otherEvent },
268
+ {body : []byte (`{"span":{}}` ), expected : otherEvent },
269
+ {body : []byte (`{"metadata":{}}\n{"transaction":{}}` ), expected : metadataEvent },
270
+ {body : []byte (`{"transaction":{}}` ), expected : transactionEvent },
221
271
} {
222
- assert .Equal (t , tc .expected , isTransactionEvent (tc .body ))
272
+ assert .Equal (t , tc .expected , findEventType (tc .body ))
223
273
}
224
274
}
225
275
0 commit comments