Skip to content

Commit ac91395

Browse files
authored
Merge pull request #27696 from MayorFaj/fix-logs-timestamp-precision
fix(logs): improve timestamp precision in container logs
2 parents eb3da9d + e28d1e5 commit ac91395

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

pkg/api/handlers/compat/containers_logs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func LogsFromContainer(w http.ResponseWriter, r *http.Request) {
155155
}
156156

157157
if query.Timestamps {
158-
frame.WriteString(line.Time.Format(time.RFC3339))
158+
frame.WriteString(line.Time.Format(logs.LogTimeFormat))
159159
frame.WriteString(" ")
160160
}
161161

test/apiv2/20-containers.at

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,18 @@ like "$(<$WORKDIR/curl.headers.out)" ".*HTTP.* 200 OK.*" \
159159

160160
podman rm -f -t0 $CTRNAME
161161

162+
# verify API logs return timestamps with nanosecond precision
163+
CTRNAME=timestamp-test
164+
podman run --name $CTRNAME -d $IMAGE sh -c "echo test message"
165+
podman wait $CTRNAME
166+
t GET "containers/${CTRNAME}/logs?timestamps=true&stdout=true" 200
167+
# /logs returns application/octet-stream with binary headers. Strip null bytes.
168+
# Verify timestamp format includes nanoseconds (LogTimeFormat with 9 zero-padded digits)
169+
# Timezone can be 'Z' (UTC) or '+/-HH:MM'
170+
like "$(tr -d \\0 <$WORKDIR/curl.result.out)" ".*[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}T[0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}\\.[0-9]\\{9\\}\\(Z\\|[+-][0-9]\\{2\\}:[0-9]\\{2\\}\\).*test message.*" \
171+
"logs timestamps should include nanosecond precision"
172+
podman rm -f $CTRNAME
173+
162174
CTRNAME=test123
163175
podman run --name $CTRNAME -d $IMAGE top
164176
t GET libpod/containers/$CTRNAME/top?ps_args=--invalid 500 \

test/e2e/logs_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@ var _ = Describe("Podman logs", func() {
182182
}).WithTimeout(logTimeout).Should(Succeed())
183183
})
184184

185+
It("timestamps with nanosecond precision: "+log, func() {
186+
skipIfJournaldInContainer()
187+
188+
logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "echo", "test message"})
189+
logc.WaitWithDefaultTimeout()
190+
Expect(logc).To(ExitCleanly())
191+
cid := logc.OutputToString()
192+
193+
wait := podmanTest.Podman([]string{"wait", cid})
194+
wait.WaitWithDefaultTimeout()
195+
Expect(wait).To(ExitCleanly())
196+
197+
Eventually(func(g Gomega) {
198+
results := podmanTest.Podman([]string{"logs", "-t", cid})
199+
results.WaitWithDefaultTimeout()
200+
g.Expect(results).To(ExitCleanly())
201+
// LogTimeFormat: 2006-01-02T15:04:05.000000000Z07:00
202+
// Verify timestamp contains nanoseconds (exactly 9 digits, zero-padded)
203+
// Timezone can be 'Z' (UTC) or '+/-HH:MM'
204+
g.Expect(results.OutputToString()).To(MatchRegexp(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}(Z|[+-]\d{2}:\d{2})\s+test message`))
205+
}).WithTimeout(logTimeout).Should(Succeed())
206+
})
207+
185208
It("since time 2017-08-07: "+log, func() {
186209
skipIfJournaldInContainer()
187210

0 commit comments

Comments
 (0)