set mongo structs strictly in one place

+ split file
This commit is contained in:
rasmus 2022-11-06 16:33:57 +02:00
parent 2fa1c6cd7b
commit 639cb6addd
3 changed files with 48 additions and 33 deletions

46
cmd/mongo_struct.go Normal file
View File

@ -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,
}
}

View File

@ -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,
}
}

View File

@ -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) {