Browse code

events/jsonmessage: add and prefer TimeNano for events

This way provide both Time and TimeNano in the event. For the display of
the JSONMessage, use either, but prefer TimeNano Proving only TimeNano
would break Subscribers that are using the `Time` field, so both are set
for backwards compatibility.

The events logging uses nano formatting, but only provides a Unix()
time, therefor ordering may get lost in the output. Example:
```
2015-09-15T14:18:51.000000000-04:00 ee46febd64ac629f7de9cd8bf58582e6f263d97ff46896adc5b508db804682da: (from busybox) resize
2015-09-15T14:18:51.000000000-04:00 a78c9149b1c0474502a117efaa814541926c2ae6ec3c76607e1c931b84c3a44b: (from busybox) resize
```

By having a field just for Nano time, when set, the marshalling back to
`time.Unix(sec int64, nsec int64)` has zeros exactly where it needs to.
This does not break any existing use of jsonmessage.JSONMessage, but now
allows for use of `UnixNano()` and get event formatting that has
distinguishable order. Example:
```
2015-09-15T15:37:23.810295632-04:00 6adcf8ed9f5f5ec059a915466cd1cde86a18b4a085fc3af405e9cc9fecbbbbaf: (from busybox) resize
2015-09-15T15:37:23.810412202-04:00 6b7c5bfdc3f902096f5a91e628f21bd4b56e32590c5b4b97044aafc005ddcb0d: (from busybox) resize
```

Including tests for TimeNano and updated event API reference doc.

Signed-off-by: Vincent Batts <vbatts@redhat.com>

Vincent Batts authored on 2015/09/16 04:33:11
Showing 5 changed files
... ...
@@ -45,7 +45,8 @@ func (e *Events) Evict(l chan interface{}) {
45 45
 // Log broadcasts event to listeners. Each listener has 100 millisecond for
46 46
 // receiving event or it will be skipped.
47 47
 func (e *Events) Log(action, id, from string) {
48
-	jm := &jsonmessage.JSONMessage{Status: action, ID: id, From: from, Time: time.Now().UTC().Unix()}
48
+	now := time.Now().UTC()
49
+	jm := &jsonmessage.JSONMessage{Status: action, ID: id, From: from, Time: now.Unix(), TimeNano: now.UnixNano()}
49 50
 	e.mu.Lock()
50 51
 	if len(e.events) == cap(e.events) {
51 52
 		// discard oldest event
... ...
@@ -87,6 +87,7 @@ This section lists each version from latest to oldest.  Each listing includes a
87 87
 * The `hostConfig` option now accepts the field `DnsOptions`, which specifies a
88 88
 list of DNS options to be used in the container.
89 89
 * `POST /build` now optionally takes a serialized map of build-time variables.
90
+* `GET /events` now includes a `timenano` field, in addition to the existing `time` field.
90 91
 
91 92
 ### v1.20 API changes
92 93
 
... ...
@@ -2014,10 +2014,10 @@ and Docker images report:
2014 2014
     HTTP/1.1 200 OK
2015 2015
     Content-Type: application/json
2016 2016
 
2017
-    {"status": "create", "id": "dfdf82bd3881","from": "ubuntu:latest", "time":1374067924}
2018
-    {"status": "start", "id": "dfdf82bd3881","from": "ubuntu:latest", "time":1374067924}
2019
-    {"status": "stop", "id": "dfdf82bd3881","from": "ubuntu:latest", "time":1374067966}
2020
-    {"status": "destroy", "id": "dfdf82bd3881","from": "ubuntu:latest", "time":1374067970}
2017
+    {"status":"pull","id":"busybox:latest","time":1442421700,"timeNano":1442421700598988358}
2018
+    {"status":"create","id":"5745704abe9caa5","from":"busybox","time":1442421716,"timeNano":1442421716853979870}
2019
+    {"status":"attach","id":"5745704abe9caa5","from":"busybox","time":1442421716,"timeNano":1442421716894759198}
2020
+    {"status":"start","id":"5745704abe9caa5","from":"busybox","time":1442421716,"timeNano":1442421716983607193}
2021 2021
 
2022 2022
 Query Parameters:
2023 2023
 
... ...
@@ -99,6 +99,7 @@ type JSONMessage struct {
99 99
 	ID              string        `json:"id,omitempty"`
100 100
 	From            string        `json:"from,omitempty"`
101 101
 	Time            int64         `json:"time,omitempty"`
102
+	TimeNano        int64         `json:"timeNano,omitempty"`
102 103
 	Error           *JSONError    `json:"errorDetail,omitempty"`
103 104
 	ErrorMessage    string        `json:"error,omitempty"` //deprecated
104 105
 }
... ...
@@ -121,7 +122,9 @@ func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error {
121 121
 	} else if jm.Progress != nil && jm.Progress.String() != "" { //disable progressbar in non-terminal
122 122
 		return nil
123 123
 	}
124
-	if jm.Time != 0 {
124
+	if jm.TimeNano != 0 {
125
+		fmt.Fprintf(out, "%s ", time.Unix(0, jm.TimeNano).Format(timeutils.RFC3339NanoFixed))
126
+	} else if jm.Time != 0 {
125 127
 		fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(timeutils.RFC3339NanoFixed))
126 128
 	}
127 129
 	if jm.ID != "" {
... ...
@@ -53,7 +53,7 @@ func TestProgress(t *testing.T) {
53 53
 }
54 54
 
55 55
 func TestJSONMessageDisplay(t *testing.T) {
56
-	now := time.Now().Unix()
56
+	now := time.Now()
57 57
 	messages := map[JSONMessage][]string{
58 58
 		// Empty
59 59
 		JSONMessage{}: {"\n", "\n"},
... ...
@@ -66,13 +66,34 @@ func TestJSONMessageDisplay(t *testing.T) {
66 66
 		},
67 67
 		// General
68 68
 		JSONMessage{
69
-			Time:   now,
69
+			Time:   now.Unix(),
70 70
 			ID:     "ID",
71 71
 			From:   "From",
72 72
 			Status: "status",
73 73
 		}: {
74
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now, 0).Format(timeutils.RFC3339NanoFixed)),
75
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now, 0).Format(timeutils.RFC3339NanoFixed)),
74
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(timeutils.RFC3339NanoFixed)),
75
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(timeutils.RFC3339NanoFixed)),
76
+		},
77
+		// General, with nano precision time
78
+		JSONMessage{
79
+			TimeNano: now.UnixNano(),
80
+			ID:       "ID",
81
+			From:     "From",
82
+			Status:   "status",
83
+		}: {
84
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(timeutils.RFC3339NanoFixed)),
85
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(timeutils.RFC3339NanoFixed)),
86
+		},
87
+		// General, with both times Nano is preferred
88
+		JSONMessage{
89
+			Time:     now.Unix(),
90
+			TimeNano: now.UnixNano(),
91
+			ID:       "ID",
92
+			From:     "From",
93
+			Status:   "status",
94
+		}: {
95
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(timeutils.RFC3339NanoFixed)),
96
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(timeutils.RFC3339NanoFixed)),
76 97
 		},
77 98
 		// Stream over status
78 99
 		JSONMessage{