Skip to content

Commit 81dbf27

Browse files
authored
chore(logging): move device_id into Segment identity traits MONGOSH-2234 (#2450)
1 parent a439de9 commit 81dbf27

File tree

4 files changed

+62
-52
lines changed

4 files changed

+62
-52
lines changed

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ describe('analytics helpers', function () {
4040

4141
toggleable.identify({
4242
userId: 'me',
43-
traits: { platform: '1234', session_id: 'abc' },
43+
traits: {
44+
platform: '1234',
45+
session_id: 'abc',
46+
device_id: 'test-device-id',
47+
},
4448
timestamp,
4549
});
4650
toggleable.track({
@@ -80,7 +84,11 @@ describe('analytics helpers', function () {
8084
'identify',
8185
{
8286
userId: 'me',
83-
traits: { platform: '1234', session_id: 'abc' },
87+
traits: {
88+
platform: '1234',
89+
session_id: 'abc',
90+
device_id: 'test-device-id',
91+
},
8492
timestamp,
8593
},
8694
],
@@ -124,7 +132,14 @@ describe('analytics helpers', function () {
124132
describe('ThrottledAnalytics', function () {
125133
const metadataPath = os.tmpdir();
126134
const userId = 'u-' + Date.now();
127-
const iEvt = { userId, traits: { platform: 'what', session_id: 'abc' } };
135+
const iEvt = {
136+
userId,
137+
traits: {
138+
platform: 'what',
139+
session_id: 'abc',
140+
device_id: 'test-device-id',
141+
},
142+
};
128143
const tEvt = {
129144
userId,
130145
event: 'hi',
@@ -252,7 +267,14 @@ describe('analytics helpers', function () {
252267

253268
describe('SampledAnalytics', function () {
254269
const userId = `u-${Date.now()}`;
255-
const iEvt = { userId, traits: { platform: 'what', session_id: 'abc' } };
270+
const iEvt = {
271+
userId,
272+
traits: {
273+
platform: 'what',
274+
session_id: 'abc',
275+
device_id: 'test-device-id',
276+
},
277+
};
256278
const tEvt = {
257279
userId,
258280
event: 'hi',

packages/logging/src/analytics-helpers.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import fs from 'fs';
22
import path from 'path';
3+
import type {
4+
IdentifyParams as SegmentIdentifyParams,
5+
TrackParams as SegmentTrackParams,
6+
} from '@segment/analytics-node';
37

4-
export type MongoshAnalyticsIdentity =
5-
| {
6-
deviceId?: string;
7-
userId: string;
8-
anonymousId?: never;
9-
}
10-
| {
11-
deviceId?: string;
12-
userId?: never;
13-
anonymousId: string;
14-
};
8+
type Timestamp = SegmentTrackParams['timestamp'];
9+
10+
export type MongoshAnalyticsIdentity = SegmentIdentifyParams;
1511

1612
export type AnalyticsIdentifyMessage = MongoshAnalyticsIdentity & {
17-
traits: { platform: string; session_id: string };
18-
timestamp?: Date;
13+
traits: {
14+
platform: string;
15+
session_id: string;
16+
device_id: string;
17+
} & SegmentIdentifyParams['traits'];
1918
};
2019

2120
export type AnalyticsTrackMessage = MongoshAnalyticsIdentity & {
@@ -25,7 +24,7 @@ export type AnalyticsTrackMessage = MongoshAnalyticsIdentity & {
2524
session_id: string;
2625
[key: string]: any;
2726
};
28-
timestamp?: Date;
27+
timestamp?: Timestamp;
2928
};
3029

3130
/**
@@ -93,10 +92,14 @@ type AnalyticsEventsQueueItem =
9392
| ['identify', Parameters<MongoshAnalytics['identify']>]
9493
| ['track', Parameters<MongoshAnalytics['track']>];
9594

96-
function addTimestamp<T extends { timestamp?: Date }>(
95+
function addTimestamp<T extends { timestamp?: Timestamp }>(
9796
message: T
98-
): T & { timestamp: Date } {
99-
return { ...message, timestamp: message.timestamp ?? new Date() };
97+
): T & { timestamp: Timestamp } {
98+
const timestampDate =
99+
message.timestamp instanceof Date || message.timestamp === undefined
100+
? message.timestamp
101+
: new Date(message.timestamp);
102+
return { ...message, timestamp: timestampDate };
100103
}
101104

102105
/**
@@ -275,7 +278,10 @@ export class ThrottledAnalytics implements MongoshAnalytics {
275278
if (this.currentUserId) {
276279
throw new Error('Identify can only be called once per user session');
277280
}
278-
this.currentUserId = message.userId ?? message.anonymousId;
281+
282+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
283+
this.currentUserId = message.userId ?? message.anonymousId!;
284+
279285
this.restorePromise = this.restoreThrottleState().then((enabled) => {
280286
if (!enabled) {
281287
this.trackQueue.disable();

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

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('MongoshLoggingAndTelemetry', function () {
1919

2020
const userId = '53defe995fa47e6c13102d9d';
2121
const logId = '5fb3c20ee1507e894e5340f3';
22+
const deviceId = 'test-device';
2223

2324
let logger: MongoLogWriter;
2425

@@ -39,7 +40,6 @@ describe('MongoshLoggingAndTelemetry', function () {
3940
const testLoggingArguments: Omit<MongoshLoggingAndTelemetryArguments, 'bus'> =
4041
{
4142
analytics,
42-
deviceId: 'test-device',
4343
userTraits: {
4444
platform: process.platform,
4545
arch: process.arch,
@@ -54,6 +54,7 @@ describe('MongoshLoggingAndTelemetry', function () {
5454

5555
loggingAndTelemetry = setupLoggingAndTelemetry({
5656
...testLoggingArguments,
57+
deviceId,
5758
bus,
5859
});
5960

@@ -117,8 +118,8 @@ describe('MongoshLoggingAndTelemetry', function () {
117118
'identify',
118119
{
119120
anonymousId: userId,
120-
deviceId: 'test-device',
121121
traits: {
122+
device_id: deviceId,
122123
arch: process.arch,
123124
platform: process.platform,
124125
session_id: logId,
@@ -129,7 +130,6 @@ describe('MongoshLoggingAndTelemetry', function () {
129130
'track',
130131
{
131132
anonymousId: userId,
132-
deviceId: 'test-device',
133133
event: 'New Connection',
134134
properties: {
135135
mongosh_version: '1.0.0',
@@ -178,8 +178,8 @@ describe('MongoshLoggingAndTelemetry', function () {
178178
'identify',
179179
{
180180
anonymousId: userId,
181-
deviceId: 'test-device',
182181
traits: {
182+
device_id: deviceId,
183183
arch: process.arch,
184184
platform: process.platform,
185185
session_id: logId,
@@ -190,7 +190,6 @@ describe('MongoshLoggingAndTelemetry', function () {
190190
'track',
191191
{
192192
anonymousId: userId,
193-
deviceId: 'test-device',
194193
event: 'New Connection',
195194
properties: {
196195
mongosh_version: '1.0.0',
@@ -235,9 +234,9 @@ describe('MongoshLoggingAndTelemetry', function () {
235234
[
236235
'identify',
237236
{
238-
deviceId: 'unknown',
239237
anonymousId: userId,
240238
traits: {
239+
device_id: 'unknown',
241240
platform: process.platform,
242241
arch: process.arch,
243242
session_id: logId,
@@ -272,9 +271,9 @@ describe('MongoshLoggingAndTelemetry', function () {
272271
[
273272
'identify',
274273
{
275-
deviceId,
276274
anonymousId: userId,
277275
traits: {
276+
device_id: deviceId,
278277
platform: process.platform,
279278
arch: process.arch,
280279
session_id: logId,
@@ -299,7 +298,7 @@ describe('MongoshLoggingAndTelemetry', function () {
299298
...testLoggingArguments,
300299
bus,
301300
deviceId: undefined,
302-
});
301+
}) as LoggingAndTelemetry;
303302

304303
loggingAndTelemetry.attachLogger(logger);
305304

@@ -310,13 +309,13 @@ describe('MongoshLoggingAndTelemetry', function () {
310309
expect(analyticsOutput).to.have.lengthOf(0);
311310

312311
resolveTelemetry('1234');
313-
await (loggingAndTelemetry as LoggingAndTelemetry).setupTelemetryPromise;
312+
await loggingAndTelemetry.setupTelemetryPromise;
314313

315314
expect(logOutput).to.have.lengthOf(1);
316315
expect(analyticsOutput).to.have.lengthOf(1);
317316

318317
// Hash created from machine ID 1234
319-
expect(analyticsOutput[0][1].deviceId).equals(
318+
expect(loggingAndTelemetry['deviceId']).equals(
320319
'8c9f929608f0ef13bfd5a290e0233f283e2cc25ffefc2ad8d9ef0650eb224a52'
321320
);
322321
});
@@ -719,8 +718,8 @@ describe('MongoshLoggingAndTelemetry', function () {
719718
'identify',
720719
{
721720
anonymousId: userId,
722-
deviceId: 'test-device',
723721
traits: {
722+
device_id: deviceId,
724723
platform: process.platform,
725724
arch: process.arch,
726725
session_id: logId,
@@ -731,8 +730,8 @@ describe('MongoshLoggingAndTelemetry', function () {
731730
'identify',
732731
{
733732
anonymousId: userId,
734-
deviceId: 'test-device',
735733
traits: {
734+
device_id: deviceId,
736735
platform: process.platform,
737736
arch: process.arch,
738737
session_id: logId,
@@ -743,7 +742,6 @@ describe('MongoshLoggingAndTelemetry', function () {
743742
'track',
744743
{
745744
anonymousId: userId,
746-
deviceId: 'test-device',
747745
event: 'Startup Time',
748746
properties: {
749747
is_interactive: true,
@@ -759,7 +757,6 @@ describe('MongoshLoggingAndTelemetry', function () {
759757
'track',
760758
{
761759
anonymousId: '53defe995fa47e6c13102d9d',
762-
deviceId: 'test-device',
763760
event: 'Error',
764761
properties: {
765762
mongosh_version: '1.0.0',
@@ -775,7 +772,6 @@ describe('MongoshLoggingAndTelemetry', function () {
775772
'track',
776773
{
777774
anonymousId: '53defe995fa47e6c13102d9d',
778-
deviceId: 'test-device',
779775
event: 'Error',
780776
properties: {
781777
mongosh_version: '1.0.0',
@@ -791,7 +787,6 @@ describe('MongoshLoggingAndTelemetry', function () {
791787
'track',
792788
{
793789
anonymousId: '53defe995fa47e6c13102d9d',
794-
deviceId: 'test-device',
795790
event: 'Use',
796791
properties: {
797792
mongosh_version: '1.0.0',
@@ -803,7 +798,6 @@ describe('MongoshLoggingAndTelemetry', function () {
803798
'track',
804799
{
805800
anonymousId: '53defe995fa47e6c13102d9d',
806-
deviceId: 'test-device',
807801
event: 'Show',
808802
properties: {
809803
mongosh_version: '1.0.0',
@@ -823,7 +817,6 @@ describe('MongoshLoggingAndTelemetry', function () {
823817
shell: true,
824818
},
825819
anonymousId: '53defe995fa47e6c13102d9d',
826-
deviceId: 'test-device',
827820
},
828821
],
829822
[
@@ -836,7 +829,6 @@ describe('MongoshLoggingAndTelemetry', function () {
836829
nested: false,
837830
},
838831
anonymousId: '53defe995fa47e6c13102d9d',
839-
deviceId: 'test-device',
840832
},
841833
],
842834
[
@@ -848,7 +840,6 @@ describe('MongoshLoggingAndTelemetry', function () {
848840
session_id: logId,
849841
},
850842
anonymousId: '53defe995fa47e6c13102d9d',
851-
deviceId: 'test-device',
852843
},
853844
],
854845
[
@@ -860,7 +851,6 @@ describe('MongoshLoggingAndTelemetry', function () {
860851
session_id: logId,
861852
},
862853
anonymousId: '53defe995fa47e6c13102d9d',
863-
deviceId: 'test-device',
864854
},
865855
],
866856
[
@@ -873,14 +863,12 @@ describe('MongoshLoggingAndTelemetry', function () {
873863
shell: true,
874864
},
875865
anonymousId: '53defe995fa47e6c13102d9d',
876-
deviceId: 'test-device',
877866
},
878867
],
879868
[
880869
'track',
881870
{
882871
anonymousId: '53defe995fa47e6c13102d9d',
883-
deviceId: 'test-device',
884872
event: 'Snippet Install',
885873
properties: {
886874
mongosh_version: '1.0.0',
@@ -976,7 +964,6 @@ describe('MongoshLoggingAndTelemetry', function () {
976964
'track',
977965
{
978966
anonymousId: '53defe995fa47e6c13102d9d',
979-
deviceId: 'test-device',
980967
event: 'Deprecated Method',
981968
properties: {
982969
mongosh_version: '1.0.0',
@@ -990,7 +977,6 @@ describe('MongoshLoggingAndTelemetry', function () {
990977
'track',
991978
{
992979
anonymousId: '53defe995fa47e6c13102d9d',
993-
deviceId: 'test-device',
994980
event: 'Deprecated Method',
995981
properties: {
996982
mongosh_version: '1.0.0',
@@ -1004,7 +990,6 @@ describe('MongoshLoggingAndTelemetry', function () {
1004990
'track',
1005991
{
1006992
anonymousId: '53defe995fa47e6c13102d9d',
1007-
deviceId: 'test-device',
1008993
event: 'Deprecated Method',
1009994
properties: {
1010995
mongosh_version: '1.0.0',
@@ -1018,7 +1003,6 @@ describe('MongoshLoggingAndTelemetry', function () {
10181003
'track',
10191004
{
10201005
anonymousId: '53defe995fa47e6c13102d9d',
1021-
deviceId: 'test-device',
10221006
event: 'API Call',
10231007
properties: {
10241008
mongosh_version: '1.0.0',
@@ -1033,7 +1017,6 @@ describe('MongoshLoggingAndTelemetry', function () {
10331017
'track',
10341018
{
10351019
anonymousId: '53defe995fa47e6c13102d9d',
1036-
deviceId: 'test-device',
10371020
event: 'API Call',
10381021
properties: {
10391022
mongosh_version: '1.0.0',
@@ -1188,7 +1171,6 @@ describe('MongoshLoggingAndTelemetry', function () {
11881171
'track',
11891172
{
11901173
anonymousId: undefined,
1191-
deviceId: 'test-device',
11921174
event: 'New Connection',
11931175
properties: {
11941176
mongosh_version: '1.0.0',

0 commit comments

Comments
 (0)