Skip to content

Commit

Permalink
feat: report hostname for atlas COMPASS-8093 (#2113)
Browse files Browse the repository at this point in the history
* feat: report hostname for atlas COMPASS-8093

* fix: do not call on method in worker-thread environment

* refactor: dynamically import mongodb-cloud-info

* refactor: read resolved srv from topology

* fix: add getTopology to service provider default functions

* refactor: make s optional

* refactor: use uri if cannot resolve host

* refactor: move mongodb-cloud-info

* feat: handle IPv6 hostnames

* refactor: import mongodb-cloud-info at startup

* refactor: remove mongodb-cloud-info

* refactor: listen on topologyDescriptionChanged

* refactor: move getHostnameForConnection to service-provider-server

* refactor: clean up

* fix: connectionInfo can be undefined

* chore: add dependency
  • Loading branch information
alenakhineika authored Aug 14, 2024
1 parent 04c8a49 commit 98a0619
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 175 deletions.
16 changes: 9 additions & 7 deletions package-lock.json

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

1 change: 0 additions & 1 deletion packages/browser-repl/src/sandbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class DemoServiceProvider {
extraInfo: {
uri: 'mongodb://localhost/',
},
topology: null,
};
}

Expand Down
4 changes: 0 additions & 4 deletions packages/cli-repl/src/mongosh-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ describe('MongoshNodeRepl', function () {
buildInfo: {
version: '4.4.1',
},
topology: null,
});
sp.runCommandWithCheck.resolves({ ok: 1 });
serviceProvider = sp;
Expand Down Expand Up @@ -1259,7 +1258,6 @@ describe('MongoshNodeRepl', function () {
version: '4.4.1',
modules: ['enterprise'],
},
topology: null,
});

const initialized = await mongoshRepl.initialize(serviceProvider);
Expand Down Expand Up @@ -1291,7 +1289,6 @@ describe('MongoshNodeRepl', function () {
version: '4.4.1',
modules: ['enterprise'],
},
topology: null,
};

sp.getConnectionInfo.resolves(connectionInfo);
Expand Down Expand Up @@ -1421,7 +1418,6 @@ describe('MongoshNodeRepl', function () {
buildInfo: {
version: '4.4.1',
},
topology: null,
});
mongoshReplOptions.shellCliOptions = {
nodb: false,
Expand Down
126 changes: 99 additions & 27 deletions packages/logging/src/setup-logger-and-telemetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('setupLoggerAndTelemetry', function () {
bus = new EventEmitter();
});

it('works', function () {
it('tracks new local connection events', function () {
setupLoggerAndTelemetry(
bus,
logger,
Expand All @@ -78,14 +78,108 @@ describe('setupLoggerAndTelemetry', function () {
expect(logOutput).to.have.lengthOf(0);
expect(analyticsOutput).to.be.empty;

bus.emit('mongosh:new-user', { userId, anonymousId: userId });
bus.emit('mongosh:update-user', { userId, anonymousId: userId });
bus.emit('mongosh:connect', {
uri: 'mongodb://localhost/',
is_localhost: true,
is_atlas: false,
resolved_hostname: 'localhost',
node_version: 'v12.19.0',
});

expect(logOutput[0].msg).to.equal('Connecting to server');
expect(logOutput[0].attr.connectionUri).to.equal('mongodb://localhost/');
expect(logOutput[0].attr.is_localhost).to.equal(true);
expect(logOutput[0].attr.is_atlas).to.equal(false);
expect(logOutput[0].attr.atlas_hostname).to.equal(null);
expect(logOutput[0].attr.node_version).to.equal('v12.19.0');

expect(analyticsOutput).to.deep.equal([
[
'track',
{
anonymousId: undefined,
event: 'New Connection',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
is_localhost: true,
is_atlas: false,
atlas_hostname: null,
node_version: 'v12.19.0',
},
},
],
]);
});

it('tracks new atlas connection events', function () {
setupLoggerAndTelemetry(
bus,
logger,
analytics,
{
platform: process.platform,
arch: process.arch,
},
'1.0.0'
);
expect(logOutput).to.have.lengthOf(0);
expect(analyticsOutput).to.be.empty;

bus.emit('mongosh:connect', {
uri: 'mongodb://test-data-sets-a011bb.mongodb.net/',
is_localhost: false,
is_atlas: true,
resolved_hostname: 'test-data-sets-00-02-a011bb.mongodb.net',
node_version: 'v12.19.0',
} as any);
});

expect(logOutput[0].msg).to.equal('Connecting to server');
expect(logOutput[0].attr.connectionUri).to.equal(
'mongodb://test-data-sets-a011bb.mongodb.net/'
);
expect(logOutput[0].attr.is_localhost).to.equal(false);
expect(logOutput[0].attr.is_atlas).to.equal(true);
expect(logOutput[0].attr.atlas_hostname).to.equal(
'test-data-sets-00-02-a011bb.mongodb.net'
);
expect(logOutput[0].attr.node_version).to.equal('v12.19.0');

expect(analyticsOutput).to.deep.equal([
[
'track',
{
anonymousId: undefined,
event: 'New Connection',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
is_localhost: false,
is_atlas: true,
atlas_hostname: 'test-data-sets-00-02-a011bb.mongodb.net',
node_version: 'v12.19.0',
},
},
],
]);
});

it('tracks a sequence of events', function () {
setupLoggerAndTelemetry(
bus,
logger,
analytics,
{
platform: process.platform,
arch: process.arch,
},
'1.0.0'
);
expect(logOutput).to.have.lengthOf(0);
expect(analyticsOutput).to.be.empty;

bus.emit('mongosh:new-user', { userId, anonymousId: userId });
bus.emit('mongosh:update-user', { userId, anonymousId: userId });
bus.emit('mongosh:start-session', {
isInteractive: true,
jsContext: 'foo',
Expand Down Expand Up @@ -230,16 +324,8 @@ describe('setupLoggerAndTelemetry', function () {
});

let i = 0;

expect(logOutput[i++].msg).to.equal('User updated');
expect(logOutput[i].msg).to.equal('Connecting to server');
expect(logOutput[i].attr.session_id).to.equal('5fb3c20ee1507e894e5340f3');
expect(logOutput[i].attr.telemetryAnonymousId).to.equal(
'53defe995fa47e6c13102d9d'
);
expect(logOutput[i].attr.connectionUri).to.equal('mongodb://localhost/');
expect(logOutput[i].attr.is_localhost).to.equal(true);
expect(logOutput[i].attr.is_atlas).to.equal(false);
expect(logOutput[i++].attr.node_version).to.equal('v12.19.0');
expect(logOutput[i].s).to.equal('E');
expect(logOutput[i++].attr.message).to.match(/meow/);
expect(logOutput[i].s).to.equal('F');
Expand Down Expand Up @@ -401,20 +487,6 @@ describe('setupLoggerAndTelemetry', function () {
},
},
],
[
'track',
{
anonymousId: '53defe995fa47e6c13102d9d',
event: 'New Connection',
properties: {
mongosh_version: '1.0.0',
session_id: '5fb3c20ee1507e894e5340f3',
is_localhost: true,
is_atlas: false,
node_version: 'v12.19.0',
},
},
],
[
'track',
{
Expand Down
31 changes: 17 additions & 14 deletions packages/logging/src/setup-logger-and-telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,34 @@ export function setupLoggerAndTelemetry(
);

bus.on('mongosh:connect', function (args: ConnectEvent) {
const connectionUri = args.uri && redactURICredentials(args.uri);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { uri: _uri, ...argsWithoutUri } = args;
const params = {
session_id,
userId,
telemetryAnonymousId,
connectionUri,
...argsWithoutUri,
const { uri, resolved_hostname, ...argsWithoutUriAndHostname } = args;
const connectionUri = uri && redactURICredentials(uri);
const atlasHostname = {
atlas_hostname: args.is_atlas ? resolved_hostname : null,
};
const properties = {
...trackProperties,
...argsWithoutUriAndHostname,
...atlasHostname,
};

log.info(
'MONGOSH',
mongoLogId(1_000_000_004),
'connect',
'Connecting to server',
params
{
userId,
telemetryAnonymousId,
connectionUri,
...properties,
}
);

analytics.track({
...getTelemetryUserIdentity(),
event: 'New Connection',
properties: {
...trackProperties,
...argsWithoutUri,
},
properties,
});
});

Expand Down
3 changes: 2 additions & 1 deletion packages/service-provider-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"@mongosh/errors": "0.0.0-dev.0",
"bson": "^6.7.0",
"mongodb": "^6.8.0",
"mongodb-build-info": "^1.7.2"
"mongodb-build-info": "^1.7.2",
"mongodb-connection-string-url": "^3.0.1"
},
"optionalDependencies": {
"mongodb-client-encryption": "^6.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/service-provider-core/src/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface CheckMetadataConsistencyOptions {

export interface ConnectionInfo {
buildInfo: Document | null;
topology: any | null;
resolvedHostname?: string;
extraInfo: (ConnectionExtraInfo & { fcv?: string }) | null;
}

Expand Down
Loading

0 comments on commit 98a0619

Please sign in to comment.