@@ -31,6 +31,7 @@ import (
31
31
"time"
32
32
33
33
"github.com/elastic/apm-aws-lambda/accumulator"
34
+ "go.uber.org/zap"
34
35
)
35
36
36
37
type jsonResult struct {
@@ -186,39 +187,23 @@ func (c *Client) PostToApmServer(ctx context.Context, apmData accumulator.APMDat
186
187
return nil
187
188
}
188
189
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
-
196
190
// Auth errors
197
191
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 )
202
193
c .UpdateStatus (ctx , Failing )
203
194
return nil
204
195
}
205
196
206
197
// ClientErrors
207
198
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 )
212
200
c .UpdateStatus (ctx , ClientFailing )
213
201
return nil
214
202
}
215
203
216
204
// critical errors
217
205
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 )
222
207
c .UpdateStatus (ctx , Failing )
223
208
return nil
224
209
}
@@ -227,6 +212,30 @@ func (c *Client) PostToApmServer(ctx context.Context, apmData accumulator.APMDat
227
212
return nil
228
213
}
229
214
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
+
230
239
// IsUnhealthy returns true if the apmproxy is not healthy.
231
240
func (c * Client ) IsUnhealthy () bool {
232
241
c .mu .RLock ()
0 commit comments