Skip to content

Commit acc71e3

Browse files
committed
Stringify extras values when building ping payload
1 parent e768bbe commit acc71e3

File tree

4 files changed

+61
-18
lines changed

4 files changed

+61
-18
lines changed

glean/src/core/metrics/events_database/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class EventsDatabase {
278278
* 1. Sorts event by execution counter and timestamp;
279279
* 2. Applies offset to events timestamps;
280280
* 3. Removes the first event if it is a `glean.restarted` event;
281-
* 4. Removes reserved extra keys.
281+
* 4. Removes reserved extra keys and stringifies all extras values.
282282
*
283283
* @param pingName The name of the ping for which the payload is being prepared.
284284
* @param pingData An unsorted list of events.
@@ -368,7 +368,7 @@ class EventsDatabase {
368368
);
369369
}
370370

371-
return sortedEvents.map((e) => RecordedEvent.toJSONObject(e.withoutReservedExtras()));
371+
return sortedEvents.map((e) => RecordedEvent.toJSONObject(e.payload()));
372372
}
373373

374374
/**

glean/src/core/metrics/events_database/recorded_event.ts

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ export class RecordedEvent {
4848
);
4949
}
5050

51+
private static withTransformedExtras(
52+
e: RecordedEvent,
53+
transformFn: (extras: ExtraMap) => ExtraMap
54+
): RecordedEvent {
55+
const extras = e.extra || {};
56+
const transformedExtras = transformFn(extras);
57+
return new RecordedEvent(
58+
e.category,
59+
e.name,
60+
e.timestamp,
61+
(transformedExtras && Object.keys(transformedExtras).length > 0) ? transformedExtras : undefined
62+
);
63+
}
64+
5165
/**
5266
* Add another extra key to a RecordedEvent object.
5367
*
@@ -69,19 +83,38 @@ export class RecordedEvent {
6983
* @returns A new RecordedEvent object.
7084
*/
7185
withoutReservedExtras(): RecordedEvent {
72-
const extras = this.extra || {};
73-
const filteredExtras = Object.keys(extras)
74-
.filter(key => !GLEAN_RESERVED_EXTRA_KEYS.includes(key))
75-
.reduce((obj: ExtraMap, key) => {
76-
obj[key] = extras[key];
77-
return obj;
78-
}, {});
86+
return RecordedEvent.withTransformedExtras(
87+
this,
88+
(extras: ExtraMap): ExtraMap => {
89+
return Object.keys(extras)
90+
.filter(key => !GLEAN_RESERVED_EXTRA_KEYS.includes(key))
91+
.reduce((obj: ExtraMap, key) => {
92+
obj[key] = extras[key];
93+
return obj;
94+
}, {});
95+
}
96+
);
97+
}
7998

80-
return new RecordedEvent(
81-
this.category,
82-
this.name,
83-
this.timestamp,
84-
(filteredExtras && Object.keys(filteredExtras).length > 0) ? filteredExtras : undefined
99+
/**
100+
* Generate a new RecordedEvent object,
101+
* in the format expected on ping payloads.
102+
*
103+
* Strips reserved extra keys
104+
* and stringifies all event extras.
105+
*
106+
* @returns A new RecordedEvent object.
107+
*/
108+
payload(): RecordedEvent {
109+
return RecordedEvent.withTransformedExtras(
110+
this.withoutReservedExtras(),
111+
(extras: ExtraMap): ExtraMap => {
112+
return Object.keys(extras)
113+
.reduce((extra: Record<string, string>, key) => {
114+
extra[key] = extras[key].toString();
115+
return extra;
116+
}, {});
117+
}
85118
);
86119
}
87120
}

glean/tests/integration/schema/metrics.yaml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,15 @@ for_testing:
139139
send_in_pings:
140140
- testing
141141
extra_keys:
142-
sample:
143-
description: |
144-
Sample extra key.
142+
sample_string:
143+
description: Sample string extra key.
144+
type: string
145+
sample_boolean:
146+
description: Sample boolean extra key.
147+
type: boolean
148+
sample_quantity:
149+
description: Sample quantity extra key.
150+
type: quantity
145151
quantity:
146152
type: quantity
147153
description: |

glean/tests/integration/schema/schema.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ describe("schema", function() {
6060
metrics.boolean.set(false);
6161
metrics.counter.add(10);
6262
metrics.datetime.set();
63-
metrics.event.record({ sample: "hey" });
63+
metrics.event.record({
64+
sample_string: "hey",
65+
sample_boolean: false,
66+
sample_quantity: 42,
67+
});
6468
metrics.labeledBoolean["a_label"].set(true);
6569
metrics.labeledCounter["a_label"].add();
6670
metrics.labeledString["a_label"].set("ho");

0 commit comments

Comments
 (0)