logmower-shipper/cmd/mongo_struct.go

60 lines
1.5 KiB
Go
Raw Normal View History

package logmower
import (
2022-11-06 14:52:22 +00:00
"context"
"time"
"go.mongodb.org/mongo-driver/bson"
2022-11-06 14:52:22 +00:00
"go.mongodb.org/mongo-driver/mongo"
)
2022-11-06 14:52:22 +00:00
func initializeIndexes(ctx context.Context, col *mongo.Collection) error {
ind := col.Indexes()
// (does not create duplicates)
_, err := ind.CreateOne(mongoTimeoutCtx(ctx), mongo.IndexModel{
Keys: bson.D{{Key: mongoKeyFileBasename, Value: 1}, {Key: mongoKeyOffset, Value: -1}},
})
return err
}
// 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,
}
}