Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to use slog instead of logrus #2

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CI
on: [push, pull_request]
env:
go-version: "1.21.x"
jobs:
test:
name: Test
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.go-version }}

- name: Run tests
run: go test -p=1 -coverprofile=coverage.text -covermode=atomic ./...

- name: Upload coverage
if: success()
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

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=
25 changes: 12 additions & 13 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 @@ -62,18 +60,18 @@ func NewCollector(username string, token string, source string, timeout time.Dur

// Start starts our librato sender, callers can use Stop to stop it
func (c *collector) Start() {
c.waitGroup.Add(1)
go func() {
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 All @@ -114,19 +112,20 @@ func (c *collector) flush(count int) {
}

// read up to our count of gauges
readCounts:
for i := 0; i < count; i++ {
select {
case g := <-c.buffer:
reqPayload.Gauges = append(reqPayload.Gauges, g)
default:
break
break readCounts
}
}

// 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 +135,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))
}