Skip to content

Commit 1c5075d

Browse files
logging: format timestamp value
1 parent e9e4170 commit 1c5075d

4 files changed

Lines changed: 99 additions & 17 deletions

File tree

lib/common/grpc-service.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,22 @@ GrpcService.convertValue_ = function(value) {
289289
convertedValue = {
290290
booleanValue: value
291291
};
292+
} else if (Buffer.isBuffer(value)) {
293+
convertedValue = {
294+
blobValue: value
295+
};
292296
} else if (is.object(value)) {
293297
convertedValue = GrpcService.objToStruct_(value);
298+
} else if (is.date(value)) {
299+
var seconds = value.getTime() / 1000;
300+
var secondsRounded = Math.floor(seconds);
301+
302+
convertedValue = {
303+
timestampValue: {
304+
seconds: secondsRounded,
305+
nanos: Math.floor((seconds - secondsRounded) * 1e9)
306+
}
307+
};
294308
} else if (is.array(value)) {
295309
convertedValue = {
296310
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
@@ -554,19 +554,28 @@ describe('GrpcService', function() {
554554

555555
describe('convertValue_', function() {
556556
it('should convert primitive values correctly', function() {
557-
var convertedValues = extend(
558-
GrpcService.convertValue_(null),
559-
GrpcService.convertValue_(1),
560-
GrpcService.convertValue_('Hi'),
561-
GrpcService.convertValue_(true)
562-
);
557+
var buffer = new Buffer('Value');
563558

564-
assert.deepEqual(convertedValues, {
565-
nullValue: null,
566-
numberValue: 1,
567-
stringValue: 'Hi',
559+
assert.deepEqual(GrpcService.convertValue_(null), {
560+
nullValue: null
561+
});
562+
563+
assert.deepEqual(GrpcService.convertValue_(1), {
564+
numberValue: 1
565+
});
566+
567+
assert.deepEqual(GrpcService.convertValue_('Hi'), {
568+
stringValue: 'Hi'
569+
});
570+
571+
assert.deepEqual(GrpcService.convertValue_(true), {
568572
booleanValue: true
569573
});
574+
575+
assert.strictEqual(
576+
GrpcService.convertValue_(buffer).blobValue.toString(),
577+
'Value'
578+
);
570579
});
571580

572581
it('should convert objects', function() {
@@ -576,11 +585,26 @@ describe('GrpcService', function() {
576585
return value;
577586
};
578587

579-
var convertedValue = GrpcService.convertValue_({});
588+
var convertedValue = GrpcService.convertValue_(value);
580589

581590
assert.strictEqual(convertedValue, value);
582591
});
583592

593+
it('should convert dates', function() {
594+
var value = new Date();
595+
var seconds = value.getTime() / 1000;
596+
var secondsRounded = Math.floor(seconds);
597+
598+
var convertedValue = GrpcService.convertValue_(value);
599+
600+
assert.deepEqual(convertedValue, {
601+
timestampValue: {
602+
seconds: secondsRounded,
603+
nanos: Math.floor((seconds - secondsRounded) * 1e9)
604+
}
605+
});
606+
});
607+
584608
it('should convert arrays', function() {
585609
var convertedValue = GrpcService.convertValue_([1, 2, 3]);
586610

@@ -593,8 +617,8 @@ describe('GrpcService', function() {
593617

594618
it('should throw if a type is not recognized', function() {
595619
assert.throws(function() {
596-
GrpcService.convertValue_(new Date());
597-
}, 'Value of type Date not recognized.');
620+
GrpcService.convertValue_();
621+
}, 'Value of type undefined not recognized.');
598622
});
599623
});
600624

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)