2022-11-06 20:02:29 +00:00
|
|
|
package logmower
|
|
|
|
|
|
|
|
import (
|
2022-11-09 12:19:56 +00:00
|
|
|
"log"
|
2022-11-06 20:02:29 +00:00
|
|
|
"sync"
|
|
|
|
|
2022-11-09 14:00:44 +00:00
|
|
|
ms "git.k-space.ee/k-space/logmower-shipper/pkg/mongo_struct"
|
2022-11-06 20:02:29 +00:00
|
|
|
prom "github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
)
|
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
var promRecordDroppedTooLarge = promauto.NewCounterVec(prom.CounterOpts{
|
|
|
|
Namespace: PrometheusPrefix,
|
|
|
|
// Subsystem: "record",
|
|
|
|
Name: "dropped_lines", // "dropped",
|
|
|
|
Help: "Records dropped due to being too large",
|
|
|
|
}, []string{"filename"})
|
2022-11-06 20:02:29 +00:00
|
|
|
|
|
|
|
type (
|
2022-11-09 12:19:56 +00:00
|
|
|
RawLines <-chan RawLine
|
|
|
|
RawLine struct {
|
|
|
|
*file
|
|
|
|
Offset int64
|
|
|
|
B []byte
|
2022-11-06 20:02:29 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// assumes all lines are from same file
|
2022-11-09 12:19:56 +00:00
|
|
|
func (unparsed RawLines) Process(bufferLimitBytes int, parsed chan<- ms.Record) {
|
2022-11-06 20:02:29 +00:00
|
|
|
lines := make(chan singleLine)
|
2022-11-09 12:19:56 +00:00
|
|
|
go unparsed.parse(lines)
|
2022-11-06 20:02:29 +00:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
stdOut, stdErr := make(chan singleLine), make(chan singleLine)
|
|
|
|
go func() {
|
2022-11-09 12:19:56 +00:00
|
|
|
singleLines(stdOut).process(bufferLimitBytes, parsed)
|
2022-11-06 20:02:29 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
2022-11-09 12:19:56 +00:00
|
|
|
singleLines(stdErr).process(bufferLimitBytes, parsed)
|
2022-11-06 20:02:29 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// split stdout and stderr
|
|
|
|
for {
|
|
|
|
line, ok := <-lines
|
|
|
|
if !ok {
|
|
|
|
close(stdOut)
|
|
|
|
close(stdErr)
|
|
|
|
wg.Wait()
|
|
|
|
close(parsed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if line.StdErr {
|
|
|
|
stdErr <- line
|
|
|
|
} else {
|
|
|
|
stdOut <- line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
func (lines singleLines) process(bufferLimitBytes int, parsed chan<- ms.Record) {
|
|
|
|
var firstMetadata *ms.ParsedMetadata
|
2022-11-06 20:02:29 +00:00
|
|
|
var buffer []byte
|
|
|
|
|
|
|
|
for {
|
|
|
|
line, ok := <-lines
|
|
|
|
if !ok {
|
2022-11-09 12:19:56 +00:00
|
|
|
// partial line should always be finished with full line
|
|
|
|
// discard any partial lines without end (full line)
|
2022-11-06 20:02:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(buffer) == 0 {
|
2022-11-09 12:19:56 +00:00
|
|
|
firstMetadata = &line.ParsedMetadata
|
2022-11-06 20:02:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
buffer = append(buffer, line.B...)
|
2022-11-06 20:02:29 +00:00
|
|
|
|
|
|
|
if len(buffer) > bufferLimitBytes {
|
2022-11-09 12:19:56 +00:00
|
|
|
promRecordDroppedTooLarge.WithLabelValues(line.metricsName).Add(1)
|
|
|
|
log.Printf("dropped record: size in bytes exceeds limit of %d", bufferLimitBytes)
|
|
|
|
|
2022-11-06 20:02:29 +00:00
|
|
|
buffer = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
if !line.partial {
|
|
|
|
parsed <- ms.Record{
|
|
|
|
File: line.file.File,
|
|
|
|
Offset: line.Offset,
|
|
|
|
|
|
|
|
String: string(buffer),
|
|
|
|
ParsedMetadata: *firstMetadata,
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = nil
|
2022-11-06 20:02:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|