This commit is contained in:
David Newhall II 2021-03-07 15:55:48 -08:00
parent 78d63afbfa
commit 4e7d06c132
3 changed files with 14 additions and 15 deletions

View File

@ -1,9 +1,9 @@
language: go
go:
- 1.14.x
- 1.15.x
before_install:
# download super-linter: golangci-lint
- curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest
script:
- golangci-lint run --enable-all
- golangci-lint run --enable-all -D exhaustivestruct,nlreturn
- go test ./...

View File

@ -8,17 +8,13 @@ import (
"io/ioutil"
"net/http"
"strings"
"github.com/pkg/errors"
)
const (
lokiPushPath = "/loki/api/v1/push"
)
var (
errStatusCode = fmt.Errorf("unexpected HTTP status code")
)
var errStatusCode = fmt.Errorf("unexpected HTTP status code")
type Client struct {
*Config
@ -43,7 +39,7 @@ func (l *Loki) httpClient() *Client {
func (c *Client) Post(logs interface{}) error {
msg, err := json.Marshal(logs)
if err != nil {
return err
return fmt.Errorf("json marshal: %w", err)
}
u := strings.TrimSuffix(c.URL, lokiPushPath) + lokiPushPath
@ -59,7 +55,7 @@ func (c *Client) Post(logs interface{}) error {
m := fmt.Sprintf("%s (%d/%s) %s, msg: %s", u, code, http.StatusText(code),
strings.TrimSpace(strings.ReplaceAll(string(body), "\n", " ")), msg)
return errors.Wrap(errStatusCode, m)
return fmt.Errorf("%s: %w", m, errStatusCode)
}
return nil
@ -67,9 +63,9 @@ func (c *Client) Post(logs interface{}) error {
// NewRequest creates the http request based on input data.
func (c *Client) NewRequest(url, method, cType string, msg []byte) (*http.Request, error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(msg))
req, err := http.NewRequest(method, url, bytes.NewBuffer(msg)) //nolint:noctx
if err != nil {
return nil, err
return nil, fmt.Errorf("creating request: %w", err)
}
if cType != "" {
@ -91,11 +87,14 @@ func (c *Client) NewRequest(url, method, cType string, msg []byte) (*http.Reques
func (c *Client) Do(req *http.Request) (int, []byte, error) {
resp, err := c.Client.Do(req)
if err != nil {
return 0, nil, err
return 0, nil, fmt.Errorf("making request: %w", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return resp.StatusCode, body, fmt.Errorf("reading body: %w", err)
}
return resp.StatusCode, body, err
return resp.StatusCode, body, nil
}

View File

@ -1,11 +1,11 @@
package lokiunifi
import (
"fmt"
"io/ioutil"
"strings"
"time"
"github.com/pkg/errors"
"github.com/unifi-poller/poller"
"golift.io/cnfg"
)
@ -126,7 +126,7 @@ func (l *Loki) ProcessEvents(report *Report, events *poller.Events) error {
logs := report.ProcessEventLogs(events)
if err := l.client.Post(logs); err != nil {
return errors.Wrap(err, "sending to Loki failed")
return fmt.Errorf("sending to Loki failed: %w", err)
}
l.last = report.Start