-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathLambdaHandlerInstrumentationTest.groovy
More file actions
272 lines (247 loc) · 7.62 KB
/
LambdaHandlerInstrumentationTest.groovy
File metadata and controls
272 lines (247 loc) · 7.62 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
import static datadog.trace.api.gateway.Events.EVENTS
import datadog.trace.agent.test.naming.VersionedNamingTestBase
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.function.TriConsumer
import datadog.trace.api.function.TriFunction
import datadog.trace.api.gateway.Flow
import datadog.trace.api.gateway.RequestContext
import datadog.trace.api.gateway.RequestContextSlot
import datadog.trace.bootstrap.ActiveSubsystems
import datadog.trace.bootstrap.instrumentation.api.AgentTracer
import datadog.trace.bootstrap.instrumentation.api.URIDataAdapter
import com.amazonaws.services.lambda.runtime.Context
import java.nio.charset.StandardCharsets
import java.util.function.BiFunction
import java.util.function.Function
import java.util.function.Supplier
abstract class LambdaHandlerInstrumentationTest extends VersionedNamingTestBase {
def requestId = "test-request-id"
// Must set this env var before the Datadog integration is initialized.
// If present at load time, the integration auto-enables.
static {
environmentVariables.set("_HANDLER", "Handler")
}
@Override
String service() {
null
}
def ig
def appSecStarted = false
def capturedMethod = null
def capturedPath = null
def capturedHeaders = [:]
def capturedBody = null
def appSecEnded = false
def setup() {
ig = AgentTracer.get().getCallbackProvider(RequestContextSlot.APPSEC)
ActiveSubsystems.APPSEC_ACTIVE = true
appSecStarted = false
capturedMethod = null
capturedPath = null
capturedHeaders = [:]
capturedBody = null
appSecEnded = false
ig.registerCallback(EVENTS.requestStarted(), {
appSecStarted = true
new Flow.ResultFlow(new Object())
} as Supplier)
ig.registerCallback(EVENTS.requestMethodUriRaw(), { RequestContext ctx, String method, URIDataAdapter uri ->
capturedMethod = method
capturedPath = uri.path()
Flow.ResultFlow.empty()
} as TriFunction)
ig.registerCallback(EVENTS.requestHeader(), { RequestContext ctx, String name, String value ->
capturedHeaders[name] = value
} as TriConsumer)
ig.registerCallback(EVENTS.requestHeaderDone(), { RequestContext ctx ->
Flow.ResultFlow.empty()
} as Function)
ig.registerCallback(EVENTS.requestBodyProcessed(), { RequestContext ctx, Object body ->
capturedBody = body
Flow.ResultFlow.empty()
} as BiFunction)
ig.registerCallback(EVENTS.requestEnded(), { RequestContext ctx, Object spanInfo ->
appSecEnded = true
Flow.ResultFlow.empty()
} as BiFunction)
}
def cleanup() {
ig.reset()
ActiveSubsystems.APPSEC_ACTIVE = false
}
def "test lambda streaming handler"() {
when:
def input = new ByteArrayInputStream(StandardCharsets.UTF_8.encode("Hello").array())
def output = new ByteArrayOutputStream()
def ctx = Stub(Context) {
getAwsRequestId() >> requestId
}
new HandlerStreaming().handleRequest(input, output, ctx)
then:
assertTraces(1) {
trace(1) {
span {
operationName operation()
spanType DDSpanTypes.SERVERLESS
errored false
}
}
}
}
def "test streaming handler with error"() {
when:
def input = new ByteArrayInputStream(StandardCharsets.UTF_8.encode("Hello").array())
def output = new ByteArrayOutputStream()
def ctx = Stub(Context) {
getAwsRequestId() >> requestId
}
new HandlerStreamingWithError().handleRequest(input, output, ctx)
then:
thrown(Error)
assertTraces(1) {
trace(1) {
span {
operationName operation()
spanType DDSpanTypes.SERVERLESS
errored true
tags {
tag "request_id", requestId
tag "error.type", "java.lang.Error"
tag "error.message", "Some error"
tag "error.stack", String
tag "language", "jvm"
tag "process_id", Long
tag "runtime-id", String
tag "thread.id", Long
tag "thread.name", String
tag "_dd.profiling.ctx", "test"
tag "_dd.profiling.enabled", 0
tag "_dd.agent_psr", 1.0
tag "_dd.tracer_host", String
tag "_sample_rate", 1
tag "_dd.trace_span_attribute_schema", { it != null }
}
}
}
}
}
def "appsec callbacks are invoked for API Gateway v1 event"() {
given:
def eventJson = """{
"path": "/api/users/123",
"headers": {"content-type": "application/json", "x-forwarded-for": "203.0.113.1"},
"body": "{\\"key\\": \\"value\\"}",
"requestContext": {
"httpMethod": "GET",
"requestId": "req-abc",
"identity": {"sourceIp": "203.0.113.1"}
}
}"""
when:
def input = new ByteArrayInputStream(eventJson.getBytes(StandardCharsets.UTF_8))
def output = new ByteArrayOutputStream()
def ctx = Stub(Context) { getAwsRequestId() >> requestId }
new HandlerStreaming().handleRequest(input, output, ctx)
then:
appSecStarted
capturedMethod == "GET"
capturedPath == "/api/users/123"
capturedHeaders["content-type"] == "application/json"
capturedBody instanceof Map
appSecEnded
assertTraces(1) {
trace(1) {
span {
operationName operation()
spanType DDSpanTypes.SERVERLESS
errored false
}
}
}
}
def "appsec callbacks are invoked for API Gateway v2 HTTP event"() {
given:
def eventJson = """{
"version": "2.0",
"headers": {"content-type": "application/json", "accept": "application/json"},
"cookies": ["session=abc123"],
"body": "{\\"key\\": \\"value\\"}",
"requestContext": {
"http": {
"method": "POST",
"path": "/api/items",
"sourceIp": "198.51.100.1"
},
"domainName": "api.example.com"
}
}"""
when:
def input = new ByteArrayInputStream(eventJson.getBytes(StandardCharsets.UTF_8))
def output = new ByteArrayOutputStream()
def ctx = Stub(Context) { getAwsRequestId() >> requestId }
new HandlerStreaming().handleRequest(input, output, ctx)
then:
appSecStarted
capturedMethod == "POST"
capturedPath == "/api/items"
capturedHeaders["content-type"] == "application/json"
capturedHeaders["cookie"] == "session=abc123"
capturedBody instanceof Map
appSecEnded
assertTraces(1) {
trace(1) {
span {
operationName operation()
spanType DDSpanTypes.SERVERLESS
errored false
}
}
}
}
def "appsec callbacks are not invoked when appsec is disabled"() {
given:
ActiveSubsystems.APPSEC_ACTIVE = false
when:
def eventJson = """{
"path": "/api/test",
"requestContext": {"httpMethod": "GET", "requestId": "req-xyz"}
}"""
def input = new ByteArrayInputStream(eventJson.getBytes(StandardCharsets.UTF_8))
def output = new ByteArrayOutputStream()
def ctx = Stub(Context) { getAwsRequestId() >> requestId }
new HandlerStreaming().handleRequest(input, output, ctx)
then:
!appSecStarted
capturedMethod == null
!appSecEnded
assertTraces(1) {
trace(1) {
span {
operationName operation()
spanType DDSpanTypes.SERVERLESS
errored false
}
}
}
}
}
class LambdaHandlerInstrumentationV0Test extends LambdaHandlerInstrumentationTest {
@Override
int version() {
0
}
@Override
String operation() {
"dd-tracer-serverless-span"
}
}
class LambdaHandlerInstrumentationV1ForkedTest extends LambdaHandlerInstrumentationTest {
@Override
int version() {
1
}
@Override
String operation() {
"aws.lambda.invoke"
}
}