Skip to content

Commit 43ebe3e

Browse files
committed
improve extracting timestamp from a log
Signed-off-by: Mangaal <[email protected]>
1 parent 4cbf115 commit 43ebe3e

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

agent/log.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ func (a *Agent) startLogStreamIfNew(logReq *event.ContainerLogRequest, logCtx *l
6363
a.inflightMu.Unlock()
6464
}()
6565

66+
logCtx.WithFields(logrus.Fields{
67+
"uuid": logReq.UUID,
68+
"namespace": logReq.Namespace,
69+
"pod": logReq.PodName,
70+
"container": logReq.Container,
71+
"follow": logReq.Follow,
72+
}).Info("Processing log request")
73+
6674
if logReq.Follow {
6775
// Handle live logs with early ACK
6876
return a.handleLiveStreaming(ctx, logReq, logCtx)
@@ -213,8 +221,8 @@ func (a *Agent) streamLogsToCompletion(
213221
logCtx *logrus.Entry,
214222
) error {
215223
const (
216-
chunkMax = 64 * 1024 // 64KiB chunks
217-
flushEvery = 50 * time.Millisecond // keep UI latency small
224+
chunkMax = 64 * 1024
225+
flushEvery = 50 * time.Millisecond
218226
)
219227

220228
br := bufio.NewReader(rc)
@@ -272,7 +280,6 @@ func (a *Agent) streamLogsToCompletion(
272280
// Add each processed line to the buffer
273281
for _, processedLine := range processedLines {
274282
wire := processedLine
275-
// append to chunk buffer, splitting if a single line is larger than remaining space
276283
for len(wire) > 0 {
277284
space := chunkMax - len(buf)
278285
if space == 0 {
@@ -552,35 +559,28 @@ func (a *Agent) processLogLine(line string) ([]string, error) {
552559

553560
// extractTimestamp extracts timestamp from a log line for resume capability
554561
func extractTimestamp(line string) *time.Time {
555-
// Look for RFC3339 timestamp at the beginning of the line
556-
// Format: 2023-12-07T10:30:45.123456789Z
557-
if len(line) < 20 {
562+
if len(line) < 20 { // "2006-01-02T15:04:05Z" is 20 chars
558563
return nil
559564
}
560-
561-
// Find the first space or tab after potential timestamp
562-
spaceIdx := strings.IndexAny(line, " \t")
563-
if spaceIdx == -1 || spaceIdx > 35 { // RFC3339 with nanoseconds is max ~35 chars
565+
// Grab the first token (up to whitespace). k8s puts a space after the timestamp.
566+
space := strings.IndexAny(line, " \t")
567+
if space == -1 {
568+
// Fall back: try whole line (cheap fast-fail)
569+
space = len(line)
570+
}
571+
// Guard against absurdly long "tokens"
572+
const maxTSLen = 40 // a tad higher than needed; RFC3339Nano+offset is 35
573+
if space > maxTSLen {
564574
return nil
565575
}
576+
token := line[:space]
566577

567-
timestampStr := strings.TrimSpace(line[:spaceIdx])
568-
569-
// Try parsing common timestamp formats
570-
formats := []string{
571-
time.RFC3339Nano,
572-
time.RFC3339,
573-
"2006-01-02T15:04:05.000000000Z",
574-
"2006-01-02T15:04:05.000000Z",
575-
"2006-01-02T15:04:05.000Z",
576-
"2006-01-02T15:04:05Z",
578+
// Try the common RFC3339 flavors (covers with/without fractional seconds and offsets)
579+
if ts, err := time.Parse(time.RFC3339Nano, token); err == nil {
580+
return &ts
577581
}
578-
579-
for _, format := range formats {
580-
if ts, err := time.Parse(format, timestampStr); err == nil {
581-
return &ts
582-
}
582+
if ts, err := time.Parse(time.RFC3339, token); err == nil {
583+
return &ts
583584
}
584-
585585
return nil
586586
}

0 commit comments

Comments
 (0)