Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: accept entry.timestamp string input in RFC3339 format #937

Merged
merged 5 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {objToStruct, structToObj} from './common';

const eventId = new EventId();

export type Timestamp = google.protobuf.ITimestamp | Date;
export type Timestamp = google.protobuf.ITimestamp | Date | string;
export type LogSeverity = google.logging.type.LogSeverity | string;
export type LogEntry = Merge<
google.logging.v2.ILogEntry,
Expand Down Expand Up @@ -152,6 +152,7 @@ class Entry {
*/
toJSON(options: ToJsonOptions = {}) {
const entry = (extend(true, {}, this.metadata) as {}) as EntryJson;
// Format log message
if (is.object(this.data)) {
entry.jsonPayload = objToStruct(this.data, {
removeCircular: !!options.removeCircular,
Expand All @@ -160,13 +161,24 @@ class Entry {
} else if (is.string(this.data)) {
entry.textPayload = this.data;
}
// Format log timestamp
if (is.date(entry.timestamp)) {
const seconds = (entry.timestamp as Date).getTime() / 1000;
const secondsRounded = Math.floor(seconds);
entry.timestamp = {
seconds: secondsRounded,
nanos: Math.floor((seconds - secondsRounded) * 1e9),
};
} else if (is.string(entry.timestamp)) {
// Convert RFC3339 "Zulu" timestamp into a format that can be parsed to Date
const zuluTime = entry.timestamp as string;
const ms = Date.parse(zuluTime.split(/[.,Z]/)[0] + 'Z');
const reNano = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.(\d{0,9})Z$/;
const nanoSecs = zuluTime.match(reNano)?.[1];
entry.timestamp = {
seconds: ms ? Math.floor(ms / 1000) : 0,
nanos: nanoSecs ? parseInt(nanoSecs.padEnd(9, '0')) : 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's generally safer to use the Number constructor instead of parseInt. parseInt is more liberal in what it accepts, and in some cases can make assumptions about the radix (leading 0s and octal numbers, etc).

};
}
return entry;
}
Expand Down
36 changes: 35 additions & 1 deletion test/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ describe('Entry', () => {
assert.strictEqual(json.textPayload, entry.data);
});

it('should convert a date', () => {
it('should convert a date timestamp', () => {
const date = new Date();
entry.metadata.timestamp = date as entryTypes.Timestamp;
const json = entry.toJSON();
Expand All @@ -223,5 +223,39 @@ describe('Entry', () => {
nanos: Math.floor((seconds - secondsRounded) * 1e9),
});
});

it('should convert a string timestamp', () => {
const tests = [
{
inputTime: '2020-01-01T00:00:00.11Z',
expectedSeconds: 1577836800,
expectedNanos: 110000000,
},
{
inputTime: '2020-01-01T00:00:00Z',
expectedSeconds: 1577836800,
expectedNanos: 0,
},
{
inputTime: '2020-01-01T00:00:00.999999999Z',
expectedSeconds: 1577836800,
expectedNanos: 999999999,
},
{
inputTime: 'invalid timestamp string',
expectedSeconds: 0,
expectedNanos: 0,
},
];

for (const test of tests) {
entry.metadata.timestamp = test.inputTime;
const json = entry.toJSON();
assert.deepStrictEqual(json.timestamp, {
seconds: test.expectedSeconds,
nanos: test.expectedNanos,
});
}
});
});
});