2022-11-09 16:07:28 +00:00
|
|
|
package lines
|
2022-11-09 12:19:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
m "git.k-space.ee/k-space/logmower-shipper/pkg/mongo"
|
2022-11-09 12:19:56 +00:00
|
|
|
)
|
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
func (unparsed RawC) parse(parsed chan<- singleLine) {
|
2022-11-09 12:19:56 +00:00
|
|
|
for {
|
|
|
|
raw, ok := <-unparsed
|
|
|
|
if !ok {
|
|
|
|
close(parsed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
line := singleLine{Raw: raw}
|
2022-11-09 12:19:56 +00:00
|
|
|
|
|
|
|
if err := line.parse(); err != nil {
|
2022-11-09 16:07:28 +00:00
|
|
|
promRecordPrefixParsingErr.WithLabelValues(raw.MetricsName).Add(1)
|
2022-11-09 12:19:56 +00:00
|
|
|
log.Printf("parsing kubernetes log line in %q: %e", raw.File.Path, err)
|
2022-11-09 18:45:44 +00:00
|
|
|
|
|
|
|
continue
|
2022-11-09 12:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parsed <- line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
singleLines <-chan singleLine
|
|
|
|
singleLine struct {
|
2022-11-09 16:07:28 +00:00
|
|
|
Raw
|
2022-11-09 12:19:56 +00:00
|
|
|
|
|
|
|
// populated by parse()
|
2022-11-09 16:07:28 +00:00
|
|
|
m.ParsedMetadata
|
2022-11-09 12:19:56 +00:00
|
|
|
partial bool // P or F
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (line *singleLine) parse() (err error) {
|
|
|
|
split := bytes.SplitN(line.B, []byte(" "), 4)
|
|
|
|
if len(split) != 4 {
|
|
|
|
return fmt.Errorf("expected at least 3 spaces in , got %d", len(split)-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
line.TimeKubernetes, err = time.Parse(time.RFC3339Nano, string(split[0]))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid time: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
line.StdErr = string(split[1]) == "stderr" // or stdout
|
|
|
|
|
|
|
|
switch string(split[2]) {
|
|
|
|
case "P":
|
|
|
|
line.partial = true
|
|
|
|
case "F":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("partial indicator must be 'P' or 'F', not %q", split[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
line.B = split[3]
|
|
|
|
return nil
|
|
|
|
}
|