Skip to content

Commit 963c75b

Browse files
authored
chore(telemetry): set timestamp on telemetry event on created; always pass session_id MONGOSH-1646 (#1748)
1 parent c92e10e commit 963c75b

File tree

4 files changed

+82
-29
lines changed

4 files changed

+82
-29
lines changed

packages/logging/src/analytics-helpers.spec.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { ToggleableAnalytics, ThrottledAnalytics } from './analytics-helpers';
88

99
const wait = promisify(setTimeout);
1010

11+
const timestamp = new Date();
12+
1113
describe('analytics helpers', function () {
1214
let events: any[];
1315
let target: MongoshAnalytics;
@@ -32,11 +34,16 @@ describe('analytics helpers', function () {
3234
const toggleable = new ToggleableAnalytics(target);
3335
expect(events).to.have.lengthOf(0);
3436

35-
toggleable.identify({ userId: 'me', traits: { platform: '1234' } });
37+
toggleable.identify({
38+
userId: 'me',
39+
traits: { platform: '1234', session_id: 'abc' },
40+
timestamp,
41+
});
3642
toggleable.track({
3743
userId: 'me',
3844
event: 'something',
39-
properties: { mongosh_version: '1.2.3' },
45+
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
46+
timestamp,
4047
});
4148
expect(events).to.have.lengthOf(0);
4249

@@ -46,15 +53,17 @@ describe('analytics helpers', function () {
4653
toggleable.track({
4754
userId: 'me',
4855
event: 'something2',
49-
properties: { mongosh_version: '1.2.3' },
56+
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
57+
timestamp,
5058
});
5159
expect(events).to.have.lengthOf(3);
5260

5361
toggleable.pause();
5462
toggleable.track({
5563
userId: 'me',
5664
event: 'something3',
57-
properties: { mongosh_version: '1.2.3' },
65+
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
66+
timestamp,
5867
});
5968
expect(events).to.have.lengthOf(3);
6069

@@ -63,21 +72,30 @@ describe('analytics helpers', function () {
6372
toggleable.enable();
6473

6574
expect(events).to.deep.equal([
66-
['identify', { userId: 'me', traits: { platform: '1234' } }],
75+
[
76+
'identify',
77+
{
78+
userId: 'me',
79+
traits: { platform: '1234', session_id: 'abc' },
80+
timestamp,
81+
},
82+
],
6783
[
6884
'track',
6985
{
7086
userId: 'me',
7187
event: 'something',
72-
properties: { mongosh_version: '1.2.3' },
88+
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
89+
timestamp,
7390
},
7491
],
7592
[
7693
'track',
7794
{
7895
userId: 'me',
7996
event: 'something2',
80-
properties: { mongosh_version: '1.2.3' },
97+
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
98+
timestamp,
8199
},
82100
],
83101
]);
@@ -102,16 +120,16 @@ describe('analytics helpers', function () {
102120
describe('ThrottledAnalytics', function () {
103121
const metadataPath = os.tmpdir();
104122
const userId = 'u-' + Date.now();
105-
const iEvt = { userId, traits: { platform: 'what' } };
123+
const iEvt = { userId, traits: { platform: 'what', session_id: 'abc' } };
106124
const tEvt = {
107125
userId,
108126
event: 'hi',
109-
properties: { mongosh_version: '1.2.3' },
127+
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
110128
};
111129
const t2Evt = {
112130
userId,
113131
event: 'bye',
114-
properties: { mongosh_version: '1.2.3' },
132+
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
115133
};
116134

117135
afterEach(async function () {

packages/logging/src/analytics-helpers.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ export type MongoshAnalyticsIdentity =
1212
};
1313

1414
type AnalyticsIdentifyMessage = MongoshAnalyticsIdentity & {
15-
traits: { platform: string };
15+
traits: { platform: string; session_id: string };
16+
timestamp?: Date;
1617
};
1718

1819
type AnalyticsTrackMessage = MongoshAnalyticsIdentity & {
1920
event: string;
2021
properties: {
2122
mongosh_version: string;
23+
session_id: string;
2224
[key: string]: any;
2325
};
26+
timestamp?: Date;
2427
};
2528

2629
/**
@@ -90,6 +93,12 @@ type AnalyticsEventsQueueItem =
9093
| ['identify', Parameters<MongoshAnalytics['identify']>]
9194
| ['track', Parameters<MongoshAnalytics['track']>];
9295

96+
function addTimestamp<T extends { timestamp?: Date }>(
97+
message: T
98+
): T & { timestamp: Date } {
99+
return { ...message, timestamp: message.timestamp ?? new Date() };
100+
}
101+
93102
/**
94103
* An implementation of MongoshAnalytics that forwards to another implementation
95104
* and can be enabled/paused/disabled.
@@ -112,12 +121,12 @@ export class ToggleableAnalytics implements MongoshAnalytics {
112121

113122
identify(...args: Parameters<MongoshAnalytics['identify']>): void {
114123
this._validateArgs(args);
115-
this._queue.push(['identify', args]);
124+
this._queue.push(['identify', [addTimestamp(args[0])]]);
116125
}
117126

118127
track(...args: Parameters<MongoshAnalytics['track']>): void {
119128
this._validateArgs(args);
120-
this._queue.push(['track', args]);
129+
this._queue.push(['track', [addTimestamp(args[0])]]);
121130
}
122131

123132
enable() {
@@ -262,6 +271,7 @@ export class ThrottledAnalytics implements MongoshAnalytics {
262271
}
263272

264273
identify(message: AnalyticsIdentifyMessage): void {
274+
message = addTimestamp(message);
265275
if (this.currentUserId) {
266276
throw new Error('Identify can only be called once per user session');
267277
}
@@ -280,7 +290,7 @@ export class ThrottledAnalytics implements MongoshAnalytics {
280290
}
281291

282292
track(message: AnalyticsTrackMessage): void {
283-
this.trackQueue.push(message);
293+
this.trackQueue.push(addTimestamp(message));
284294
}
285295

286296
// Tries to restore persisted throttle state and returns `true` if telemetry can

packages/logging/src/setup-logger-and-telemetry.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ describe('setupLoggerAndTelemetry', function () {
351351
traits: {
352352
platform: process.platform,
353353
arch: process.arch,
354+
session_id: '5fb3c20ee1507e894e5340f3',
354355
},
355356
},
356357
],
@@ -361,6 +362,7 @@ describe('setupLoggerAndTelemetry', function () {
361362
traits: {
362363
platform: process.platform,
363364
arch: process.arch,
365+
session_id: '5fb3c20ee1507e894e5340f3',
364366
},
365367
},
366368
],
@@ -385,6 +387,7 @@ describe('setupLoggerAndTelemetry', function () {
385387
event: 'Error',
386388
properties: {
387389
mongosh_version: '1.0.0',
390+
session_id: '5fb3c20ee1507e894e5340f3',
388391
name: 'MongoshInvalidInputError',
389392
code: 'CLIREPL-1005',
390393
scope: 'CLIREPL',
@@ -399,6 +402,7 @@ describe('setupLoggerAndTelemetry', function () {
399402
event: 'Error',
400403
properties: {
401404
mongosh_version: '1.0.0',
405+
session_id: '5fb3c20ee1507e894e5340f3',
402406
name: 'MongoshInvalidInputError',
403407
code: 'CLIREPL-1005',
404408
scope: 'CLIREPL',
@@ -411,7 +415,10 @@ describe('setupLoggerAndTelemetry', function () {
411415
{
412416
anonymousId: '53defe995fa47e6c13102d9d',
413417
event: 'Use',
414-
properties: { mongosh_version: '1.0.0' },
418+
properties: {
419+
mongosh_version: '1.0.0',
420+
session_id: '5fb3c20ee1507e894e5340f3',
421+
},
415422
},
416423
],
417424
[
@@ -421,6 +428,7 @@ describe('setupLoggerAndTelemetry', function () {
421428
event: 'Show',
422429
properties: {
423430
mongosh_version: '1.0.0',
431+
session_id: '5fb3c20ee1507e894e5340f3',
424432
method: 'dbs',
425433
},
426434
},
@@ -431,6 +439,7 @@ describe('setupLoggerAndTelemetry', function () {
431439
event: 'Script Loaded CLI',
432440
properties: {
433441
mongosh_version: '1.0.0',
442+
session_id: '5fb3c20ee1507e894e5340f3',
434443
nested: true,
435444
shell: true,
436445
},
@@ -443,6 +452,7 @@ describe('setupLoggerAndTelemetry', function () {
443452
event: 'Script Loaded',
444453
properties: {
445454
mongosh_version: '1.0.0',
455+
session_id: '5fb3c20ee1507e894e5340f3',
446456
nested: false,
447457
},
448458
anonymousId: '53defe995fa47e6c13102d9d',
@@ -454,6 +464,7 @@ describe('setupLoggerAndTelemetry', function () {
454464
event: 'Mongoshrc Loaded',
455465
properties: {
456466
mongosh_version: '1.0.0',
467+
session_id: '5fb3c20ee1507e894e5340f3',
457468
},
458469
anonymousId: '53defe995fa47e6c13102d9d',
459470
},
@@ -464,6 +475,7 @@ describe('setupLoggerAndTelemetry', function () {
464475
event: 'Mongorc Warning',
465476
properties: {
466477
mongosh_version: '1.0.0',
478+
session_id: '5fb3c20ee1507e894e5340f3',
467479
},
468480
anonymousId: '53defe995fa47e6c13102d9d',
469481
},
@@ -474,6 +486,7 @@ describe('setupLoggerAndTelemetry', function () {
474486
event: 'Script Evaluated',
475487
properties: {
476488
mongosh_version: '1.0.0',
489+
session_id: '5fb3c20ee1507e894e5340f3',
477490
shell: true,
478491
},
479492
anonymousId: '53defe995fa47e6c13102d9d',
@@ -486,6 +499,7 @@ describe('setupLoggerAndTelemetry', function () {
486499
event: 'Snippet Install',
487500
properties: {
488501
mongosh_version: '1.0.0',
502+
session_id: '5fb3c20ee1507e894e5340f3',
489503
},
490504
},
491505
],
@@ -575,6 +589,7 @@ describe('setupLoggerAndTelemetry', function () {
575589
event: 'Deprecated Method',
576590
properties: {
577591
mongosh_version: '1.0.0',
592+
session_id: '5fb3c20ee1507e894e5340f3',
578593
class: 'Database',
579594
method: 'cloneDatabase',
580595
},
@@ -587,6 +602,7 @@ describe('setupLoggerAndTelemetry', function () {
587602
event: 'Deprecated Method',
588603
properties: {
589604
mongosh_version: '1.0.0',
605+
session_id: '5fb3c20ee1507e894e5340f3',
590606
class: 'Database',
591607
method: 'copyDatabase',
592608
},
@@ -599,6 +615,7 @@ describe('setupLoggerAndTelemetry', function () {
599615
event: 'Deprecated Method',
600616
properties: {
601617
mongosh_version: '1.0.0',
618+
session_id: '5fb3c20ee1507e894e5340f3',
602619
class: 'Database',
603620
method: 'mangleDatabase',
604621
},
@@ -611,6 +628,7 @@ describe('setupLoggerAndTelemetry', function () {
611628
event: 'API Call',
612629
properties: {
613630
mongosh_version: '1.0.0',
631+
session_id: '5fb3c20ee1507e894e5340f3',
614632
class: 'Database',
615633
method: 'cloneDatabase',
616634
count: 3,
@@ -624,6 +642,7 @@ describe('setupLoggerAndTelemetry', function () {
624642
event: 'API Call',
625643
properties: {
626644
mongosh_version: '1.0.0',
645+
session_id: '5fb3c20ee1507e894e5340f3',
627646
class: 'Database',
628647
method: 'copyDatabase',
629648
count: 1,

0 commit comments

Comments
 (0)