Skip to content

Commit

Permalink
Switch to use slog instead of logrus
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Oct 12, 2023
1 parent ba96f58 commit ea99949
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module github.com/nyaruka/librato

go 1.13
go 1.21

require github.com/stretchr/testify v1.4.0

require (
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.4.0
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
11 changes: 2 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
20 changes: 9 additions & 11 deletions librato.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log/slog"
"net/http"
"strings"
"sync"
"time"

"github.com/sirupsen/logrus"
)

// The endpoint we post librato to
Expand Down Expand Up @@ -66,14 +64,14 @@ func (c *collector) Start() {
c.waitGroup.Add(1)
defer c.waitGroup.Done()

logrus.WithField("comp", "librato").Info("started for username ", c.username)
slog.Info("started collector", "username", c.username, "comp", "librato")
for {
select {
case <-c.stop:
for len(c.buffer) > 0 {
c.flush(250)
}
logrus.WithField("comp", "librato").Info("stopped")
slog.Info("stopped", "comp", "librato")
return

case <-time.After(c.timeout):
Expand All @@ -89,7 +87,7 @@ func (c *collector) Start() {
func (c *collector) Gauge(name string, value float64) {
// our buffer is full, log an error but continue
if len(c.buffer) >= cap(c.buffer) {
logrus.Error("unable to add new gauges, buffer full, you may want to increase your buffer size or decrease your timeout")
slog.Error("unable to add new gauges, buffer full, you may want to increase your buffer size or decrease your timeout")
return
}

Expand Down Expand Up @@ -126,7 +124,7 @@ func (c *collector) flush(count int) {
// send it off
encoded, err := json.Marshal(reqPayload)
if err != nil {
logrus.WithField("comp", "librato").WithError(err).Error("error encoding librato metrics")
slog.Error("error encoding librato metrics", "error", err, "comp", "librato")
return
}

Expand All @@ -136,18 +134,18 @@ func (c *collector) flush(count int) {

resp, err := c.httpClient.Do(req)
if err != nil {
logrus.WithField("comp", "librato").WithError(err).Error("error sending librato metrics")
slog.Error("error sending librato metrics", "error", err, "comp", "librato")
return
}
// read our entire body and always close so we reuse connections
defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body)
io.Copy(io.Discard, resp.Body)

// non 200 or 201 are errors
if resp.StatusCode != 200 && resp.StatusCode != 201 {
logrus.WithField("comp", "librato").WithField("status_code", resp.StatusCode).Error("non 200 returned when posting librato metrics")
slog.Error("non 200 returned when posting librato metrics", "comp", "librato", "status_code", resp.StatusCode)
return
}

logrus.WithField("comp", "librato").WithField("count", len(reqPayload.Gauges)).Debug("flushed to librato")
slog.Debug("flushed to librato", "comp", "librato", "count", len(reqPayload.Gauges))
}

0 comments on commit ea99949

Please sign in to comment.