From 639cb6addd5f1d92663b46a4308600ec086eda7d Mon Sep 17 00:00:00 2001 From: rasmus Date: Sun, 6 Nov 2022 16:33:57 +0200 Subject: [PATCH] set mongo structs strictly in one place + split file --- cmd/mongo_struct.go | 46 +++++++++++++++++++++++++++++++++++++++++++++ cmd/sender.go | 31 ------------------------------ cmd/submit.go | 4 ++-- 3 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 cmd/mongo_struct.go diff --git a/cmd/mongo_struct.go b/cmd/mongo_struct.go new file mode 100644 index 0000000..afe4fee --- /dev/null +++ b/cmd/mongo_struct.go @@ -0,0 +1,46 @@ +package logmower + +import ( + "time" + + "go.mongodb.org/mongo-driver/bson" +) + +// when editing, also edit everything in this file! +type mLog struct { + HostInfo HostInfo + File string + Offset int64 // byte offset where log entry ends at + Content string // TODO: + ShipTime time.Time + CollectTime time.Time + StdErr bool + Format string // F or P TODO: what does it mean? Is there a well-defined log format for cri-o? +} + +const ( + mongoKeyHostInfo = "host_info" + mongoKeyId = "id" + mongoKeyHostInfoId = mongoKeyHostInfo + "." + mongoKeyId + mongoKeyFileBasename = "file" + mongoKeyOffset = "offset" +) + +// not using marshal, since it is <0.1x performance +func (l *mLog) toBson() bson.M { + // DO NOT USE QUOTED STRINGS! Move them to const and use variable instead + return bson.M{ + mongoKeyHostInfo: bson.M{ + mongoKeyId: l.HostInfo.id, + "name": l.HostInfo.name, + "arch": l.HostInfo.arch, + }, + mongoKeyFileBasename: l.File, + mongoKeyOffset: l.Offset, + "content": l.Content, + "ship_time": l.ShipTime, + "container_time": l.CollectTime, + "stderr": l.StdErr, + "format": l.Format, + } +} diff --git a/cmd/sender.go b/cmd/sender.go index 85072f8..cf4d3c5 100644 --- a/cmd/sender.go +++ b/cmd/sender.go @@ -8,7 +8,6 @@ import ( "github.com/jtagcat/util" prom "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" - "go.mongodb.org/mongo-driver/bson" "go.uber.org/zap" ) @@ -124,33 +123,3 @@ func (s *submitter) sender(name string, sendQueue <-chan mLog) { } } } - -// when editing, also edit toBson(); all bson.D (and bson.M) uses -type mLog struct { - HostInfo HostInfo - File string - Offset int64 // byte offset where log entry ends at - Content string // TODO: - ShipTime time.Time - CollectTime time.Time - StdErr bool - Format string // F or P TODO: what does it mean? Is there a well-defined log format for cri-o? -} - -// not using marshal, since it is <0.1x performance -func (l *mLog) toBson() bson.M { - return bson.M{ - "host_info": bson.M{ - "id": l.HostInfo.id, - "name": l.HostInfo.name, - "arch": l.HostInfo.arch, - }, - "filename": l.File, - "offset": l.Offset, - "content": l.Content, - "ship_time": l.ShipTime, - "container_time": l.CollectTime, - "stderr": l.StdErr, - "format": l.Format, - } -} diff --git a/cmd/submit.go b/cmd/submit.go index 8f68218..89f0713 100644 --- a/cmd/submit.go +++ b/cmd/submit.go @@ -83,8 +83,8 @@ func (s *submitter) shipFileRoutine(ctx context.Context, name string, sendQueue // get files with offset offsetResult, err := mongoWithErr(s.db.FindOne(mongoTimeoutCtx(ctx), - bson.D{{Key: "hostinfo.id", Value: s.hostInfo.id}, {Key: "file", Value: baseName}}, - &mongoOpt.FindOneOptions{Sort: bson.D{{Key: "offset", Value: -1}}}, // sort descending (get largest) + bson.D{{Key: mongoKeyHostInfoId, Value: s.hostInfo.id}, {Key: mongoKeyFileBasename, Value: baseName}}, + &mongoOpt.FindOneOptions{Sort: bson.D{{Key: mongoKeyOffset, Value: -1}}}, // sort descending (get largest) )) if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {