Skip to content

chore(logging): move device_id into Segment identity traits #2450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions packages/logging/src/analytics-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ describe('analytics helpers', function () {

toggleable.identify({
userId: 'me',
traits: { platform: '1234', session_id: 'abc' },
traits: {
platform: '1234',
session_id: 'abc',
device_id: 'test-device-id',
},
timestamp,
});
toggleable.track({
Expand Down Expand Up @@ -80,7 +84,11 @@ describe('analytics helpers', function () {
'identify',
{
userId: 'me',
traits: { platform: '1234', session_id: 'abc' },
traits: {
platform: '1234',
session_id: 'abc',
device_id: 'test-device-id',
},
timestamp,
},
],
Expand Down Expand Up @@ -124,7 +132,14 @@ describe('analytics helpers', function () {
describe('ThrottledAnalytics', function () {
const metadataPath = os.tmpdir();
const userId = 'u-' + Date.now();
const iEvt = { userId, traits: { platform: 'what', session_id: 'abc' } };
const iEvt = {
userId,
traits: {
platform: 'what',
session_id: 'abc',
device_id: 'test-device-id',
},
};
const tEvt = {
userId,
event: 'hi',
Expand Down Expand Up @@ -252,7 +267,14 @@ describe('analytics helpers', function () {

describe('SampledAnalytics', function () {
const userId = `u-${Date.now()}`;
const iEvt = { userId, traits: { platform: 'what', session_id: 'abc' } };
const iEvt = {
userId,
traits: {
platform: 'what',
session_id: 'abc',
device_id: 'test-device-id',
},
};
const tEvt = {
userId,
event: 'hi',
Expand Down
27 changes: 14 additions & 13 deletions packages/logging/src/analytics-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import fs from 'fs';
import path from 'path';
import type { IdentifyParams as SegmentIdentity } from '@segment/analytics-node';

export type MongoshAnalyticsIdentity =
| {
deviceId?: string;
userId: string;
anonymousId?: never;
}
| {
deviceId?: string;
userId?: never;
anonymousId: string;
};
export type MongoshAnalyticsIdentity = Omit<
SegmentIdentity,
'context' | 'traits' | 'timestamp'
>;

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

Expand Down Expand Up @@ -275,7 +273,10 @@ export class ThrottledAnalytics implements MongoshAnalytics {
if (this.currentUserId) {
throw new Error('Identify can only be called once per user session');
}
this.currentUserId = message.userId ?? message.anonymousId;

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.currentUserId = message.userId ?? message.anonymousId!;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately because of the more derivative type I think TypeScript can no longer correctly resolve this scenario


this.restorePromise = this.restoreThrottleState().then((enabled) => {
if (!enabled) {
this.trackQueue.disable();
Expand Down
40 changes: 11 additions & 29 deletions packages/logging/src/logging-and-telemetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('MongoshLoggingAndTelemetry', function () {

const userId = '53defe995fa47e6c13102d9d';
const logId = '5fb3c20ee1507e894e5340f3';
const deviceId = 'test-device';

let logger: MongoLogWriter;

Expand All @@ -39,7 +40,6 @@ describe('MongoshLoggingAndTelemetry', function () {
const testLoggingArguments: Omit<MongoshLoggingAndTelemetryArguments, 'bus'> =
{
analytics,
deviceId: 'test-device',
userTraits: {
platform: process.platform,
arch: process.arch,
Expand All @@ -54,6 +54,7 @@ describe('MongoshLoggingAndTelemetry', function () {

loggingAndTelemetry = setupLoggingAndTelemetry({
...testLoggingArguments,
deviceId,
bus,
});

Expand Down Expand Up @@ -117,8 +118,8 @@ describe('MongoshLoggingAndTelemetry', function () {
'identify',
{
anonymousId: userId,
deviceId: 'test-device',
traits: {
device_id: deviceId,
arch: process.arch,
platform: process.platform,
session_id: logId,
Expand All @@ -129,7 +130,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: userId,
deviceId: 'test-device',
event: 'New Connection',
properties: {
mongosh_version: '1.0.0',
Expand Down Expand Up @@ -178,8 +178,8 @@ describe('MongoshLoggingAndTelemetry', function () {
'identify',
{
anonymousId: userId,
deviceId: 'test-device',
traits: {
device_id: deviceId,
arch: process.arch,
platform: process.platform,
session_id: logId,
Expand All @@ -190,7 +190,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: userId,
deviceId: 'test-device',
event: 'New Connection',
properties: {
mongosh_version: '1.0.0',
Expand Down Expand Up @@ -235,9 +234,9 @@ describe('MongoshLoggingAndTelemetry', function () {
[
'identify',
{
deviceId: 'unknown',
anonymousId: userId,
traits: {
device_id: 'unknown',
platform: process.platform,
arch: process.arch,
session_id: logId,
Expand Down Expand Up @@ -272,9 +271,9 @@ describe('MongoshLoggingAndTelemetry', function () {
[
'identify',
{
deviceId,
anonymousId: userId,
traits: {
device_id: deviceId,
platform: process.platform,
arch: process.arch,
session_id: logId,
Expand All @@ -299,7 +298,7 @@ describe('MongoshLoggingAndTelemetry', function () {
...testLoggingArguments,
bus,
deviceId: undefined,
});
}) as LoggingAndTelemetry;

loggingAndTelemetry.attachLogger(logger);

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

resolveTelemetry('1234');
await (loggingAndTelemetry as LoggingAndTelemetry).setupTelemetryPromise;
await loggingAndTelemetry.setupTelemetryPromise;

expect(logOutput).to.have.lengthOf(1);
expect(analyticsOutput).to.have.lengthOf(1);

// Hash created from machine ID 1234
expect(analyticsOutput[0][1].deviceId).equals(
expect(loggingAndTelemetry['deviceId']).equals(
'8c9f929608f0ef13bfd5a290e0233f283e2cc25ffefc2ad8d9ef0650eb224a52'
);
});
Expand Down Expand Up @@ -719,8 +718,8 @@ describe('MongoshLoggingAndTelemetry', function () {
'identify',
{
anonymousId: userId,
deviceId: 'test-device',
traits: {
device_id: deviceId,
platform: process.platform,
arch: process.arch,
session_id: logId,
Expand All @@ -731,8 +730,8 @@ describe('MongoshLoggingAndTelemetry', function () {
'identify',
{
anonymousId: userId,
deviceId: 'test-device',
traits: {
device_id: deviceId,
platform: process.platform,
arch: process.arch,
session_id: logId,
Expand All @@ -743,7 +742,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: userId,
deviceId: 'test-device',
event: 'Startup Time',
properties: {
is_interactive: true,
Expand All @@ -759,7 +757,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
event: 'Error',
properties: {
mongosh_version: '1.0.0',
Expand All @@ -775,7 +772,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
event: 'Error',
properties: {
mongosh_version: '1.0.0',
Expand All @@ -791,7 +787,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
event: 'Use',
properties: {
mongosh_version: '1.0.0',
Expand All @@ -803,7 +798,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
event: 'Show',
properties: {
mongosh_version: '1.0.0',
Expand All @@ -823,7 +817,6 @@ describe('MongoshLoggingAndTelemetry', function () {
shell: true,
},
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
},
],
[
Expand All @@ -836,7 +829,6 @@ describe('MongoshLoggingAndTelemetry', function () {
nested: false,
},
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
},
],
[
Expand All @@ -848,7 +840,6 @@ describe('MongoshLoggingAndTelemetry', function () {
session_id: logId,
},
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
},
],
[
Expand All @@ -860,7 +851,6 @@ describe('MongoshLoggingAndTelemetry', function () {
session_id: logId,
},
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
},
],
[
Expand All @@ -873,14 +863,12 @@ describe('MongoshLoggingAndTelemetry', function () {
shell: true,
},
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
},
],
[
'track',
{
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
event: 'Snippet Install',
properties: {
mongosh_version: '1.0.0',
Expand Down Expand Up @@ -976,7 +964,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
event: 'Deprecated Method',
properties: {
mongosh_version: '1.0.0',
Expand All @@ -990,7 +977,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
event: 'Deprecated Method',
properties: {
mongosh_version: '1.0.0',
Expand All @@ -1004,7 +990,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
event: 'Deprecated Method',
properties: {
mongosh_version: '1.0.0',
Expand All @@ -1018,7 +1003,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
event: 'API Call',
properties: {
mongosh_version: '1.0.0',
Expand All @@ -1033,7 +1017,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: '53defe995fa47e6c13102d9d',
deviceId: 'test-device',
event: 'API Call',
properties: {
mongosh_version: '1.0.0',
Expand Down Expand Up @@ -1188,7 +1171,6 @@ describe('MongoshLoggingAndTelemetry', function () {
'track',
{
anonymousId: undefined,
deviceId: 'test-device',
event: 'New Connection',
properties: {
mongosh_version: '1.0.0',
Expand Down
2 changes: 1 addition & 1 deletion packages/logging/src/logging-and-telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {

const getUserTraits = (): AnalyticsIdentifyMessage['traits'] => ({
...this.userTraits,
device_id: this.deviceId ?? 'unknown',
session_id: this.log.logId,
});

Expand All @@ -277,7 +278,6 @@ export class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {

const getTelemetryUserIdentity = (): MongoshAnalyticsIdentity => {
return {
deviceId: this.deviceId,
anonymousId:
this.busEventState.telemetryAnonymousId ??
(this.busEventState.userId as string),
Expand Down
Loading