Skip to content
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

fix: use user id and anonymous id as segment identity MONGOSH-1143 #1225

Merged
merged 5 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 8 additions & 6 deletions packages/cli-repl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,25 @@ bus.emit('mongosh:connect', {
})
```

### bus.on('mongosh:new-user', userID, enableTelemetry)
Where `userID` is a [BSON ObjectID][object-id] and `enableTelemetry` is a boolean flag.
### bus.on('mongosh:new-user', telemetryAnonymousId, enableTelemetry)
Where `telemetryAnonymousId` is a [BSON ObjectID][object-id] and `enableTelemetry` is a boolean flag.
This is used for telemetry tracking when the user initially uses mongosh.

Example:
```js
bus.emit('mongosh:new-user', '12394dfjvnaw3uw3erdf', true)
```

### bus.on('mongosh:update-user', id, enableTelemetry)
Where `userID` is a [BSON ObjectID][object-id] and `enableTelemetry` is a boolean flag.
This is used internally to update telemetry preferences and userID in the
### bus.on('mongosh:update-user', telemetryUserIdentity, enableTelemetry)
Initially, we used `userId` as Segment user identifier, but this usage is being deprecated.
The `anonymousId` should be used instead. We keep sending `userId` to Segment for old users though to preserve their analytics.
Where `userID`/`anonymousId` is a [BSON ObjectID][object-id] and `enableTelemetry` is a boolean flag.
This is used internally to update telemetry preferences and `userID`/`anonymousId` in the
logger.

Example:
```js
bus.emit('mongosh:update-user', '12394dfjvnaw3uw3erdf', false)
bus.emit('mongosh:update-user', { userId: undefined, anonymousId: '12394dfjvnaw3uw3erdf' } , false)
```

### bus.on('mongosh:error', error)
Expand Down
30 changes: 15 additions & 15 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ describe('CliRepl', () => {
const updateUser = waitBus(cliRepl.bus, 'mongosh:update-user');
const evalComplete = waitBus(cliRepl.bus, 'mongosh:eval-complete');
input.write('disableTelemetry()\n');
const [ userId ] = await updateUser;
expect(typeof userId).to.equal('string');
const [ telemetryUserIdentity ] = await updateUser;
expect(typeof telemetryUserIdentity).to.equal('object');

await evalComplete; // eval-complete includes the fs.writeFile() call.
const content = await fs.readFile(path.join(tmpdir.path, 'config'), { encoding: 'utf8' });
Expand All @@ -113,23 +113,23 @@ describe('CliRepl', () => {
it('does not store config options on disk that have not been changed', async() => {
let content = await fs.readFile(path.join(tmpdir.path, 'config'), { encoding: 'utf8' });
expect(Object.keys(EJSON.parse(content))).to.deep.equal([
'userId', 'enableTelemetry', 'disableGreetingMessage'
'telemetryAnonymousId', 'enableTelemetry', 'disableGreetingMessage'
]);

input.write('config.set("inspectDepth", config.get("inspectDepth"))\n');

await waitEval(cliRepl.bus);
content = await fs.readFile(path.join(tmpdir.path, 'config'), { encoding: 'utf8' });
expect(Object.keys(EJSON.parse(content))).to.deep.equal([
'userId', 'enableTelemetry', 'disableGreetingMessage', 'inspectDepth'
'telemetryAnonymousId', 'enableTelemetry', 'disableGreetingMessage', 'inspectDepth'
]);

// When a new REPL is created:
cliRepl = new CliRepl(cliReplOptions);
await cliRepl.start('', {});
content = await fs.readFile(path.join(tmpdir.path, 'config'), { encoding: 'utf8' });
expect(Object.keys(EJSON.parse(content))).to.deep.equal([
'userId', 'enableTelemetry', 'disableGreetingMessage', 'inspectDepth'
'telemetryAnonymousId', 'enableTelemetry', 'disableGreetingMessage', 'inspectDepth'
]);
});

Expand Down Expand Up @@ -225,12 +225,12 @@ describe('CliRepl', () => {

it('fails when trying to overwrite mongosh-owned config settings', async() => {
output = '';
input.write('config.set("userId", "foo")\n');
input.write('config.set("telemetryAnonymousId", "foo")\n');
await waitEval(cliRepl.bus);
expect(output).to.include('Option "userId" is not available in this environment');
expect(output).to.include('Option "telemetryAnonymousId" is not available in this environment');

output = '';
input.write('config.get("userId")\n');
input.write('config.get("telemetryAnonymousId")\n');
await waitEval(cliRepl.bus);
expect(output).to.match(/^[a-z0-9]{24}\n> $/);
});
Expand Down Expand Up @@ -286,16 +286,16 @@ describe('CliRepl', () => {
});

context('during startup', () => {
it('persists userId', async() => {
const userIds: string[] = [];
it('persists telemetryAnonymousId', async() => {
const telemetryAnonymousIds: string[] = [];
for (let i = 0; i < 2; i++) {
cliRepl = new CliRepl(cliReplOptions);
cliRepl.bus.on('mongosh:new-user', userId => userIds.push(userId));
cliRepl.bus.on('mongosh:update-user', userId => userIds.push(userId));
cliRepl.bus.on('mongosh:new-user', telemetryAnonymousId => telemetryAnonymousIds.push(telemetryAnonymousId));
cliRepl.bus.on('mongosh:update-user', telemetryUserIdentity => telemetryAnonymousIds.push(telemetryUserIdentity.anonymousId));
await cliRepl.start('', {});
}
expect(userIds).to.have.lengthOf(2);
expect([...new Set(userIds)]).to.have.lengthOf(1);
expect(telemetryAnonymousIds).to.have.lengthOf(2);
expect([...new Set(telemetryAnonymousIds)]).to.have.lengthOf(1);
});

it('emits error for invalid config', async() => {
Expand Down Expand Up @@ -985,7 +985,7 @@ describe('CliRepl', () => {
const connectEvents = requests.flatMap(
req => JSON.parse(req.body).batch.filter(entry => entry.event === 'New Connection'));
expect(connectEvents).to.have.lengthOf(1);
expect(connectEvents[0].userId).to.be.a('string');
expect(connectEvents[0].anonymousId).to.be.a('string');
const { properties } = connectEvents[0];
expect(properties.mongosh_version).to.be.a('string');
expect(properties.session_id).to.be.a('string');
Expand Down
12 changes: 6 additions & 6 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type CliReplOptions = {
} & Pick<MongoshNodeReplOptions, 'nodeReplOptions'>;

/** The set of config options that is *always* available in config files stored on the file system. */
type CliUserConfigOnDisk = Partial<CliUserConfig> & Pick<CliUserConfig, 'enableTelemetry' | 'userId'>;
type CliUserConfigOnDisk = Partial<CliUserConfig> & Pick<CliUserConfig, 'enableTelemetry' | 'telemetryAnonymousId'>;

/**
* The REPL used from the terminal.
Expand Down Expand Up @@ -105,7 +105,7 @@ class CliRepl implements MongoshIOProvider {
this.analyticsOptions = options.analyticsOptions;
this.onExit = options.onExit;
this.config = {
userId: new bson.ObjectId().toString(),
telemetryAnonymousId: new bson.ObjectId().toString(),
enableTelemetry: true
};

Expand All @@ -118,11 +118,11 @@ class CliRepl implements MongoshIOProvider {
})
.on('new-config', (config: CliUserConfigOnDisk) => {
this.setTelemetryEnabled(config.enableTelemetry);
this.bus.emit('mongosh:new-user', config.userId);
this.bus.emit('mongosh:new-user', config.telemetryAnonymousId);
})
.on('update-config', (config: CliUserConfigOnDisk) => {
this.setTelemetryEnabled(config.enableTelemetry);
this.bus.emit('mongosh:update-user', config.userId);
this.bus.emit('mongosh:update-user', { userId: config.userId, anonymousId: config.telemetryAnonymousId ?? config.userId });
});

this.mongocryptdManager = new MongocryptdManager(
Expand Down Expand Up @@ -485,7 +485,7 @@ class CliRepl implements MongoshIOProvider {
this.config[key] = value;
if (key === 'enableTelemetry') {
this.setTelemetryEnabled(this.config.enableTelemetry);
this.bus.emit('mongosh:update-user', this.config.userId);
this.bus.emit('mongosh:update-user', { userId: this.config.userId, anonymousId: this.config.telemetryAnonymousId });
}
try {
await this.configDirectory.writeConfigFile(this.config);
Expand All @@ -499,7 +499,7 @@ class CliRepl implements MongoshIOProvider {
* Implements listConfigOptions from the {@link ConfigProvider} interface.
*/
listConfigOptions(): string[] {
const hiddenKeys = ['userId', 'disableGreetingMessage', 'forceDisableTelemetry'];
const hiddenKeys = ['userId', 'telemetryAnonymousId', 'disableGreetingMessage', 'forceDisableTelemetry'];
const keys = Object.keys(new CliUserConfig());
return keys.filter(key => !hiddenKeys.includes(key));
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-repl/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ describe('e2e', function() {
describe('config file', () => {
it('sets up a config file', async() => {
const config = await readConfig();
expect(config.userId).to.match(/^[a-f0-9]{24}$/);
expect(config.telemetryAnonymousId).to.match(/^[a-f0-9]{24}$/);
expect(config.enableTelemetry).to.be.true;
expect(config.disableGreetingMessage).to.be.true;
});
Expand Down
6 changes: 3 additions & 3 deletions packages/java-shell/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions packages/logging/src/analytics-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ describe('ToggleableAnalytics', () => {
const toggleable = new ToggleableAnalytics(target);
expect(events).to.have.lengthOf(0);

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

toggleable.enable();
expect(events).to.have.lengthOf(2);

toggleable.track({ userId: 'me', event: 'something2', properties: { mongosh_version: '1.2.3' } });
toggleable.track({ anonymousId: 'me', event: 'something2', properties: { mongosh_version: '1.2.3' } });
expect(events).to.have.lengthOf(3);

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

toggleable.disable();
expect(events).to.have.lengthOf(3);
toggleable.enable();

expect(events).to.deep.equal([
[ 'identify', { userId: 'me', traits: { platform: '1234' } } ],
[ 'track', { userId: 'me', event: 'something', properties: { mongosh_version: '1.2.3' } } ],
[ 'track', { userId: 'me', event: 'something2', properties: { mongosh_version: '1.2.3' } } ]
[ 'identify', { anonymousId: 'me', traits: { platform: '1234' } } ],
[ 'track', { anonymousId: 'me', event: 'something', properties: { mongosh_version: '1.2.3' } } ],
[ 'track', { anonymousId: 'me', event: 'something2', properties: { mongosh_version: '1.2.3' } } ]
]);
});
});
6 changes: 4 additions & 2 deletions packages/logging/src/analytics-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
*/
export interface MongoshAnalytics {
identify(message: {
userId: string,
userId?: string,
anonymousId: string,
traits: { platform: string }
}): void;

track(message: {
userId: string,
userId?: string,
anonymousId: string,
event: string,
properties: {
// eslint-disable-next-line camelcase
Expand Down
Loading