From d791b7967bb652e91624f9c15ad2b9ea84cfe487 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?France=CC=81=20Wilke?= <francewilke@gmail.com>
Date: Mon, 20 Dec 2021 10:36:09 +0200
Subject: [PATCH] Truncate response body for incoming GET requests

---
 api_logs/api-logs.go | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/api_logs/api-logs.go b/api_logs/api-logs.go
index ce1cc05..28ddac5 100644
--- a/api_logs/api-logs.go
+++ b/api_logs/api-logs.go
@@ -3,6 +3,7 @@ package api_logs
 import (
 	"github.com/thoas/go-funk"
 	"gitlab.com/uafrica/go-utils/string_utils"
+	"net/http"
 	"net/url"
 	"strings"
 	"time"
@@ -18,6 +19,15 @@ func GenerateIncomingAPILog(startTime time.Time, requestID *string, claim map[st
 		currentRequestID = *requestID
 	}
 
+	// Truncate response body for incoming GET requests
+	responseBody := res.Body
+	if req.HTTPMethod == http.MethodGet {
+		// SQS has a 256KB limit, so let's limit the response to 96KB
+		if len(responseBody) > 12000 {
+			responseBody = responseBody[:12000] + "..."
+		}
+	}
+
 	var authType string
 	var authUsername string
 	if req.RequestContext.Identity.CognitoAuthenticationType != "" {
@@ -87,7 +97,7 @@ func GenerateIncomingAPILog(startTime time.Time, requestID *string, claim map[st
 		Response: ApiLogResponse{
 			Headers:  res.Headers,
 			BodySize: len(res.Body),
-			Body:     res.Body,
+			Body:     responseBody,
 		},
 	}
 
-- 
GitLab