| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otellambda |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"encoding/json" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"os" |
| 12 |
"reflect" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/aws/aws-lambda-go/lambda" |
| 16 |
"github.com/aws/aws-lambda-go/lambda/messages" |
| 17 |
"github.com/aws/aws-lambda-go/lambdacontext" |
| 18 |
"github.com/stretchr/testify/assert" |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
mockLambdaContext = lambdacontext.LambdaContext{ |
| 23 |
AwsRequestID: "123", |
| 24 |
InvokedFunctionArn: "arn:partition:service:region:account-id:resource-type:resource-id", |
| 25 |
Identity: lambdacontext.CognitoIdentity{ |
| 26 |
CognitoIdentityID: "someId", |
| 27 |
CognitoIdentityPoolID: "somePoolId", |
| 28 |
}, |
| 29 |
ClientContext: lambdacontext.ClientContext{}, |
| 30 |
} |
| 31 |
mockContext = lambdacontext.NewContext(context.TODO(), &mockLambdaContext) |
| 32 |
) |
| 33 |
|
| 34 |
type emptyHandler struct{} |
| 35 |
|
| 36 |
func (emptyHandler) Invoke(context.Context, []byte) ([]byte, error) { |
| 37 |
return nil, nil |
| 38 |
} |
| 39 |
|
| 40 |
var _ lambda.Handler = emptyHandler{} |
| 41 |
|
| 42 |
func setEnvVars() { |
| 43 |
_ = os.Setenv("AWS_LAMBDA_FUNCTION_NAME", "testFunction") |
| 44 |
_ = os.Setenv("AWS_REGION", "us-texas-1") |
| 45 |
_ = os.Setenv("AWS_LAMBDA_FUNCTION_VERSION", "$LATEST") |
| 46 |
_ = os.Setenv("AWS_LAMBDA_LOG_STREAM_NAME", "2023/01/01/[$LATEST]5d1edb9e525d486696cf01a3503487bc") |
| 47 |
_ = os.Setenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128") |
| 48 |
_ = os.Setenv("_X_AMZN_TRACE_ID", "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1") |
| 49 |
} |
| 50 |
|
| 51 |
func TestLambdaHandlerSignatures(t *testing.T) { |
| 52 |
setEnvVars() |
| 53 |
|
| 54 |
emptyPayload := "" |
| 55 |
testCases := []struct { |
| 56 |
name string |
| 57 |
handler any |
| 58 |
expected error |
| 59 |
args []reflect.Value |
| 60 |
}{ |
| 61 |
{ |
| 62 |
name: "nil handler", |
| 63 |
expected: errors.New("handler is nil"), |
| 64 |
handler: nil, |
| 65 |
args: []reflect.Value{reflect.ValueOf(mockContext), reflect.ValueOf(emptyPayload)}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "handler is not a function", |
| 69 |
expected: errors.New("handler kind struct is not func"), |
| 70 |
handler: struct{}{}, |
| 71 |
args: []reflect.Value{reflect.ValueOf(mockContext), reflect.ValueOf(emptyPayload)}, |
| 72 |
}, |
| 73 |
{ |
| 74 |
name: "handler declares too many arguments", |
| 75 |
expected: errors.New("handlers may not take more than two arguments, but handler takes 3"), |
| 76 |
handler: func(context.Context, string, string) error { |
| 77 |
return nil |
| 78 |
}, |
| 79 |
args: []reflect.Value{reflect.ValueOf(mockContext), reflect.ValueOf(emptyPayload)}, |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "two argument handler does not have context as first argument", |
| 83 |
expected: errors.New("handler takes two arguments, but the first is not Context. got string"), |
| 84 |
handler: func(string, context.Context) error { |
| 85 |
return nil |
| 86 |
}, |
| 87 |
args: []reflect.Value{reflect.ValueOf(mockContext), reflect.ValueOf(emptyPayload)}, |
| 88 |
}, |
| 89 |
{ |
| 90 |
name: "handler returns too many values", |
| 91 |
expected: errors.New("handler may not return more than two values"), |
| 92 |
handler: func() (error, error, error) { |
| 93 |
return nil, nil, nil |
| 94 |
}, |
| 95 |
args: []reflect.Value{reflect.ValueOf(mockContext), reflect.ValueOf(emptyPayload)}, |
| 96 |
}, |
| 97 |
{ |
| 98 |
name: "handler returning two values does not declare error as the second return value", |
| 99 |
expected: errors.New("handler returns two values, but the second does not implement error"), |
| 100 |
handler: func() (error, string) { //nolint:staticcheck // Tests error first. |
| 101 |
return nil, "hello" |
| 102 |
}, |
| 103 |
args: []reflect.Value{reflect.ValueOf(mockContext), reflect.ValueOf(emptyPayload)}, |
| 104 |
}, |
| 105 |
{ |
| 106 |
name: "handler returning a single value does not implement error", |
| 107 |
expected: errors.New("handler returns a single value, but it does not implement error"), |
| 108 |
handler: func() string { |
| 109 |
return "hello" |
| 110 |
}, |
| 111 |
args: []reflect.Value{reflect.ValueOf(mockContext), reflect.ValueOf(emptyPayload)}, |
| 112 |
}, |
| 113 |
{ |
| 114 |
name: "no args or return value should not result in error", |
| 115 |
expected: nil, |
| 116 |
handler: func() { |
| 117 |
}, |
| 118 |
args: []reflect.Value{reflect.ValueOf(mockContext)}, // reminder - customer takes no args but wrapped handler always takes context from lambda |
| 119 |
}, |
| 120 |
} |
| 121 |
for i, testCase := range testCases { |
| 122 |
t.Run(fmt.Sprintf("testCase[%d] %s", i, testCase.name), func(t *testing.T) { |
| 123 |
lambdaHandler := InstrumentHandler(testCase.handler) |
| 124 |
handler := reflect.ValueOf(lambdaHandler) |
| 125 |
resp := handler.Call(testCase.args) |
| 126 |
assert.Len(t, resp, 2) |
| 127 |
assert.Equal(t, testCase.expected, resp[1].Interface()) |
| 128 |
}) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
type expected struct { |
| 133 |
val any |
| 134 |
err error |
| 135 |
} |
| 136 |
|
| 137 |
func TestHandlerInvokes(t *testing.T) { |
| 138 |
setEnvVars() |
| 139 |
|
| 140 |
hello := func(s string) string { |
| 141 |
return fmt.Sprintf("Hello %s!", s) |
| 142 |
} |
| 143 |
|
| 144 |
testCases := []struct { |
| 145 |
name string |
| 146 |
input any |
| 147 |
expected expected |
| 148 |
handler any |
| 149 |
}{ |
| 150 |
{ |
| 151 |
name: "string input and return without context", |
| 152 |
input: "Lambda", |
| 153 |
expected: expected{`"Hello Lambda!"`, nil}, |
| 154 |
handler: func(name string) (string, error) { |
| 155 |
return hello(name), nil |
| 156 |
}, |
| 157 |
}, |
| 158 |
{ |
| 159 |
name: "string input and return with context", |
| 160 |
input: "Lambda", |
| 161 |
expected: expected{`"Hello Lambda!"`, nil}, |
| 162 |
handler: func(_ context.Context, name string) (string, error) { |
| 163 |
return hello(name), nil |
| 164 |
}, |
| 165 |
}, |
| 166 |
{ |
| 167 |
name: "no input with response event and simple error", |
| 168 |
input: nil, |
| 169 |
expected: expected{"", errors.New("bad stuff")}, |
| 170 |
handler: func() (any, error) { |
| 171 |
return nil, errors.New("bad stuff") |
| 172 |
}, |
| 173 |
}, |
| 174 |
{ |
| 175 |
name: "input with response event and simple error", |
| 176 |
input: "Lambda", |
| 177 |
expected: expected{"", errors.New("bad stuff")}, |
| 178 |
handler: func(any) (any, error) { |
| 179 |
return nil, errors.New("bad stuff") |
| 180 |
}, |
| 181 |
}, |
| 182 |
{ |
| 183 |
name: "input and context with response event and simple error", |
| 184 |
input: "Lambda", |
| 185 |
expected: expected{"", errors.New("bad stuff")}, |
| 186 |
handler: func(context.Context, any) (any, error) { |
| 187 |
return nil, errors.New("bad stuff") |
| 188 |
}, |
| 189 |
}, |
| 190 |
{ |
| 191 |
name: "input with response event and complex error", |
| 192 |
input: "Lambda", |
| 193 |
expected: expected{"", messages.InvokeResponse_Error{Message: "message", Type: "type"}}, |
| 194 |
handler: func(any) (any, error) { |
| 195 |
return nil, messages.InvokeResponse_Error{Message: "message", Type: "type"} |
| 196 |
}, |
| 197 |
}, |
| 198 |
{ |
| 199 |
name: "basic input struct serialization", |
| 200 |
input: struct{ Custom int }{9001}, |
| 201 |
expected: expected{`9001`, nil}, |
| 202 |
handler: func(event struct{ Custom int }) (int, error) { |
| 203 |
return event.Custom, nil |
| 204 |
}, |
| 205 |
}, |
| 206 |
{ |
| 207 |
name: "basic output struct serialization", |
| 208 |
input: 9001, |
| 209 |
expected: expected{`{"Number":9001}`, nil}, |
| 210 |
handler: func(event int) (struct{ Number int }, error) { |
| 211 |
return struct{ Number int }{event}, nil |
| 212 |
}, |
| 213 |
}, |
| 214 |
} |
| 215 |
|
| 216 |
// test invocation via a lambda handler |
| 217 |
for i, testCase := range testCases { |
| 218 |
t.Run(fmt.Sprintf("lambdaHandlerTestCase[%d] %s", i, testCase.name), func(t *testing.T) { |
| 219 |
lambdaHandler := InstrumentHandler(testCase.handler) |
| 220 |
handler := reflect.ValueOf(lambdaHandler) |
| 221 |
handlerType := handler.Type() |
| 222 |
|
| 223 |
var args []reflect.Value |
| 224 |
args = append(args, reflect.ValueOf(mockContext)) |
| 225 |
if handlerType.NumIn() > 1 { |
| 226 |
args = append(args, reflect.ValueOf(testCase.input)) |
| 227 |
} |
| 228 |
response := handler.Call(args) |
| 229 |
assert.Len(t, response, 2) |
| 230 |
if testCase.expected.err != nil { |
| 231 |
assert.Equal(t, testCase.expected.err, response[handlerType.NumOut()-1].Interface()) |
| 232 |
} else { |
| 233 |
assert.Nil(t, response[handlerType.NumOut()-1].Interface()) |
| 234 |
responseValMarshalled, _ := json.Marshal(response[0].Interface()) |
| 235 |
assert.Equal(t, testCase.expected.val, string(responseValMarshalled)) |
| 236 |
} |
| 237 |
}) |
| 238 |
} |
| 239 |
|
| 240 |
// test invocation via a Handler |
| 241 |
for i, testCase := range testCases { |
| 242 |
t.Run(fmt.Sprintf("handlerTestCase[%d] %s", i, testCase.name), func(t *testing.T) { |
| 243 |
handler := WrapHandler(lambda.NewHandler(testCase.handler)) |
| 244 |
inputPayload, _ := json.Marshal(testCase.input) |
| 245 |
response, err := handler.Invoke(mockContext, inputPayload) |
| 246 |
if testCase.expected.err != nil { |
| 247 |
assert.Equal(t, testCase.expected.err, err) |
| 248 |
} else { |
| 249 |
assert.NoError(t, err) |
| 250 |
assert.Equal(t, testCase.expected.val, string(response)) |
| 251 |
} |
| 252 |
}) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
func BenchmarkInstrumentHandler(b *testing.B) { |
| 257 |
setEnvVars() |
| 258 |
|
| 259 |
customerHandler := func(context.Context, int) error { |
| 260 |
return nil |
| 261 |
} |
| 262 |
wrapped := InstrumentHandler(customerHandler) |
| 263 |
wrappedCallable := reflect.ValueOf(wrapped) |
| 264 |
ctx := reflect.ValueOf(mockContext) |
| 265 |
payload := reflect.ValueOf(0) |
| 266 |
args := []reflect.Value{ctx, payload} |
| 267 |
|
| 268 |
b.ResetTimer() |
| 269 |
for range b.N { |
| 270 |
wrappedCallable.Call(args) |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
func BenchmarkWrapHandler(b *testing.B) { |
| 275 |
setEnvVars() |
| 276 |
|
| 277 |
wrapped := WrapHandler(emptyHandler{}) |
| 278 |
|
| 279 |
b.ResetTimer() |
| 280 |
for range b.N { |
| 281 |
_, _ = wrapped.Invoke(mockContext, []byte{0}) |
| 282 |
} |
| 283 |
} |