lint up
This commit is contained in:
parent
78d63afbfa
commit
4e7d06c132
|
|
@ -1,9 +1,9 @@
|
||||||
language: go
|
language: go
|
||||||
go:
|
go:
|
||||||
- 1.14.x
|
- 1.15.x
|
||||||
before_install:
|
before_install:
|
||||||
# download super-linter: golangci-lint
|
# 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
|
- curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest
|
||||||
script:
|
script:
|
||||||
- golangci-lint run --enable-all
|
- golangci-lint run --enable-all -D exhaustivestruct,nlreturn
|
||||||
- go test ./...
|
- go test ./...
|
||||||
|
|
|
||||||
|
|
@ -8,17 +8,13 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
lokiPushPath = "/loki/api/v1/push"
|
lokiPushPath = "/loki/api/v1/push"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var errStatusCode = fmt.Errorf("unexpected HTTP status code")
|
||||||
errStatusCode = fmt.Errorf("unexpected HTTP status code")
|
|
||||||
)
|
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
*Config
|
*Config
|
||||||
|
|
@ -43,7 +39,7 @@ func (l *Loki) httpClient() *Client {
|
||||||
func (c *Client) Post(logs interface{}) error {
|
func (c *Client) Post(logs interface{}) error {
|
||||||
msg, err := json.Marshal(logs)
|
msg, err := json.Marshal(logs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("json marshal: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
u := strings.TrimSuffix(c.URL, lokiPushPath) + lokiPushPath
|
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),
|
m := fmt.Sprintf("%s (%d/%s) %s, msg: %s", u, code, http.StatusText(code),
|
||||||
strings.TrimSpace(strings.ReplaceAll(string(body), "\n", " ")), msg)
|
strings.TrimSpace(strings.ReplaceAll(string(body), "\n", " ")), msg)
|
||||||
|
|
||||||
return errors.Wrap(errStatusCode, m)
|
return fmt.Errorf("%s: %w", m, errStatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -67,9 +63,9 @@ func (c *Client) Post(logs interface{}) error {
|
||||||
|
|
||||||
// NewRequest creates the http request based on input data.
|
// NewRequest creates the http request based on input data.
|
||||||
func (c *Client) NewRequest(url, method, cType string, msg []byte) (*http.Request, error) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("creating request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cType != "" {
|
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) {
|
func (c *Client) Do(req *http.Request) (int, []byte, error) {
|
||||||
resp, err := c.Client.Do(req)
|
resp, err := c.Client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, fmt.Errorf("making request: %w", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
return resp.StatusCode, body, err
|
return resp.StatusCode, body, fmt.Errorf("reading body: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.StatusCode, body, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
package lokiunifi
|
package lokiunifi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/unifi-poller/poller"
|
"github.com/unifi-poller/poller"
|
||||||
"golift.io/cnfg"
|
"golift.io/cnfg"
|
||||||
)
|
)
|
||||||
|
|
@ -126,7 +126,7 @@ func (l *Loki) ProcessEvents(report *Report, events *poller.Events) error {
|
||||||
|
|
||||||
logs := report.ProcessEventLogs(events)
|
logs := report.ProcessEventLogs(events)
|
||||||
if err := l.client.Post(logs); err != nil {
|
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
|
l.last = report.Start
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue