Skip to content

Commit 7d8e203

Browse files
committed
Merge pull request #1274 from stephenplusplus/spp--1270
logging: format timestamp value
2 parents 7a508f6 + 1c5075d commit 7d8e203

File tree

4 files changed

+99
-17
lines changed

4 files changed

+99
-17
lines changed

lib/common/grpc-service.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,22 @@ GrpcService.convertValue_ = function(value) {
321321
convertedValue = {
322322
booleanValue: value
323323
};
324+
} else if (Buffer.isBuffer(value)) {
325+
convertedValue = {
326+
blobValue: value
327+
};
324328
} else if (is.object(value)) {
325329
convertedValue = GrpcService.objToStruct_(value);
330+
} else if (is.date(value)) {
331+
var seconds = value.getTime() / 1000;
332+
var secondsRounded = Math.floor(seconds);
333+
334+
convertedValue = {
335+
timestampValue: {
336+
seconds: secondsRounded,
337+
nanos: Math.floor((seconds - secondsRounded) * 1e9)
338+
}
339+
};
326340
} else if (is.array(value)) {
327341
convertedValue = {
328342
listValue: value.map(GrpcService.convertValue_)

lib/logging/entry.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,16 @@ function Entry(resource, data) {
101101
* @return {module:logging/entity}
102102
*/
103103
Entry.fromApiResponse_ = function(entry) {
104-
// Only one of these are populated.
105-
var data = entry.protoPayload || entry.jsonPayload || entry.textPayload;
106-
return extend(new Entry(entry.resource, data), entry);
104+
var data = entry[entry.payload];
105+
var serializedEntry = extend(new Entry(entry.resource, data), entry);
106+
107+
if (serializedEntry.timestamp) {
108+
var ms = serializedEntry.timestamp.seconds * 1000;
109+
ms += serializedEntry.timestamp.nanos / 1e6;
110+
serializedEntry.timestamp = new Date(ms);
111+
}
112+
113+
return serializedEntry;
107114
};
108115

109116
/**
@@ -143,6 +150,16 @@ Entry.prototype.toJSON = function() {
143150
entry.textPayload = this.data;
144151
}
145152

153+
if (is.date(entry.timestamp)) {
154+
var seconds = entry.timestamp.getTime() / 1000;
155+
var secondsRounded = Math.floor(seconds);
156+
157+
entry.timestamp = {
158+
seconds: secondsRounded,
159+
nanos: Math.floor((seconds - secondsRounded) * 1e9)
160+
};
161+
}
162+
146163
return entry;
147164
};
148165

test/common/grpc-service.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -677,19 +677,28 @@ describe('GrpcService', function() {
677677

678678
describe('convertValue_', function() {
679679
it('should convert primitive values correctly', function() {
680-
var convertedValues = extend(
681-
GrpcService.convertValue_(null),
682-
GrpcService.convertValue_(1),
683-
GrpcService.convertValue_('Hi'),
684-
GrpcService.convertValue_(true)
685-
);
680+
var buffer = new Buffer('Value');
686681

687-
assert.deepEqual(convertedValues, {
688-
nullValue: null,
689-
numberValue: 1,
690-
stringValue: 'Hi',
682+
assert.deepEqual(GrpcService.convertValue_(null), {
683+
nullValue: null
684+
});
685+
686+
assert.deepEqual(GrpcService.convertValue_(1), {
687+
numberValue: 1
688+
});
689+
690+
assert.deepEqual(GrpcService.convertValue_('Hi'), {
691+
stringValue: 'Hi'
692+
});
693+
694+
assert.deepEqual(GrpcService.convertValue_(true), {
691695
booleanValue: true
692696
});
697+
698+
assert.strictEqual(
699+
GrpcService.convertValue_(buffer).blobValue.toString(),
700+
'Value'
701+
);
693702
});
694703

695704
it('should convert objects', function() {
@@ -699,11 +708,26 @@ describe('GrpcService', function() {
699708
return value;
700709
};
701710

702-
var convertedValue = GrpcService.convertValue_({});
711+
var convertedValue = GrpcService.convertValue_(value);
703712

704713
assert.strictEqual(convertedValue, value);
705714
});
706715

716+
it('should convert dates', function() {
717+
var value = new Date();
718+
var seconds = value.getTime() / 1000;
719+
var secondsRounded = Math.floor(seconds);
720+
721+
var convertedValue = GrpcService.convertValue_(value);
722+
723+
assert.deepEqual(convertedValue, {
724+
timestampValue: {
725+
seconds: secondsRounded,
726+
nanos: Math.floor((seconds - secondsRounded) * 1e9)
727+
}
728+
});
729+
});
730+
707731
it('should convert arrays', function() {
708732
var convertedValue = GrpcService.convertValue_([1, 2, 3]);
709733

@@ -716,8 +740,8 @@ describe('GrpcService', function() {
716740

717741
it('should throw if a type is not recognized', function() {
718742
assert.throws(function() {
719-
GrpcService.convertValue_(new Date());
720-
}, 'Value of type Date not recognized.');
743+
GrpcService.convertValue_();
744+
}, 'Value of type undefined not recognized.');
721745
});
722746
});
723747

test/logging/entry.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,21 @@ describe('Entry', function() {
6767

6868
describe('fromApiResponse_', function() {
6969
var entry;
70+
var date = new Date();
7071

7172
beforeEach(function() {
73+
var seconds = date.getTime() / 1000;
74+
var secondsRounded = Math.floor(seconds);
75+
7276
entry = Entry.fromApiResponse_({
7377
resource: RESOURCE,
78+
payload: 'jsonPayload',
7479
jsonPayload: DATA,
75-
extraProperty: true
80+
extraProperty: true,
81+
timestamp: {
82+
seconds: secondsRounded,
83+
nanos: Math.floor((seconds - secondsRounded) * 1e9)
84+
}
7685
});
7786
});
7887

@@ -81,11 +90,13 @@ describe('Entry', function() {
8190
assert.strictEqual(entry.resource, RESOURCE);
8291
assert.strictEqual(entry.data, DATA);
8392
assert.strictEqual(entry.extraProperty, true);
93+
assert.deepEqual(entry.timestamp, date);
8494
});
8595

8696
it('should extend the entry with proto data', function() {
8797
var entry = Entry.fromApiResponse_({
8898
resource: RESOURCE,
99+
payload: 'protoPayload',
89100
protoPayload: DATA,
90101
extraProperty: true
91102
});
@@ -100,6 +111,7 @@ describe('Entry', function() {
100111
it('should extend the entry with text data', function() {
101112
var entry = Entry.fromApiResponse_({
102113
resource: RESOURCE,
114+
payload: 'textPayload',
103115
textPayload: DATA,
104116
extraProperty: true
105117
});
@@ -181,5 +193,20 @@ describe('Entry', function() {
181193
var json = entry.toJSON();
182194
assert.strictEqual(json.textPayload, entry.data);
183195
});
196+
197+
it('should convert a date', function() {
198+
var date = new Date();
199+
entry.timestamp = date;
200+
201+
var json = entry.toJSON();
202+
203+
var seconds = date.getTime() / 1000;
204+
var secondsRounded = Math.floor(seconds);
205+
206+
assert.deepEqual(json.timestamp, {
207+
seconds: secondsRounded,
208+
nanos: Math.floor((seconds - secondsRounded) * 1e9)
209+
});
210+
});
184211
});
185212
});

0 commit comments

Comments
 (0)