-
Notifications
You must be signed in to change notification settings - Fork 39
/
lambda_agent_test.go
115 lines (86 loc) · 2.82 KB
/
lambda_agent_test.go
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
// (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.InitSensor(instana.DefaultOptions())
os.Exit(m.Run())
}
func TestIntegration_LambdaAgent_SendSpans(t *testing.T) {
defer agent.Reset()
tracer := instana.NewTracer()
sensor := instana.NewSensorWithTracer(tracer)
sp := sensor.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, tracer.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()
tracer := instana.NewTracer()
sensor := instana.NewSensorWithTracer(tracer)
defer instana.ShutdownSensor()
sp := sensor.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, tracer.Flush(ctx))
require.Len(t, agent.Bundles, 0)
}
func setupAWSLambdaEnv() func() {
teardown := restoreEnvVarFunc("AWS_EXECUTION_ENV")
os.Setenv("AWS_EXECUTION_ENV", "AWS_Lambda_go1.x")
return teardown
}