Skip to content

Commit

Permalink
chore(telemetry): set timestamp on telemetry event on created; always…
Browse files Browse the repository at this point in the history
… pass session_id MONGOSH-1646 (mongodb-js#1748)
  • Loading branch information
gribnoysup committed Nov 20, 2023
1 parent c92e10e commit 963c75b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 29 deletions.
38 changes: 28 additions & 10 deletions packages/logging/src/analytics-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { ToggleableAnalytics, ThrottledAnalytics } from './analytics-helpers';

const wait = promisify(setTimeout);

const timestamp = new Date();

describe('analytics helpers', function () {
let events: any[];
let target: MongoshAnalytics;
Expand All @@ -32,11 +34,16 @@ describe('analytics helpers', function () {
const toggleable = new ToggleableAnalytics(target);
expect(events).to.have.lengthOf(0);

toggleable.identify({ userId: 'me', traits: { platform: '1234' } });
toggleable.identify({
userId: 'me',
traits: { platform: '1234', session_id: 'abc' },
timestamp,
});
toggleable.track({
userId: 'me',
event: 'something',
properties: { mongosh_version: '1.2.3' },
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
timestamp,
});
expect(events).to.have.lengthOf(0);

Expand All @@ -46,15 +53,17 @@ describe('analytics helpers', function () {
toggleable.track({
userId: 'me',
event: 'something2',
properties: { mongosh_version: '1.2.3' },
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
timestamp,
});
expect(events).to.have.lengthOf(3);

toggleable.pause();
toggleable.track({
userId: 'me',
event: 'something3',
properties: { mongosh_version: '1.2.3' },
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
timestamp,
});
expect(events).to.have.lengthOf(3);

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

expect(events).to.deep.equal([
['identify', { userId: 'me', traits: { platform: '1234' } }],
[
'identify',
{
userId: 'me',
traits: { platform: '1234', session_id: 'abc' },
timestamp,
},
],
[
'track',
{
userId: 'me',
event: 'something',
properties: { mongosh_version: '1.2.3' },
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
timestamp,
},
],
[
'track',
{
userId: 'me',
event: 'something2',
properties: { mongosh_version: '1.2.3' },
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
timestamp,
},
],
]);
Expand All @@ -102,16 +120,16 @@ describe('analytics helpers', function () {
describe('ThrottledAnalytics', function () {
const metadataPath = os.tmpdir();
const userId = 'u-' + Date.now();
const iEvt = { userId, traits: { platform: 'what' } };
const iEvt = { userId, traits: { platform: 'what', session_id: 'abc' } };
const tEvt = {
userId,
event: 'hi',
properties: { mongosh_version: '1.2.3' },
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
};
const t2Evt = {
userId,
event: 'bye',
properties: { mongosh_version: '1.2.3' },
properties: { mongosh_version: '1.2.3', session_id: 'abc' },
};

afterEach(async function () {
Expand Down
18 changes: 14 additions & 4 deletions packages/logging/src/analytics-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ export type MongoshAnalyticsIdentity =
};

type AnalyticsIdentifyMessage = MongoshAnalyticsIdentity & {
traits: { platform: string };
traits: { platform: string; session_id: string };
timestamp?: Date;
};

type AnalyticsTrackMessage = MongoshAnalyticsIdentity & {
event: string;
properties: {
mongosh_version: string;
session_id: string;
[key: string]: any;
};
timestamp?: Date;
};

/**
Expand Down Expand Up @@ -90,6 +93,12 @@ type AnalyticsEventsQueueItem =
| ['identify', Parameters<MongoshAnalytics['identify']>]
| ['track', Parameters<MongoshAnalytics['track']>];

function addTimestamp<T extends { timestamp?: Date }>(
message: T
): T & { timestamp: Date } {
return { ...message, timestamp: message.timestamp ?? new Date() };
}

/**
* An implementation of MongoshAnalytics that forwards to another implementation
* and can be enabled/paused/disabled.
Expand All @@ -112,12 +121,12 @@ export class ToggleableAnalytics implements MongoshAnalytics {

identify(...args: Parameters<MongoshAnalytics['identify']>): void {
this._validateArgs(args);
this._queue.push(['identify', args]);
this._queue.push(['identify', [addTimestamp(args[0])]]);
}

track(...args: Parameters<MongoshAnalytics['track']>): void {
this._validateArgs(args);
this._queue.push(['track', args]);
this._queue.push(['track', [addTimestamp(args[0])]]);
}

enable() {
Expand Down Expand Up @@ -262,6 +271,7 @@ export class ThrottledAnalytics implements MongoshAnalytics {
}

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

track(message: AnalyticsTrackMessage): void {
this.trackQueue.push(message);
this.trackQueue.push(addTimestamp(message));
}

// Tries to restore persisted throttle state and returns `true` if telemetry can
Expand Down
21 changes: 20 additions & 1 deletion packages/logging/src/setup-logger-and-telemetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ describe('setupLoggerAndTelemetry', function () {
traits: {
platform: process.platform,
arch: process.arch,
session_id: '5fb3c20ee1507e894e5340f3',
},
},
],
Expand All @@ -361,6 +362,7 @@ describe('setupLoggerAndTelemetry', function () {
traits: {
platform: process.platform,
arch: process.arch,
session_id: '5fb3c20ee1507e894e5340f3',
},
},
],
Expand All @@ -385,6 +387,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'Error',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
name: 'MongoshInvalidInputError',
code: 'CLIREPL-1005',
scope: 'CLIREPL',
Expand All @@ -399,6 +402,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'Error',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
name: 'MongoshInvalidInputError',
code: 'CLIREPL-1005',
scope: 'CLIREPL',
Expand All @@ -411,7 +415,10 @@ describe('setupLoggerAndTelemetry', function () {
{
anonymousId: '53defe995fa47e6c13102d9d',
event: 'Use',
properties: { mongosh_version: '1.0.0' },
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
},
},
],
[
Expand All @@ -421,6 +428,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'Show',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
method: 'dbs',
},
},
Expand All @@ -431,6 +439,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'Script Loaded CLI',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
nested: true,
shell: true,
},
Expand All @@ -443,6 +452,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'Script Loaded',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
nested: false,
},
anonymousId: '53defe995fa47e6c13102d9d',
Expand All @@ -454,6 +464,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'Mongoshrc Loaded',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
},
anonymousId: '53defe995fa47e6c13102d9d',
},
Expand All @@ -464,6 +475,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'Mongorc Warning',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
},
anonymousId: '53defe995fa47e6c13102d9d',
},
Expand All @@ -474,6 +486,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'Script Evaluated',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
shell: true,
},
anonymousId: '53defe995fa47e6c13102d9d',
Expand All @@ -486,6 +499,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'Snippet Install',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
},
},
],
Expand Down Expand Up @@ -575,6 +589,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'Deprecated Method',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
class: 'Database',
method: 'cloneDatabase',
},
Expand All @@ -587,6 +602,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'Deprecated Method',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
class: 'Database',
method: 'copyDatabase',
},
Expand All @@ -599,6 +615,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'Deprecated Method',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
class: 'Database',
method: 'mangleDatabase',
},
Expand All @@ -611,6 +628,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'API Call',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
class: 'Database',
method: 'cloneDatabase',
count: 3,
Expand All @@ -624,6 +642,7 @@ describe('setupLoggerAndTelemetry', function () {
event: 'API Call',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
class: 'Database',
method: 'copyDatabase',
count: 1,
Expand Down
Loading

0 comments on commit 963c75b

Please sign in to comment.