-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathlambda_agent_test.go
More file actions
343 lines (261 loc) · 9.78 KB
/
lambda_agent_test.go
File metadata and controls
343 lines (261 loc) · 9.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2020
//go:build lambda && integration
// +build lambda,integration
package instana_test
import (
"context"
"encoding/json"
"log"
"os"
"testing"
"time"
instana "github.com/instana/go-sensor"
"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var agent *serverlessAgent
func TestMain(m *testing.M) {
teardownEnv := setupAWSLambdaEnv()
defer teardownEnv()
defer restoreEnvVarFunc("INSTANA_AGENT_KEY")
os.Setenv("INSTANA_AGENT_KEY", "testkey1")
defer restoreEnvVarFunc("INSTANA_ZONE")
os.Setenv("INSTANA_ZONE", "testzone")
defer restoreEnvVarFunc("INSTANA_TAGS")
os.Setenv("INSTANA_TAGS", "key1=value1,key2")
defer restoreEnvVarFunc("INSTANA_SECRETS")
os.Setenv("INSTANA_SECRETS", "contains-ignore-case:key,password,secret,classified")
defer restoreEnvVarFunc("CLASSIFIED_DATA")
os.Setenv("CLASSIFIED_DATA", "classified")
var err error
agent, err = setupServerlessAgent()
if err != nil {
log.Fatalf("failed to initialize serverless agent: %s", err)
}
instana.InitCollector(instana.DefaultOptions())
defer instana.ShutdownCollector()
os.Exit(m.Run())
}
func TestIntegration_LambdaAgent_SendSpans(t *testing.T) {
defer agent.Reset()
c := instana.InitCollector(instana.DefaultOptions())
defer instana.ShutdownCollector()
sp := c.Tracer().StartSpan("aws.lambda.entry", opentracing.Tags{
"lambda.arn": "aws::test-lambda::$LATEST",
"lambda.name": "test-lambda",
"lambda.version": "$LATEST",
})
sp.Finish()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, c.Flush(ctx))
require.Len(t, agent.Bundles, 1)
var spans []map[string]json.RawMessage
for _, bundle := range agent.Bundles {
var payload struct {
Spans []map[string]json.RawMessage `json:"spans"`
}
require.NoError(t, json.Unmarshal(bundle.Body, &payload), "%s", string(bundle.Body))
spans = append(spans, payload.Spans...)
}
require.Len(t, spans, 1)
assert.JSONEq(t, `{"hl": true, "cp": "aws", "e": "aws::test-lambda::$LATEST"}`, string(spans[0]["f"]))
}
func TestIntegration_LambdaAgent_SendSpans_Error(t *testing.T) {
defer agent.Reset()
c := instana.InitCollector(instana.DefaultOptions())
defer instana.ShutdownCollector()
sp := c.Tracer().StartSpan("aws.lambda.entry", opentracing.Tags{
"lambda.arn": "aws::test-lambda::$LATEST",
"lambda.name": "test-lambda",
"lambda.version": "$LATEST",
"lambda.error": "true",
})
sp.Finish()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, c.Flush(ctx))
require.Len(t, agent.Bundles, 0)
}
func TestIntegration_LambdaAgent_SendSpans_WithTrigger(t *testing.T) {
defer agent.Reset()
c := instana.InitCollector(instana.DefaultOptions())
defer instana.ShutdownCollector()
sp := c.Tracer().StartSpan("aws.lambda.entry", opentracing.Tags{
"lambda.arn": "arn:aws:lambda:us-east-1:123456789:function:test-lambda",
"lambda.name": "test-lambda",
"lambda.version": "$LATEST",
"lambda.trigger": "aws:api.gateway",
})
sp.Finish()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, c.Flush(ctx))
require.Len(t, agent.Bundles, 1)
var spans []map[string]json.RawMessage
for _, bundle := range agent.Bundles {
var payload struct {
Spans []map[string]json.RawMessage `json:"spans"`
}
require.NoError(t, json.Unmarshal(bundle.Body, &payload), "%s", string(bundle.Body))
spans = append(spans, payload.Spans...)
}
require.Len(t, spans, 1)
assert.JSONEq(t, `{"hl": true, "cp": "aws", "e": "arn:aws:lambda:us-east-1:123456789:function:test-lambda"}`, string(spans[0]["f"]))
}
func TestIntegration_LambdaAgent_SendSpans_Multiple(t *testing.T) {
defer agent.Reset()
c := instana.InitCollector(instana.DefaultOptions())
defer instana.ShutdownCollector()
// Create parent lambda span
parentSp := c.Tracer().StartSpan("aws.lambda.entry", opentracing.Tags{
"lambda.arn": "arn:aws:lambda:us-east-1:123456789:function:test-lambda",
"lambda.name": "test-lambda",
"lambda.version": "$LATEST",
})
// Create child spans
childSp1 := c.Tracer().StartSpan("http.client", opentracing.ChildOf(parentSp.Context()))
childSp1.SetTag("http.url", "https://api.example.com/data")
childSp1.SetTag("http.method", "GET")
childSp1.Finish()
childSp2 := c.Tracer().StartSpan("sdk.custom", opentracing.ChildOf(parentSp.Context()))
childSp2.SetTag("custom.tag", "value")
childSp2.Finish()
parentSp.Finish()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, c.Flush(ctx))
require.Len(t, agent.Bundles, 1)
var spans []map[string]json.RawMessage
for _, bundle := range agent.Bundles {
var payload struct {
Spans []map[string]json.RawMessage `json:"spans"`
}
require.NoError(t, json.Unmarshal(bundle.Body, &payload), "%s", string(bundle.Body))
spans = append(spans, payload.Spans...)
}
require.Len(t, spans, 3)
}
func TestIntegration_LambdaAgent_SendSpans_WithColdStart(t *testing.T) {
defer agent.Reset()
c := instana.InitCollector(instana.DefaultOptions())
defer instana.ShutdownCollector()
sp := c.Tracer().StartSpan("aws.lambda.entry", opentracing.Tags{
"lambda.arn": "arn:aws:lambda:us-east-1:123456789:function:test-lambda",
"lambda.name": "test-lambda",
"lambda.version": "$LATEST",
"lambda.coldStart": true,
"lambda.msleft": 5000,
})
sp.Finish()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, c.Flush(ctx))
require.Len(t, agent.Bundles, 1)
var spans []map[string]json.RawMessage
for _, bundle := range agent.Bundles {
var payload struct {
Spans []map[string]json.RawMessage `json:"spans"`
}
require.NoError(t, json.Unmarshal(bundle.Body, &payload), "%s", string(bundle.Body))
spans = append(spans, payload.Spans...)
}
require.Len(t, spans, 1)
// Verify span data contains lambda information
var spanData map[string]interface{}
require.NoError(t, json.Unmarshal(spans[0]["data"], &spanData))
lambdaData, ok := spanData["lambda"].(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "arn:aws:lambda:us-east-1:123456789:function:test-lambda", lambdaData["arn"])
assert.Equal(t, true, lambdaData["coldStart"])
}
func TestIntegration_LambdaAgent_Headers(t *testing.T) {
defer agent.Reset()
c := instana.InitCollector(instana.DefaultOptions())
defer instana.ShutdownCollector()
sp := c.Tracer().StartSpan("aws.lambda.entry", opentracing.Tags{
"lambda.arn": "arn:aws:lambda:us-east-1:123456789:function:test-lambda",
"lambda.name": "test-lambda",
"lambda.version": "$LATEST",
})
sp.Finish()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, c.Flush(ctx))
require.Len(t, agent.Bundles, 1)
collected := agent.Bundles[0]
// Verify headers
assert.Equal(t, "arn:aws:lambda:us-east-1:123456789:function:test-lambda", collected.Header.Get("X-Instana-Host"))
assert.Equal(t, "testkey1", collected.Header.Get("X-Instana-Key"))
assert.NotEmpty(t, collected.Header.Get("X-Instana-Time"))
assert.Equal(t, "application/json", collected.Header.Get("Content-Type"))
}
func TestIntegration_LambdaAgent_Metrics(t *testing.T) {
defer agent.Reset()
c := instana.InitCollector(instana.DefaultOptions())
defer instana.ShutdownCollector()
sp := c.Tracer().StartSpan("aws.lambda.entry", opentracing.Tags{
"lambda.arn": "arn:aws:lambda:us-east-1:123456789:function:test-lambda",
"lambda.name": "test-lambda",
"lambda.version": "$LATEST",
})
sp.Finish()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, c.Flush(ctx))
require.Len(t, agent.Bundles, 1)
var payload struct {
Metrics struct {
Plugins []struct {
Name string `json:"name"`
EntityID string `json:"entityId"`
Data map[string]interface{} `json:"data"`
} `json:"plugins"`
} `json:"metrics"`
}
require.NoError(t, json.Unmarshal(agent.Bundles[0].Body, &payload))
// Verify AWS Lambda plugin payload is present
require.Len(t, payload.Metrics.Plugins, 1)
assert.Equal(t, "com.instana.plugin.aws.lambda", payload.Metrics.Plugins[0].Name)
assert.Equal(t, "arn:aws:lambda:us-east-1:123456789:function:test-lambda", payload.Metrics.Plugins[0].EntityID)
}
func TestIntegration_LambdaAgent_FlushWithoutSnapshot(t *testing.T) {
defer agent.Reset()
c := instana.InitCollector(instana.DefaultOptions())
defer instana.ShutdownCollector()
// Create a non-lambda span
sp := c.Tracer().StartSpan("sdk.custom")
sp.SetTag("custom.tag", "value")
sp.Finish()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
// Flush should fail because there's no lambda snapshot
err := c.Flush(ctx)
// The error might be wrapped, so we just check that flush completes
// In lambda agent, it returns ErrAgentNotReady when no snapshot is available
if err != nil {
assert.Contains(t, err.Error(), "agent not ready")
}
}
func TestIntegration_LambdaAgent_PeriodicFlush(t *testing.T) {
defer agent.Reset()
c := instana.InitCollector(instana.DefaultOptions())
defer instana.ShutdownCollector()
sp := c.Tracer().StartSpan("aws.lambda.entry", opentracing.Tags{
"lambda.arn": "arn:aws:lambda:us-east-1:123456789:function:test-lambda",
"lambda.name": "test-lambda",
"lambda.version": "$LATEST",
})
sp.Finish()
// Wait for periodic flush (awsLambdaAgentFlushPeriod = 2 seconds)
time.Sleep(2500 * time.Millisecond)
// Should have received at least one bundle
assert.GreaterOrEqual(t, len(agent.Bundles), 1)
}
func setupAWSLambdaEnv() func() {
teardown := restoreEnvVarFunc("AWS_EXECUTION_ENV")
os.Setenv("AWS_EXECUTION_ENV", "AWS_Lambda_go1.x")
return teardown
}