Skip to content

Commit 93091df

Browse files
authored
Merge pull request #6260 from sbueringer/pr-impr-log-push
🌱 log-push: preserve original timestamp, parse controller too
2 parents 5652c01 + 5e8dbd9 commit 93091df

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

docs/book/src/developer/testing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ analyzing them via Grafana.
289289

290290
* Make sure you query the correct time range via Grafana or `logcli`.
291291
* The logs are currently uploaded by using now as the timestamp, because otherwise it would
292-
take a few minutes until the logs show up in Loki.
292+
take a few minutes until the logs show up in Loki. The original timestamp is preserved as `original_ts`.
293293

294294
</aside>
295295

hack/tools/log-push/main.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import (
4949
var (
5050
logPath = flag.String("log-path", "", "Can be either a GCS path, a ProwJob URL or a local directory")
5151
logFileRegex = flag.String("log-file-regex", "manager\\.log", "Regex used to find log files")
52-
logJSONAdditionalLabels = flag.String("log-json-additional-labels", "cluster,machine", "Comma-separated list of additional labels to parse from JSON logs")
52+
logJSONAdditionalLabels = flag.String("log-json-additional-labels", "controller,cluster,machine", "Comma-separated list of additional labels to parse from JSON logs")
5353
lokiURL = flag.String("loki-url", "http://localhost:3100/loki/api/v1/push", "Loki URL to push the logs to")
5454
)
5555

@@ -320,15 +320,23 @@ func prepareLogsForLoki(ld LogData, logJSONAdditionalLabels []string) ([]LokiStr
320320
logLineMetadata[k] = v
321321
}
322322

323-
lokiStream := LokiStream{
324-
Stream: logLineMetadata,
325-
Values: [][]string{{tsNano, logLine}},
326-
}
327-
328323
parsedLogLine, err := fastjson.Parse(logLine)
329324
// We intentionally silently ignore the error, otherwise we
330325
// would get too many errors with logs in text format.
331326
if err == nil {
327+
// Store the ts in original_ts so that it's shown as a separate k/v in Loki.
328+
if parsedLogLine.Exists("ts") {
329+
originalTimestampMillis := parsedLogLine.Get("ts")
330+
parsedLogLine.Set("original_tsMs", originalTimestampMillis)
331+
332+
t := time.UnixMilli(int64(originalTimestampMillis.GetFloat64()))
333+
originalTimestamp := strconv.Quote(t.Format(time.RFC3339Nano))
334+
parsedLogLine.Set("original_ts", fastjson.MustParse(originalTimestamp))
335+
336+
// Overwrite the original log line with the one with the additional original timestamp.
337+
logLine = parsedLogLine.String()
338+
}
339+
332340
// Add cluster and machine labels to logLineMetadata
333341
// if they exist in the current log line.
334342
for _, label := range logJSONAdditionalLabels {
@@ -344,11 +352,14 @@ func prepareLogsForLoki(ld LogData, logJSONAdditionalLabels []string) ([]LokiStr
344352
if err != nil {
345353
return nil, errors.Wrapf(err, "failed to unquote label %q: %q", label, labelValue)
346354
}
347-
lokiStream.Stream[label] = labelValue
355+
logLineMetadata[label] = labelValue
348356
}
349357
}
350358

351-
allStreams = append(allStreams, lokiStream)
359+
allStreams = append(allStreams, LokiStream{
360+
Stream: logLineMetadata,
361+
Values: [][]string{{tsNano, logLine}},
362+
})
352363
}
353364

354365
// We have to batch the streams, because we can only push

0 commit comments

Comments
 (0)