nflog: add logging

This commit is contained in:
Fabian Reinartz 2016-08-16 08:44:16 +02:00
parent b40703a584
commit 4a5df40539

View File

@ -10,6 +10,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/go-kit/kit/log"
"github.com/golang/protobuf/ptypes" "github.com/golang/protobuf/ptypes"
"github.com/matttproud/golang_protobuf_extensions/pbutil" "github.com/matttproud/golang_protobuf_extensions/pbutil"
pb "github.com/prometheus/alertmanager/nflog/nflogpb" pb "github.com/prometheus/alertmanager/nflog/nflogpb"
@ -74,6 +75,7 @@ func QGroupKey(gk []byte) QueryParam {
} }
type nlog struct { type nlog struct {
logger log.Logger
now func() time.Time now func() time.Time
retention time.Duration retention time.Duration
@ -123,6 +125,13 @@ func WithNow(f func() time.Time) Option {
} }
} }
func WithLogger(logger log.Logger) Option {
return func(l *nlog) error {
l.logger = logger
return nil
}
}
// WithMaintenance configures the Log to run garbage collection // WithMaintenance configures the Log to run garbage collection
// and snapshotting to the provided file at the given interval. // and snapshotting to the provided file at the given interval.
// On startup the a snapshot is also loaded from the given file. // On startup the a snapshot is also loaded from the given file.
@ -150,8 +159,9 @@ func utcNow() time.Time {
// The snapshot is loaded into the Log if it is set. // The snapshot is loaded into the Log if it is set.
func New(opts ...Option) (Log, error) { func New(opts ...Option) (Log, error) {
l := &nlog{ l := &nlog{
now: utcNow, logger: log.NewNopLogger(),
st: map[string]*pb.MeshEntry{}, now: utcNow,
st: map[string]*pb.MeshEntry{},
} }
for _, o := range opts { for _, o := range opts {
if err := o(l); err != nil { if err := o(l); err != nil {
@ -186,24 +196,22 @@ func (l *nlog) run() {
defer l.done() defer l.done()
} }
f := func() { f := func() error {
if _, err := l.GC(); err != nil { if _, err := l.GC(); err != nil {
// TODO(fabxc): log error instead return err
panic(err)
} }
if l.snapf == "" { if l.snapf == "" {
return return nil
} }
f, err := openReplace(l.snapf) f, err := openReplace(l.snapf)
if err != nil { if err != nil {
panic(err) return err
} }
// TODO(fabxc): potentially expose snapshot size in log message.
if _, err := l.Snapshot(f); err != nil { if _, err := l.Snapshot(f); err != nil {
panic(err) return err
}
if err := f.Close(); err != nil {
panic(err)
} }
return f.Close()
} }
for { for {
@ -211,10 +219,18 @@ func (l *nlog) run() {
case <-l.stopc: case <-l.stopc:
return return
case <-t.C: case <-t.C:
f() if err := f(); err != nil {
l.logger.Log("msg", "running maintenance failed", "err", err)
}
} }
} }
f() // No need to run final maintenance if we don't want to snapshot.
if l.snapf == "" {
return
}
if err := f(); err != nil {
l.logger.Log("msg", "creating shutdown snapshot failed", "err", err)
}
} }
// LogActive implements the Log interface. // LogActive implements the Log interface.
@ -438,7 +454,7 @@ func (gd gossipData) Encode() [][]byte {
m, err := pbutil.WriteDelimited(&buf, e) m, err := pbutil.WriteDelimited(&buf, e)
n += m n += m
if err != nil { if err != nil {
// TODO(fabxc): log error and skip entry. // TODO(fabxc): log error and skip entry. Or can this really not happen with a bytes.Buffer?
panic(err) panic(err)
} }
if n > maxSize { if n > maxSize {
@ -473,7 +489,7 @@ func (gd gossipData) Merge(other mesh.GossipData) mesh.GossipData {
} }
pts, err := ptypes.Timestamp(prev.Entry.Timestamp) pts, err := ptypes.Timestamp(prev.Entry.Timestamp)
if err != nil { if err != nil {
// TODO(fabxc): log error and skip entry. // TODO(fabxc): log error and skip entry. What can actually error here?
panic(err) panic(err)
} }
ets, err := ptypes.Timestamp(e.Entry.Timestamp) ets, err := ptypes.Timestamp(e.Entry.Timestamp)
@ -501,7 +517,7 @@ func (gd gossipData) mergeDelta(od gossipData) gossipData {
} }
pts, err := ptypes.Timestamp(prev.Entry.Timestamp) pts, err := ptypes.Timestamp(prev.Entry.Timestamp)
if err != nil { if err != nil {
// TODO(fabxc): log error and skip entry. // TODO(fabxc): log error and skip entry. What can actually error here?
panic(err) panic(err)
} }
ets, err := ptypes.Timestamp(e.Entry.Timestamp) ets, err := ptypes.Timestamp(e.Entry.Timestamp)