| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otellambda // import "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"encoding/json" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"reflect" |
| 12 |
) |
| 13 |
|
| 14 |
// wrappedHandlerFunction is a struct which only holds an instrumentor and is |
| 15 |
// able to instrument invocations of the user's lambda handler function. |
| 16 |
type wrappedHandlerFunction struct { |
| 17 |
instrumentor instrumentor |
| 18 |
} |
| 19 |
|
| 20 |
func errorHandler(e error) func(context.Context, any) (any, error) { |
| 21 |
return func(context.Context, any) (any, error) { |
| 22 |
return nil, e |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
// Ensure handler takes 0-2 values, with context |
| 27 |
// as its first value if two arguments exist. |
| 28 |
func validateArguments(handler reflect.Type) (bool, error) { |
| 29 |
handlerTakesContext := false |
| 30 |
if handler.NumIn() > 2 { |
| 31 |
return false, fmt.Errorf("handlers may not take more than two arguments, but handler takes %d", handler.NumIn()) |
| 32 |
} else if handler.NumIn() > 0 { |
| 33 |
contextType := reflect.TypeFor[context.Context]() |
| 34 |
argumentType := handler.In(0) |
| 35 |
handlerTakesContext = argumentType.Implements(contextType) |
| 36 |
if handler.NumIn() > 1 && !handlerTakesContext { |
| 37 |
return false, fmt.Errorf("handler takes two arguments, but the first is not Context. got %s", argumentType.Kind()) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
return handlerTakesContext, nil |
| 42 |
} |
| 43 |
|
| 44 |
// Ensure handler returns 0-2 values, with an error |
| 45 |
// as its first value if any exist. |
| 46 |
func validateReturns(handler reflect.Type) error { |
| 47 |
errorType := reflect.TypeFor[error]() |
| 48 |
|
| 49 |
switch n := handler.NumOut(); { |
| 50 |
case n > 2: |
| 51 |
return errors.New("handler may not return more than two values") |
| 52 |
case n == 2: |
| 53 |
if !handler.Out(1).Implements(errorType) { |
| 54 |
return errors.New("handler returns two values, but the second does not implement error") |
| 55 |
} |
| 56 |
case n == 1: |
| 57 |
if !handler.Out(0).Implements(errorType) { |
| 58 |
return errors.New("handler returns a single value, but it does not implement error") |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return nil |
| 63 |
} |
| 64 |
|
| 65 |
// Wraps and calls customer lambda handler then unpacks response as necessary. |
| 66 |
func (whf *wrappedHandlerFunction) wrapperInternals(ctx context.Context, handlerFunc any, eventJSON []byte, event reflect.Value, takesContext bool) (any, error) { |
| 67 |
wrappedLambdaHandler := reflect.ValueOf(whf.wrapper(handlerFunc)) |
| 68 |
|
| 69 |
argsWrapped := []reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(eventJSON), event, reflect.ValueOf(takesContext)} |
| 70 |
response := wrappedLambdaHandler.Call(argsWrapped)[0].Interface().([]reflect.Value) |
| 71 |
|
| 72 |
// convert return values into (any, error) |
| 73 |
var err error |
| 74 |
if len(response) > 0 { |
| 75 |
if errVal, ok := response[len(response)-1].Interface().(error); ok { |
| 76 |
err = errVal |
| 77 |
} |
| 78 |
} |
| 79 |
var val any |
| 80 |
if len(response) > 1 { |
| 81 |
val = response[0].Interface() |
| 82 |
} |
| 83 |
|
| 84 |
return val, err |
| 85 |
} |
| 86 |
|
| 87 |
// InstrumentHandler Provides a lambda handler which wraps customer lambda handler with OTel Tracing. |
| 88 |
func InstrumentHandler(handlerFunc any, options ...Option) any { |
| 89 |
whf := wrappedHandlerFunction{instrumentor: newInstrumentor(options...)} |
| 90 |
|
| 91 |
if handlerFunc == nil { |
| 92 |
return errorHandler(errors.New("handler is nil")) |
| 93 |
} |
| 94 |
handlerType := reflect.TypeOf(handlerFunc) |
| 95 |
if handlerType.Kind() != reflect.Func { |
| 96 |
return errorHandler(fmt.Errorf("handler kind %s is not %s", handlerType.Kind(), reflect.Func)) |
| 97 |
} |
| 98 |
|
| 99 |
takesContext, err := validateArguments(handlerType) |
| 100 |
if err != nil { |
| 101 |
return errorHandler(err) |
| 102 |
} |
| 103 |
|
| 104 |
if err := validateReturns(handlerType); err != nil { |
| 105 |
return errorHandler(err) |
| 106 |
} |
| 107 |
|
| 108 |
// note we will always take context to capture lambda context, |
| 109 |
// regardless of whether customer takes context |
| 110 |
if handlerType.NumIn() == 0 || handlerType.NumIn() == 1 && takesContext { |
| 111 |
return func(ctx context.Context) (any, error) { |
| 112 |
var temp *any |
| 113 |
event := reflect.ValueOf(temp) |
| 114 |
return whf.wrapperInternals(ctx, handlerFunc, []byte{}, event, takesContext) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// customer either takes both context and payload or just payload |
| 119 |
return func(ctx context.Context, payload any) (any, error) { |
| 120 |
event := reflect.New(handlerType.In(handlerType.NumIn() - 1)) |
| 121 |
|
| 122 |
// lambda SDK normally unmarshalls to customer event type, however |
| 123 |
// with the wrapper the SDK unmarshalls to map[string]any |
| 124 |
// due to our use of reflection. Therefore we must convert this map |
| 125 |
// to customer's desired event, we do so by simply re-marshaling then |
| 126 |
// unmarshalling to the desired event type. The remarshalledPayload |
| 127 |
// will also be used by users using custom propagators |
| 128 |
remarshalledPayload, err := json.Marshal(payload) |
| 129 |
if err != nil { |
| 130 |
return nil, err |
| 131 |
} |
| 132 |
|
| 133 |
if err := json.Unmarshal(remarshalledPayload, event.Interface()); err != nil { |
| 134 |
return nil, err |
| 135 |
} |
| 136 |
|
| 137 |
return whf.wrapperInternals(ctx, handlerFunc, remarshalledPayload, event.Elem(), takesContext) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// Adds OTel span surrounding customer handler call. |
| 142 |
func (whf *wrappedHandlerFunction) wrapper(handlerFunc any) func(ctx context.Context, eventJSON []byte, event any, takesContext bool) []reflect.Value { |
| 143 |
return func(ctx context.Context, eventJSON []byte, event any, takesContext bool) []reflect.Value { |
| 144 |
ctx, span := whf.instrumentor.tracingBegin(ctx, eventJSON) |
| 145 |
defer whf.instrumentor.tracingEnd(ctx, span) |
| 146 |
|
| 147 |
handler := reflect.ValueOf(handlerFunc) |
| 148 |
var args []reflect.Value |
| 149 |
if takesContext { |
| 150 |
args = append(args, reflect.ValueOf(ctx)) |
| 151 |
} |
| 152 |
if eventExists(event) { |
| 153 |
args = append(args, reflect.ValueOf(event)) |
| 154 |
} |
| 155 |
|
| 156 |
response := handler.Call(args) |
| 157 |
|
| 158 |
return response |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// Determine if an any is nil or the |
| 163 |
// if the reflect.Value of the event is nil. |
| 164 |
func eventExists(event any) bool { |
| 165 |
if event == nil { |
| 166 |
return false |
| 167 |
} |
| 168 |
|
| 169 |
// reflect.Value.isNil() can only be called on |
| 170 |
// Values of certain Kinds. Unsupported Kinds |
| 171 |
// will panic rather than return false |
| 172 |
switch reflect.TypeOf(event).Kind() { |
| 173 |
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice: |
| 174 |
return !reflect.ValueOf(event).IsNil() |
| 175 |
} |
| 176 |
return true |
| 177 |
} |