@@ -31,6 +31,7 @@ import (
3131 "time"
3232
3333 "github.com/elastic/apm-aws-lambda/accumulator"
34+ "go.uber.org/zap"
3435)
3536
3637type jsonResult struct {
@@ -186,39 +187,23 @@ func (c *Client) PostToApmServer(ctx context.Context, apmData accumulator.APMDat
186187 return nil
187188 }
188189
189- jErr := jsonResult {}
190- if err := json .NewDecoder (resp .Body ).Decode (& jErr ); err != nil {
191- // non critical error.
192- // Log a warning and continue.
193- c .logger .Warnf ("failed to decode response body: %v" , err )
194- }
195-
196190 // Auth errors
197191 if resp .StatusCode == http .StatusUnauthorized || resp .StatusCode == http .StatusForbidden {
198- c .logger .Warnf ("Authentication with the APM server failed: response status code: %d" , resp .StatusCode )
199- for _ , err := range jErr .Errors {
200- c .logger .Warnf ("failed to authenticate: document %s: message: %s" , err .Document , err .Message )
201- }
192+ logBodyErrors (c .logger , resp )
202193 c .UpdateStatus (ctx , Failing )
203194 return nil
204195 }
205196
206197 // ClientErrors
207198 if resp .StatusCode >= 400 && resp .StatusCode < 500 {
208- c .logger .Warnf ("client error: response status code: %d" , resp .StatusCode )
209- for _ , err := range jErr .Errors {
210- c .logger .Warnf ("client error: document %s: message: %s" , err .Document , err .Message )
211- }
199+ logBodyErrors (c .logger , resp )
212200 c .UpdateStatus (ctx , ClientFailing )
213201 return nil
214202 }
215203
216204 // critical errors
217205 if resp .StatusCode == http .StatusInternalServerError || resp .StatusCode == http .StatusServiceUnavailable {
218- c .logger .Warnf ("failed to post data to APM server: response status code: %d" , resp .StatusCode )
219- for _ , err := range jErr .Errors {
220- c .logger .Warnf ("critical error: document %s: message: %s" , err .Document , err .Message )
221- }
206+ logBodyErrors (c .logger , resp )
222207 c .UpdateStatus (ctx , Failing )
223208 return nil
224209 }
@@ -227,6 +212,30 @@ func (c *Client) PostToApmServer(ctx context.Context, apmData accumulator.APMDat
227212 return nil
228213}
229214
215+ func logBodyErrors (logger * zap.SugaredLogger , resp * http.Response ) {
216+ b , err := io .ReadAll (resp .Body )
217+ if err != nil {
218+ logger .Warnf ("failed to post data to APM server: response status: %s: failed to read response body: %v" , resp .Status , err )
219+ return
220+ }
221+
222+ jErr := jsonResult {}
223+ if err := json .Unmarshal (b , & jErr ); err != nil {
224+ logger .Warnf ("failed to post data to APM server: response status: %s: failed to decode response body: %v: body: %s" , resp .Status , err , string (b ))
225+ return
226+ }
227+
228+ if len (jErr .Errors ) == 0 {
229+ logger .Warnf ("failed to post data to APM server: response status: %s: response body: %s" , resp .Status , string (b ))
230+ return
231+ }
232+
233+ logger .Warnf ("failed to post data to APM server: response status: %s" , resp .Status )
234+ for _ , err := range jErr .Errors {
235+ logger .Warnf ("document %s: message: %s" , err .Document , err .Message )
236+ }
237+ }
238+
230239// IsUnhealthy returns true if the apmproxy is not healthy.
231240func (c * Client ) IsUnhealthy () bool {
232241 c .mu .RLock ()
0 commit comments