Skip to content

Commit

Permalink
Add insertId on log entries (#2021)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofrobots authored and stephenplusplus committed Mar 14, 2017
1 parent 3420c9c commit a5bdd20
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@google-cloud/common-grpc": "^0.2.1",
"arrify": "^1.0.0",
"async": "^2.1.4",
"eventid": "^0.1.0",
"extend": "^3.0.0",
"google-gax": "^0.12.2",
"google-proto-files": "^0.10.0",
Expand Down
14 changes: 14 additions & 0 deletions src/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
'use strict';

var commonGrpc = require('@google-cloud/common-grpc');
var EventId = require('eventid');
var extend = require('extend');
var is = require('is');
var isCircular = require('is-circular');

var eventId = new EventId();

/**
* Create an entry object to define new data to insert into a log.
*
Expand Down Expand Up @@ -87,6 +90,17 @@ function Entry(metadata, data) {
timestamp: new Date()
}, metadata);

// JavaScript date has a very coarse granularity (millisecond), which makes
// it quite likely that multiple log entries would have the same timestamp.
// The Logging API doesn't guarantee to preserve insertion order for entries
// with the same timestamp. The service does use `insertId` as a secondary
// ordering for entries with the same timestamp. `insertId` needs to be
// globally unique (within the project) however.
//
// We use a globally unique monotonically increasing EventId as the
// insertId.
this.metadata.insertId = this.metadata.insertId || eventId.new();

this.data = data;
}

Expand Down
20 changes: 18 additions & 2 deletions system-test/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ describe('Logging', function() {
var entry1 = log.entry('1');

setTimeout(function() {
var entry3 = log.entry('3');
var entry2 = log.entry({ timestamp: entry3.metadata.timestamp }, '2');
var entry2 = log.entry('2');
var entry3 = log.entry({ timestamp: entry2.metadata.timestamp }, '3');

// Re-arrange to confirm the timestamp is sent and honored.
log.write([entry2, entry3, entry1], options, function(err) {
Expand All @@ -369,6 +369,22 @@ describe('Logging', function() {
}, 1000);
});

it('should preserve order for sequential write calls', function(done) {
var messages = ['1', '2', '3', '4', '5'];

messages.forEach(function(message) {
log.write(log.entry(message));
});

setTimeout(function() {
log.getEntries({ pageSize: messages.length }, function(err, entries) {
assert.ifError(err);
assert.deepEqual(entries.reverse().map(prop('data')), messages);
done();
});
}, WRITE_CONSISTENCY_DELAY_MS);
});

it('should write an entry with primitive values', function(done) {
var logEntry = log.entry({
when: new Date(),
Expand Down
39 changes: 38 additions & 1 deletion test/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ var util = require('@google-cloud/common').util;

function FakeGrpcService() {}

var fakeEventIdNewOverride;

function FakeEventId() {}
FakeEventId.prototype.new = function() {
return (fakeEventIdNewOverride || util.noop).apply(null, arguments);
};

describe('Entry', function() {
var Entry;
var entry;
Expand All @@ -35,11 +42,13 @@ describe('Entry', function() {
Entry = proxyquire('../src/entry.js', {
'@google-cloud/common-grpc': {
Service: FakeGrpcService
}
},
'eventid': FakeEventId
});
});

beforeEach(function() {
fakeEventIdNewOverride = null;
extend(FakeGrpcService, GrpcService);
entry = new Entry(METADATA, DATA);
});
Expand Down Expand Up @@ -67,6 +76,34 @@ describe('Entry', function() {
assert.strictEqual(entry.metadata.timestamp, timestamp);
});

it('should assign insertId to metadata', function() {
var eventId = 'event-id';

fakeEventIdNewOverride = function() {
return eventId;
};

var entry = new Entry();

assert.strictEqual(entry.metadata.insertId, eventId);
});

it('should not assign insertId if one is already set', function() {
var eventId = 'event-id';

fakeEventIdNewOverride = function() {
return eventId;
};

var userDefinedInsertId = 'user-defined-insert-id';

var entry = new Entry({
insertId: userDefinedInsertId
});

assert.strictEqual(entry.metadata.insertId, userDefinedInsertId);
});

it('should localize data', function() {
assert.strictEqual(entry.data, DATA);
});
Expand Down

0 comments on commit a5bdd20

Please sign in to comment.