Skip to content

Commit 644c400

Browse files
ofrobotsstephenplusplus
authored andcommitted
Log entries may be show up in the wrong order (#1815)
If a timestamp doesn't exist on the Log Entry, the Logging service inserts a timestamp based on the time it receives the message. Local and network queuing / latency may cause logs to be delivered in an order different from the originating source. Attaching a timestamp at Entry creation time makes avoids this problem.
1 parent 41f4a77 commit 644c400

4 files changed

Lines changed: 59 additions & 22 deletions

File tree

handwritten/logging/src/entry.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ var isCircular = require('is-circular');
8383
* });
8484
*/
8585
function Entry(metadata, data) {
86-
this.metadata = metadata;
86+
this.metadata = extend({
87+
timestamp: new Date()
88+
}, metadata);
89+
8790
this.data = data;
8891
}
8992

handwritten/logging/system-test/logging.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,28 @@ describe('Logging', function() {
347347
});
348348
});
349349

350+
it('should preserve order of entries', function(done) {
351+
var entry1 = log.entry('1');
352+
353+
setTimeout(function() {
354+
var entry3 = log.entry('3');
355+
var entry2 = log.entry({ timestamp: entry3.metadata.timestamp }, '2');
356+
357+
// Re-arrange to confirm the timestamp is sent and honored.
358+
log.write([entry2, entry3, entry1], options, function(err) {
359+
assert.ifError(err);
360+
361+
setTimeout(function() {
362+
log.getEntries({ pageSize: 3 }, function(err, entries) {
363+
assert.ifError(err);
364+
assert.deepEqual(entries.map(prop('data')), [ '3', '2', '1' ]);
365+
done();
366+
});
367+
}, WRITE_CONSISTENCY_DELAY_MS);
368+
});
369+
}, 1000);
370+
});
371+
350372
it('should write an entry with primitive values', function(done) {
351373
var logEntry = log.entry({
352374
when: new Date(),

handwritten/logging/test/entry.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,29 @@ describe('Entry', function() {
4444
});
4545

4646
describe('instantiation', function() {
47-
it('should localize metadata and data', function() {
48-
assert.strictEqual(entry.metadata, METADATA);
47+
it('should assign timestamp to metadata', function() {
48+
var now = new Date();
49+
50+
var expectedTimestampBoundaries = {
51+
start: new Date(now.getTime() - 1000),
52+
end: new Date(now.getTime() + 1000)
53+
};
54+
55+
assert(entry.metadata.timestamp >= expectedTimestampBoundaries.start);
56+
assert(entry.metadata.timestamp <= expectedTimestampBoundaries.end);
57+
});
58+
59+
it('should not assign timestamp if one is already set', function() {
60+
var timestamp = new Date('2012');
61+
62+
var entry = new Entry({
63+
timestamp: timestamp
64+
});
65+
66+
assert.strictEqual(entry.metadata.timestamp, timestamp);
67+
});
68+
69+
it('should localize data', function() {
4970
assert.strictEqual(entry.data, DATA);
5071
});
5172
});

handwritten/logging/test/log.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
var assert = require('assert');
2020
var extend = require('extend');
2121
var GrpcServiceObject = require('@google-cloud/common').GrpcServiceObject;
22+
var prop = require('propprop');
2223
var proxyquire = require('proxyquire');
2324
var util = require('@google-cloud/common').util;
2425

@@ -138,31 +139,21 @@ describe('Log', function() {
138139

139140
it('should assign severity to a single entry', function() {
140141
assert.deepEqual(
141-
Log.assignSeverityToEntries_(ENTRIES[0], SEVERITY),
142-
[
143-
extend(true, {}, ENTRIES[0], {
144-
metadata: {
145-
severity: SEVERITY
146-
}
147-
})
148-
]
142+
Log.assignSeverityToEntries_(ENTRIES[0], SEVERITY)
143+
.map(prop('metadata'))
144+
.map(prop('severity')),
145+
[ SEVERITY ]
149146
);
150147
});
151148

152149
it('should assign severity property to multiple entries', function() {
153150
assert.deepEqual(
154-
Log.assignSeverityToEntries_(ENTRIES, SEVERITY),
151+
Log.assignSeverityToEntries_(ENTRIES, SEVERITY)
152+
.map(prop('metadata'))
153+
.map(prop('severity')),
155154
[
156-
extend(true, {}, ENTRIES[0], {
157-
metadata: {
158-
severity: SEVERITY
159-
}
160-
}),
161-
extend(true, {}, ENTRIES[1], {
162-
metadata: {
163-
severity: SEVERITY
164-
}
165-
})
155+
SEVERITY,
156+
SEVERITY
166157
]
167158
);
168159
});

0 commit comments

Comments
 (0)