Skip to content

Commit 4dfb8b6

Browse files
authored
Merge pull request #1899 from mozilla/default-event-timestamp
Bug 1886113 - Add wall-clock timestamps to all events
2 parents 26e5e37 + 204de38 commit 4dfb8b6

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

glean/src/core/metrics/types/event.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export class InternalEventMetricType<
4949
return;
5050
}
5151

52+
const ts = Date.now();
53+
5254
try {
5355
// Create metric here, in order to run the validations and throw in case input in invalid.
5456
const metric = new RecordedEvent({
@@ -59,9 +61,8 @@ export class InternalEventMetricType<
5961
});
6062

6163
// Truncate the extra keys, if needed.
62-
let truncatedExtra: ExtraMap | undefined = undefined;
64+
const truncatedExtra: ExtraMap = {};
6365
if (extra && this.allowedExtraKeys) {
64-
truncatedExtra = {};
6566
for (const [name, value] of Object.entries(extra)) {
6667
if (this.allowedExtraKeys.includes(name)) {
6768
if (isString(value)) {
@@ -84,6 +85,8 @@ export class InternalEventMetricType<
8485
}
8586
}
8687

88+
// Glean wall-clock timestamp added to all events
89+
truncatedExtra["glean_timestamp"] = ts.toString();
8790
metric.set({
8891
...metric.get(),
8992
extra: truncatedExtra
@@ -111,7 +114,19 @@ export class InternalEventMetricType<
111114
*/
112115
testGetValue(ping: string = this.sendInPings[0]): Event[] | undefined {
113116
if (testOnlyCheck("testGetValue", LOG_TAG)) {
114-
return Context.eventsDatabase.getEvents(ping, this);
117+
const events = Context.eventsDatabase.getEvents(ping, this);
118+
if (!events) return events;
119+
120+
events.forEach((ev) => {
121+
if (ev.extra) {
122+
delete ev.extra["glean_timestamp"];
123+
if (Object.keys(ev.extra).length == 0) {
124+
ev.extra = undefined;
125+
}
126+
}
127+
});
128+
129+
return events;
115130
}
116131
}
117132
}

0 commit comments

Comments
 (0)