@@ -93,13 +93,11 @@ func CaptureInternalJSONExchange(
9393 return
9494 }
9595
96- if req := internalJSONAuditRequest (ctx , method , path , requestIDForEntry (entry ), requestBody ); req != nil {
96+ if req := internalJSONAuditRequest (ctx , method , path , requestIDForEntry (entry ), requestBody , cfg . LogBodies ); req != nil {
9797 PopulateRequestData (entry , req , cfg )
9898 }
99- headers , body , truncated , ok := internalJSONAuditResponse (responseBody , responseErr , requestIDForEntry (entry ))
100- if ok {
101- PopulateResponseData (entry , headers , body , truncated , cfg )
102- }
99+ headers , body , truncated := internalJSONAuditResponse (responseBody , responseErr , requestIDForEntry (entry ), cfg .LogBodies )
100+ PopulateResponseData (entry , headers , body , truncated , cfg )
103101}
104102
105103func ensureLogData (entry * LogEntry ) * LogData {
@@ -116,40 +114,47 @@ func requestIDForEntry(entry *LogEntry) string {
116114 return strings .TrimSpace (entry .RequestID )
117115}
118116
119- func internalJSONAuditRequest (ctx context.Context , method , path , requestID string , bodyValue any ) * http.Request {
120- if bodyValue == nil {
121- return nil
122- }
123- body , err := json .Marshal (bodyValue )
124- if err != nil {
125- return nil
126- }
127-
117+ func internalJSONAuditRequest (ctx context.Context , method , path , requestID string , bodyValue any , logBodies bool ) * http.Request {
128118 headers := internalJSONAuditHeaders (ctx , requestID )
129- capturedBody , bodyTooBig := boundedAuditBody (body , false )
130- snapshot := core .NewRequestSnapshot (
131- method ,
132- path ,
133- nil ,
134- nil ,
135- headers ,
136- headers .Get ("Content-Type" ),
137- capturedBody ,
138- bodyTooBig ,
139- requestID ,
140- nil ,
141- core .UserPathFromContext (ctx ),
142- )
143- reqCtx := core .WithRequestSnapshot (ctx , snapshot )
144119 req := & http.Request {
145120 Method : method ,
146121 URL : & url.URL {Path : path },
147122 Header : headers ,
148123 }
124+ reqCtx := ctx
125+ if logBodies && bodyValue != nil {
126+ if body , err := json .Marshal (bodyValue ); err == nil {
127+ capturedBody , bodyTooBig := boundedAuditBody (body , false )
128+ snapshot := core .NewRequestSnapshot (
129+ method ,
130+ path ,
131+ nil ,
132+ nil ,
133+ headers ,
134+ headers .Get ("Content-Type" ),
135+ capturedBody ,
136+ bodyTooBig ,
137+ requestID ,
138+ nil ,
139+ core .UserPathFromContext (ctx ),
140+ )
141+ reqCtx = core .WithRequestSnapshot (ctx , snapshot )
142+ }
143+ }
149144 return req .WithContext (reqCtx )
150145}
151146
152- func internalJSONAuditResponse (bodyValue any , responseErr error , requestID string ) (http.Header , []byte , bool , bool ) {
147+ func internalJSONAuditResponse (bodyValue any , responseErr error , requestID string , logBodies bool ) (http.Header , []byte , bool ) {
148+ headers := make (http.Header )
149+ headers .Set ("Content-Type" , "application/json" )
150+ if requestID != "" {
151+ headers .Set ("X-Request-ID" , requestID )
152+ }
153+
154+ if ! logBodies {
155+ return headers , nil , false
156+ }
157+
153158 var (
154159 body []byte
155160 err error
@@ -165,19 +170,13 @@ func internalJSONAuditResponse(bodyValue any, responseErr error, requestID strin
165170 body , err = json .Marshal (core .NewProviderError ("" , http .StatusInternalServerError , responseErr .Error (), responseErr ).ToJSON ())
166171 }
167172 default :
168- return nil , nil , false , false
173+ return headers , nil , false
169174 }
170175 if err != nil {
171- return nil , nil , false , false
172- }
173-
174- headers := make (http.Header )
175- headers .Set ("Content-Type" , "application/json" )
176- if requestID != "" {
177- headers .Set ("X-Request-ID" , requestID )
176+ return headers , nil , false
178177 }
179178 capturedBody , truncated := boundedAuditBody (body , true )
180- return headers , capturedBody , truncated , true
179+ return headers , capturedBody , truncated
181180}
182181
183182func internalJSONAuditHeaders (ctx context.Context , requestID string ) http.Header {
0 commit comments