Skip to content

Commit

Permalink
feat: parse timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Jan 24, 2022
1 parent 1bfd63c commit 961f5e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
19 changes: 19 additions & 0 deletions api/logs/logs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package logs

import (
"bufio"
"fmt"
"strings"
"time"

"github.com/flanksource/kommons"
Expand Down Expand Up @@ -132,6 +134,23 @@ type Result struct {
Labels map[string]string `json:"labels,omitempty"`
}

func (r Result) Process() Result {
scanner := bufio.NewScanner(strings.NewReader(r.Message))
scanner.Split(bufio.ScanWords)
if scanner.Scan() {
timestamp := scanner.Text()
if _, err := time.Parse(time.RFC3339, timestamp); err == nil {
r.Time = timestamp
r.Message = strings.ReplaceAll(r.Message, timestamp, "")
fmt.Println(r.Message)
} else {
fmt.Printf("error parsing timestamp %s: %v\n", timestamp, err)
}
}
r.Message = strings.TrimSpace(r.Message)
return r
}

type SearchAPI interface {
Search(q *SearchParams) (r SearchResults, err error)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/kubernetes/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ func (s *KubernetesSearch) getLogResultsForPods(q *logs.SearchParams, pods *v1.P
}
for _, line := range containerLogs {
line.Labels = labels
results = append(results, line)
line = line.Process()
if line.Message != "" {
results = append(results, line)
}
}
}
}
Expand Down

0 comments on commit 961f5e8

Please sign in to comment.