Skip to content

Commit

Permalink
Enhance error message returned from Loki
Browse files Browse the repository at this point in the history
Before when getting http status code other than 204,
only the response http headers would get printed.
This commit adds also the body to the log message,
which includes error description sent by Loki.
  • Loading branch information
vyzigold committed Jul 29, 2021
1 parent 9960b6c commit ab1236e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions connector/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -301,9 +303,17 @@ func (client *LokiConnector) send() (*http.Response, error) {
}
defer response.Body.Close()
if response.StatusCode != 204 {
buffer := new(strings.Builder)
body := ""
_, errCopy := io.Copy(buffer, response.Body)
if errCopy == nil {
body = buffer.String()
}

client.logger.Metadata(map[string]interface{}{
"error": err,
"response": response,
"error": err,
"response headers": response,
"response body": body,
})
client.logger.Error("Recieved unexpected statuscode when trying to send logs")
return nil, fmt.Errorf("Got %d http status code after pushing to loki instead of expected 204", response.StatusCode)
Expand Down

0 comments on commit ab1236e

Please sign in to comment.