From 27cfa68af16721c978803c3b695bcc7181ccc721 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Fri, 22 Sep 2017 14:40:10 -0400 Subject: [PATCH] Move RFC3339NanoFixed to a more appropriate package. Signed-off-by: Daniel Nephin --- api/server/httputils/write_log_stream.go | 8 ++------ cmd/dockerd/daemon.go | 4 ++-- daemon/logger/logger.go | 3 --- integration-cli/docker_cli_logs_test.go | 4 ++-- pkg/jsonlog/time_marshalling.go | 15 ++++----------- pkg/jsonmessage/jsonmessage.go | 9 ++++++--- pkg/jsonmessage/jsonmessage_test.go | 13 ++++++------- 7 files changed, 22 insertions(+), 34 deletions(-) diff --git a/api/server/httputils/write_log_stream.go b/api/server/httputils/write_log_stream.go index e90e610da6..d7a49a1584 100644 --- a/api/server/httputils/write_log_stream.go +++ b/api/server/httputils/write_log_stream.go @@ -11,7 +11,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/backend" "github.com/docker/docker/pkg/ioutils" - "github.com/docker/docker/pkg/jsonlog" + "github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/stdcopy" ) @@ -49,11 +49,7 @@ func WriteLogStream(_ context.Context, w io.Writer, msgs <-chan *backend.LogMess logLine = append(logLine, msg.Line...) } if config.Timestamps { - // TODO(dperny) the format is defined in - // daemon/logger/logger.go as logger.TimeFormat. importing - // logger is verboten (not part of backend) so idk if just - // importing the same thing from jsonlog is good enough - logLine = append([]byte(msg.Timestamp.Format(jsonlog.RFC3339NanoFixed)+" "), logLine...) + logLine = append([]byte(msg.Timestamp.Format(jsonmessage.RFC3339NanoFixed)+" "), logLine...) } if msg.Source == "stdout" && config.ShowStdout { outStream.Write(logLine) diff --git a/cmd/dockerd/daemon.go b/cmd/dockerd/daemon.go index 2e8acf97da..c76886fd10 100644 --- a/cmd/dockerd/daemon.go +++ b/cmd/dockerd/daemon.go @@ -38,7 +38,7 @@ import ( "github.com/docker/docker/libcontainerd" dopts "github.com/docker/docker/opts" "github.com/docker/docker/pkg/authorization" - "github.com/docker/docker/pkg/jsonlog" + "github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/pidfile" "github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/pkg/signal" @@ -94,7 +94,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) { } logrus.SetFormatter(&logrus.TextFormatter{ - TimestampFormat: jsonlog.RFC3339NanoFixed, + TimestampFormat: jsonmessage.RFC3339NanoFixed, DisableColors: cli.Config.RawLogs, FullTimestamp: true, }) diff --git a/daemon/logger/logger.go b/daemon/logger/logger.go index a9d1e7640b..ee91b79c98 100644 --- a/daemon/logger/logger.go +++ b/daemon/logger/logger.go @@ -12,7 +12,6 @@ import ( "time" "github.com/docker/docker/api/types/backend" - "github.com/docker/docker/pkg/jsonlog" ) // ErrReadLogsNotSupported is returned when the underlying log driver does not support reading @@ -26,8 +25,6 @@ func (ErrReadLogsNotSupported) Error() string { func (ErrReadLogsNotSupported) NotImplemented() {} const ( - // TimeFormat is the time format used for timestamps sent to log readers. - TimeFormat = jsonlog.RFC3339NanoFixed logWatcherBufferSize = 4096 ) diff --git a/integration-cli/docker_cli_logs_test.go b/integration-cli/docker_cli_logs_test.go index d9523bffcc..f75da1849c 100644 --- a/integration-cli/docker_cli_logs_test.go +++ b/integration-cli/docker_cli_logs_test.go @@ -10,7 +10,7 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" - "github.com/docker/docker/pkg/jsonlog" + "github.com/docker/docker/pkg/jsonmessage" "github.com/go-check/check" "github.com/gotestyourself/gotestyourself/icmd" ) @@ -55,7 +55,7 @@ func (s *DockerSuite) TestLogsTimestamps(c *check.C) { for _, l := range lines { if l != "" { - _, err := time.Parse(jsonlog.RFC3339NanoFixed+" ", ts.FindString(l)) + _, err := time.Parse(jsonmessage.RFC3339NanoFixed+" ", ts.FindString(l)) c.Assert(err, checker.IsNil, check.Commentf("Failed to parse timestamp from %v", l)) // ensure we have padded 0's c.Assert(l[29], checker.Equals, uint8('Z')) diff --git a/pkg/jsonlog/time_marshalling.go b/pkg/jsonlog/time_marshalling.go index 2117338149..8c3a8d8282 100644 --- a/pkg/jsonlog/time_marshalling.go +++ b/pkg/jsonlog/time_marshalling.go @@ -1,19 +1,12 @@ -// Package jsonlog provides helper functions to parse and print time (time.Time) as JSON. package jsonlog import ( - "errors" "time" + + "github.com/pkg/errors" ) -const ( - // RFC3339NanoFixed is our own version of RFC339Nano because we want one - // that pads the nano seconds part with zeros to ensure - // the timestamps are aligned in the logs. - RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00" - // JSONFormat is the format used by FastMarshalJSON - JSONFormat = `"` + time.RFC3339Nano + `"` -) +const jsonFormat = `"` + time.RFC3339Nano + `"` // FastTimeMarshalJSON avoids one of the extra allocations that // time.MarshalJSON is making. @@ -23,5 +16,5 @@ func FastTimeMarshalJSON(t time.Time) (string, error) { // See golang.org/issue/4556#c15 for more discussion. return "", errors.New("time.MarshalJSON: year outside of range [0,9999]") } - return t.Format(JSONFormat), nil + return t.Format(jsonFormat), nil } diff --git a/pkg/jsonmessage/jsonmessage.go b/pkg/jsonmessage/jsonmessage.go index 09fc4cc745..6cfa464830 100644 --- a/pkg/jsonmessage/jsonmessage.go +++ b/pkg/jsonmessage/jsonmessage.go @@ -9,11 +9,14 @@ import ( "time" gotty "github.com/Nvveen/Gotty" - "github.com/docker/docker/pkg/jsonlog" "github.com/docker/docker/pkg/term" units "github.com/docker/go-units" ) +// RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to +// ensure the formatted time isalways the same number of characters. +const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00" + // JSONError wraps a concrete Code and Message, `Code` is // is an integer error code, `Message` is the error message. type JSONError struct { @@ -199,9 +202,9 @@ func (jm *JSONMessage) Display(out io.Writer, termInfo termInfo) error { return nil } if jm.TimeNano != 0 { - fmt.Fprintf(out, "%s ", time.Unix(0, jm.TimeNano).Format(jsonlog.RFC3339NanoFixed)) + fmt.Fprintf(out, "%s ", time.Unix(0, jm.TimeNano).Format(RFC3339NanoFixed)) } else if jm.Time != 0 { - fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(jsonlog.RFC3339NanoFixed)) + fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(RFC3339NanoFixed)) } if jm.ID != "" { fmt.Fprintf(out, "%s: ", jm.ID) diff --git a/pkg/jsonmessage/jsonmessage_test.go b/pkg/jsonmessage/jsonmessage_test.go index 5206789ac2..9740bcd98d 100644 --- a/pkg/jsonmessage/jsonmessage_test.go +++ b/pkg/jsonmessage/jsonmessage_test.go @@ -8,7 +8,6 @@ import ( "testing" "time" - "github.com/docker/docker/pkg/jsonlog" "github.com/docker/docker/pkg/term" "github.com/stretchr/testify/assert" ) @@ -115,8 +114,8 @@ func TestJSONMessageDisplay(t *testing.T) { From: "From", Status: "status", }: { - fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(jsonlog.RFC3339NanoFixed)), - fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(jsonlog.RFC3339NanoFixed)), + fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(RFC3339NanoFixed)), + fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(RFC3339NanoFixed)), }, // General, with nano precision time { @@ -125,8 +124,8 @@ func TestJSONMessageDisplay(t *testing.T) { From: "From", Status: "status", }: { - fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)), - fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)), + fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(RFC3339NanoFixed)), + fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(RFC3339NanoFixed)), }, // General, with both times Nano is preferred { @@ -136,8 +135,8 @@ func TestJSONMessageDisplay(t *testing.T) { From: "From", Status: "status", }: { - fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)), - fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)), + fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(RFC3339NanoFixed)), + fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(RFC3339NanoFixed)), }, // Stream over status {