Skip to content

Commit

Permalink
Merge pull request #85 from Infra-Red/go-kit-logging
Browse files Browse the repository at this point in the history
Switch logging to go-kit
  • Loading branch information
juliusv authored Oct 10, 2019
2 parents ce323be + 5bb1095 commit 7ec30b1
Show file tree
Hide file tree
Showing 349 changed files with 3,462 additions and 219,961 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ go 1.12

require (
collectd.org v0.3.1-0.20181025072142-f80706d1e115
github.com/go-kit/kit v0.8.0
github.com/prometheus/client_golang v1.0.0
github.com/prometheus/common v0.4.1
github.com/sirupsen/logrus v1.4.2 // indirect
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
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/go-kit/kit v0.8.0 h1:Wz+5lgoB0kkuqLEc6NVmwRknTKP6dTGbSqvhZtBI/j0=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0 h1:8HUsc87TaSWLKwrnumgC8/YconD2fJQsRJAsWaPg2ic=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
Expand All @@ -23,12 +26,14 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj2vyii2bbUNDw3kt9VxK2EY=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -46,8 +51,6 @@ github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNG
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
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 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
Expand Down
58 changes: 39 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ import (

"collectd.org/api"
"collectd.org/network"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
)
Expand Down Expand Up @@ -129,13 +132,15 @@ type collectdCollector struct {
ch chan api.ValueList
valueLists map[string]api.ValueList
mu *sync.Mutex
logger log.Logger
}

func newCollectdCollector() *collectdCollector {
func newCollectdCollector(logger log.Logger) *collectdCollector {
c := &collectdCollector{
ch: make(chan api.ValueList),
valueLists: make(map[string]api.ValueList),
mu: &sync.Mutex{},
logger: logger,
}
go c.processSamples()
return c
Expand Down Expand Up @@ -205,7 +210,7 @@ func (c collectdCollector) Collect(ch chan<- prometheus.Metric) {
for i := range vl.Values {
m, err := newMetric(vl, i)
if err != nil {
log.Errorf("newMetric: %v", err)
level.Error(c.logger).Log("msg", "Error converting collectd data type to a Prometheus metric", "err", err)
continue
}

Expand All @@ -228,7 +233,7 @@ func (c collectdCollector) Write(_ context.Context, vl *api.ValueList) error {
return nil
}

func startCollectdServer(ctx context.Context, w api.Writer) {
func startCollectdServer(ctx context.Context, w api.Writer, logger log.Logger) {
if *collectdAddress == "" {
return
}
Expand All @@ -245,13 +250,15 @@ func startCollectdServer(ctx context.Context, w api.Writer) {
if *collectdTypesDB != "" {
file, err := os.Open(*collectdTypesDB)
if err != nil {
log.Fatalf("Can't open types.db file %s", *collectdTypesDB)
level.Error(logger).Log("msg", "Can't open types.db file", "types", *collectdTypesDB, "err", err)
os.Exit(1)
}
defer file.Close()

typesDB, err := api.NewTypesDB(file)
if err != nil {
log.Fatalf("Error in parsing types.db file %s", *collectdTypesDB)
level.Error(logger).Log("msg", "Error in parsing types.db file", "types", *collectdTypesDB, "err", err)
os.Exit(1)
}
srv.TypesDB = typesDB
}
Expand All @@ -264,12 +271,14 @@ func startCollectdServer(ctx context.Context, w api.Writer) {
case "encrypt":
srv.SecurityLevel = network.Encrypt
default:
log.Fatalf("Unknown security level %q. Must be one of \"None\", \"Sign\" and \"Encrypt\".", *collectdSecurity)
level.Error(logger).Log("msg", "Unknown security level provided. Must be one of \"None\", \"Sign\" and \"Encrypt\"", "level", *collectdSecurity)
os.Exit(1)
}

laddr, err := net.ResolveUDPAddr("udp", *collectdAddress)
if err != nil {
log.Fatalf("Failed to resolve binary protocol listening UDP address %q: %v", *collectdAddress, err)
level.Error(logger).Log("msg", "Failed to resolve binary protocol listening UDP address", "address", *collectdAddress, "err", err)
os.Exit(1)
}

if laddr.IP != nil && laddr.IP.IsMulticast() {
Expand All @@ -278,16 +287,21 @@ func startCollectdServer(ctx context.Context, w api.Writer) {
srv.Conn, err = net.ListenUDP("udp", laddr)
}
if err != nil {
log.Fatalf("Failed to create a socket for a binary protocol server: %v", err)
level.Error(logger).Log("msg", "Failed to create a socket for a binary protocol server", "err", err)
os.Exit(1)
}
if *collectdBuffer > 0 {
if err = srv.Conn.SetReadBuffer(*collectdBuffer); err != nil {
log.Fatalf("Failed to adjust a read buffer of the socket: %v", err)
level.Error(logger).Log("msg", "Failed to adjust a read buffer of the socket", "err", err)
os.Exit(1)
}
}

go func() {
log.Fatal(srv.ListenAndWrite(ctx))
if err := srv.ListenAndWrite(ctx); err != nil {
level.Error(logger).Log("msg", "Error starting collectd server", "err", err)
os.Exit(1)
}
}()
}

Expand All @@ -296,18 +310,21 @@ func init() {
}

func main() {
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("collectd_exporter"))
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)

log.Infoln("Starting collectd_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
kingpin.Version(version.Print("collectd_exporter"))

c := newCollectdCollector()
level.Info(logger).Log("msg", "Starting collectd_exporter", "version", version.Info())
level.Info(logger).Log("msg", "Build context", "context", version.BuildContext())

c := newCollectdCollector(logger)
prometheus.MustRegister(c)

startCollectdServer(context.Background(), c)
startCollectdServer(context.Background(), c, logger)

if *collectdPostPath != "" {
http.HandleFunc(*collectdPostPath, c.collectdPost)
Expand All @@ -324,6 +341,9 @@ func main() {
</html>`))
})

log.Infoln("Listening on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
level.Info(logger).Log("msg", "Listening on address", "address", *listenAddress)
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
os.Exit(1)
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

147 changes: 147 additions & 0 deletions vendor/github.com/go-kit/kit/log/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7ec30b1

Please sign in to comment.