@@ -11,8 +11,11 @@ import (
1111 "github.com/aws/aws-sdk-go-v2/config"
1212 "github.com/aws/aws-sdk-go/aws/session"
1313 "net/http"
14+ "os"
1415)
1516
17+ var DEBUGDumpPayload = os .Getenv ("DEBUG_DUMP_PAYLOAD" )
18+
1619const DefaultNonHTTPEventPath = "/events"
1720
1821type LambdaHandlerOption func (handler * LambdaHandler )
@@ -179,6 +182,17 @@ func (l *LambdaHandler) InvokeWebsocketAPI(ctx context.Context, request *events.
179182}
180183
181184func (l * LambdaHandler ) Invoke (ctx context.Context , payload []byte ) ([]byte , error ) {
185+ if DEBUGDumpPayload != "" && (DEBUGDumpPayload == "1" || DEBUGDumpPayload == "true" ) {
186+ ctx = handlertrace .NewContext (ctx , handlertrace.HandlerTrace {
187+ RequestEvent : func (ctx context.Context , payload interface {}) {
188+ fmt .Printf ("Request payload: %s\n " , payload )
189+ },
190+ ResponseEvent : func (ctx context.Context , payload interface {}) {
191+ fmt .Printf ("Response payload: %s\n " , payload )
192+ },
193+ })
194+ }
195+
182196 trace := handlertrace .FromContext (ctx )
183197
184198 var (
@@ -187,6 +201,10 @@ func (l *LambdaHandler) Invoke(ctx context.Context, payload []byte) ([]byte, err
187201 err error
188202 )
189203
204+ if trace .RequestEvent != nil {
205+ trace .RequestEvent (ctx , payload )
206+ }
207+
190208 if err = json .Unmarshal (payload , & checker ); err != nil {
191209 res , err = l .HandleNonHTTPEvent (ctx , payload , http .DetectContentType (payload ))
192210 } else {
@@ -196,18 +214,12 @@ func (l *LambdaHandler) Invoke(ctx context.Context, payload []byte) ([]byte, err
196214 if err := json .Unmarshal (payload , event ); err != nil {
197215 return nil , err
198216 }
199- if trace .RequestEvent != nil {
200- trace .RequestEvent (ctx , payload )
201- }
202217 res , err = l .InvokeRESTAPI (ctx , event )
203218 case APIGatewayHTTPIntegration :
204219 event := & events.APIGatewayV2HTTPRequest {}
205220 if err := json .Unmarshal (payload , event ); err != nil {
206221 return nil , err
207222 }
208- if trace .RequestEvent != nil {
209- trace .RequestEvent (ctx , payload )
210- }
211223 res , err = l .InvokeHTTPAPI (ctx , event )
212224 case ALBTargetGroupIntegration :
213225 event := & events.ALBTargetGroupRequest {}
@@ -233,16 +245,18 @@ func (l *LambdaHandler) Invoke(ctx context.Context, payload []byte) ([]byte, err
233245 return nil , err
234246 }
235247
236- if trace .ResponseEvent != nil {
237- trace .ResponseEvent (ctx , res )
238- }
239-
240248 if b , ok := res .([]byte ); ok {
249+ if trace .ResponseEvent != nil {
250+ trace .ResponseEvent (ctx , b )
251+ }
241252 return b , nil
242253 } else {
243254 if responseBytes , err := json .Marshal (res ); err != nil {
244255 return nil , err
245256 } else {
257+ if trace .ResponseEvent != nil {
258+ trace .ResponseEvent (ctx , responseBytes )
259+ }
246260 return responseBytes , nil
247261 }
248262 }
0 commit comments