Skip to content

Commit 98a0619

Browse files
feat: report hostname for atlas COMPASS-8093 (#2113)
* 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
1 parent 04c8a49 commit 98a0619

17 files changed

+330
-175
lines changed

package-lock.json

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/browser-repl/src/sandbox.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ class DemoServiceProvider {
103103
extraInfo: {
104104
uri: 'mongodb://localhost/',
105105
},
106-
topology: null,
107106
};
108107
}
109108

packages/cli-repl/src/mongosh-repl.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ describe('MongoshNodeRepl', function () {
8686
buildInfo: {
8787
version: '4.4.1',
8888
},
89-
topology: null,
9089
});
9190
sp.runCommandWithCheck.resolves({ ok: 1 });
9291
serviceProvider = sp;
@@ -1259,7 +1258,6 @@ describe('MongoshNodeRepl', function () {
12591258
version: '4.4.1',
12601259
modules: ['enterprise'],
12611260
},
1262-
topology: null,
12631261
});
12641262

12651263
const initialized = await mongoshRepl.initialize(serviceProvider);
@@ -1291,7 +1289,6 @@ describe('MongoshNodeRepl', function () {
12911289
version: '4.4.1',
12921290
modules: ['enterprise'],
12931291
},
1294-
topology: null,
12951292
};
12961293

12971294
sp.getConnectionInfo.resolves(connectionInfo);
@@ -1421,7 +1418,6 @@ describe('MongoshNodeRepl', function () {
14211418
buildInfo: {
14221419
version: '4.4.1',
14231420
},
1424-
topology: null,
14251421
});
14261422
mongoshReplOptions.shellCliOptions = {
14271423
nodb: false,

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

Lines changed: 99 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('setupLoggerAndTelemetry', function () {
6464
bus = new EventEmitter();
6565
});
6666

67-
it('works', function () {
67+
it('tracks new local connection events', function () {
6868
setupLoggerAndTelemetry(
6969
bus,
7070
logger,
@@ -78,14 +78,108 @@ describe('setupLoggerAndTelemetry', function () {
7878
expect(logOutput).to.have.lengthOf(0);
7979
expect(analyticsOutput).to.be.empty;
8080

81-
bus.emit('mongosh:new-user', { userId, anonymousId: userId });
82-
bus.emit('mongosh:update-user', { userId, anonymousId: userId });
8381
bus.emit('mongosh:connect', {
8482
uri: 'mongodb://localhost/',
8583
is_localhost: true,
8684
is_atlas: false,
85+
resolved_hostname: 'localhost',
86+
node_version: 'v12.19.0',
87+
});
88+
89+
expect(logOutput[0].msg).to.equal('Connecting to server');
90+
expect(logOutput[0].attr.connectionUri).to.equal('mongodb://localhost/');
91+
expect(logOutput[0].attr.is_localhost).to.equal(true);
92+
expect(logOutput[0].attr.is_atlas).to.equal(false);
93+
expect(logOutput[0].attr.atlas_hostname).to.equal(null);
94+
expect(logOutput[0].attr.node_version).to.equal('v12.19.0');
95+
96+
expect(analyticsOutput).to.deep.equal([
97+
[
98+
'track',
99+
{
100+
anonymousId: undefined,
101+
event: 'New Connection',
102+
properties: {
103+
mongosh_version: '1.0.0',
104+
session_id: '5fb3c20ee1507e894e5340f3',
105+
is_localhost: true,
106+
is_atlas: false,
107+
atlas_hostname: null,
108+
node_version: 'v12.19.0',
109+
},
110+
},
111+
],
112+
]);
113+
});
114+
115+
it('tracks new atlas connection events', function () {
116+
setupLoggerAndTelemetry(
117+
bus,
118+
logger,
119+
analytics,
120+
{
121+
platform: process.platform,
122+
arch: process.arch,
123+
},
124+
'1.0.0'
125+
);
126+
expect(logOutput).to.have.lengthOf(0);
127+
expect(analyticsOutput).to.be.empty;
128+
129+
bus.emit('mongosh:connect', {
130+
uri: 'mongodb://test-data-sets-a011bb.mongodb.net/',
131+
is_localhost: false,
132+
is_atlas: true,
133+
resolved_hostname: 'test-data-sets-00-02-a011bb.mongodb.net',
87134
node_version: 'v12.19.0',
88-
} as any);
135+
});
136+
137+
expect(logOutput[0].msg).to.equal('Connecting to server');
138+
expect(logOutput[0].attr.connectionUri).to.equal(
139+
'mongodb://test-data-sets-a011bb.mongodb.net/'
140+
);
141+
expect(logOutput[0].attr.is_localhost).to.equal(false);
142+
expect(logOutput[0].attr.is_atlas).to.equal(true);
143+
expect(logOutput[0].attr.atlas_hostname).to.equal(
144+
'test-data-sets-00-02-a011bb.mongodb.net'
145+
);
146+
expect(logOutput[0].attr.node_version).to.equal('v12.19.0');
147+
148+
expect(analyticsOutput).to.deep.equal([
149+
[
150+
'track',
151+
{
152+
anonymousId: undefined,
153+
event: 'New Connection',
154+
properties: {
155+
mongosh_version: '1.0.0',
156+
session_id: '5fb3c20ee1507e894e5340f3',
157+
is_localhost: false,
158+
is_atlas: true,
159+
atlas_hostname: 'test-data-sets-00-02-a011bb.mongodb.net',
160+
node_version: 'v12.19.0',
161+
},
162+
},
163+
],
164+
]);
165+
});
166+
167+
it('tracks a sequence of events', function () {
168+
setupLoggerAndTelemetry(
169+
bus,
170+
logger,
171+
analytics,
172+
{
173+
platform: process.platform,
174+
arch: process.arch,
175+
},
176+
'1.0.0'
177+
);
178+
expect(logOutput).to.have.lengthOf(0);
179+
expect(analyticsOutput).to.be.empty;
180+
181+
bus.emit('mongosh:new-user', { userId, anonymousId: userId });
182+
bus.emit('mongosh:update-user', { userId, anonymousId: userId });
89183
bus.emit('mongosh:start-session', {
90184
isInteractive: true,
91185
jsContext: 'foo',
@@ -230,16 +324,8 @@ describe('setupLoggerAndTelemetry', function () {
230324
});
231325

232326
let i = 0;
327+
233328
expect(logOutput[i++].msg).to.equal('User updated');
234-
expect(logOutput[i].msg).to.equal('Connecting to server');
235-
expect(logOutput[i].attr.session_id).to.equal('5fb3c20ee1507e894e5340f3');
236-
expect(logOutput[i].attr.telemetryAnonymousId).to.equal(
237-
'53defe995fa47e6c13102d9d'
238-
);
239-
expect(logOutput[i].attr.connectionUri).to.equal('mongodb://localhost/');
240-
expect(logOutput[i].attr.is_localhost).to.equal(true);
241-
expect(logOutput[i].attr.is_atlas).to.equal(false);
242-
expect(logOutput[i++].attr.node_version).to.equal('v12.19.0');
243329
expect(logOutput[i].s).to.equal('E');
244330
expect(logOutput[i++].attr.message).to.match(/meow/);
245331
expect(logOutput[i].s).to.equal('F');
@@ -401,20 +487,6 @@ describe('setupLoggerAndTelemetry', function () {
401487
},
402488
},
403489
],
404-
[
405-
'track',
406-
{
407-
anonymousId: '53defe995fa47e6c13102d9d',
408-
event: 'New Connection',
409-
properties: {
410-
mongosh_version: '1.0.0',
411-
session_id: '5fb3c20ee1507e894e5340f3',
412-
is_localhost: true,
413-
is_atlas: false,
414-
node_version: 'v12.19.0',
415-
},
416-
},
417-
],
418490
[
419491
'track',
420492
{

packages/logging/src/setup-logger-and-telemetry.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,31 +141,34 @@ export function setupLoggerAndTelemetry(
141141
);
142142

143143
bus.on('mongosh:connect', function (args: ConnectEvent) {
144-
const connectionUri = args.uri && redactURICredentials(args.uri);
145-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
146-
const { uri: _uri, ...argsWithoutUri } = args;
147-
const params = {
148-
session_id,
149-
userId,
150-
telemetryAnonymousId,
151-
connectionUri,
152-
...argsWithoutUri,
144+
const { uri, resolved_hostname, ...argsWithoutUriAndHostname } = args;
145+
const connectionUri = uri && redactURICredentials(uri);
146+
const atlasHostname = {
147+
atlas_hostname: args.is_atlas ? resolved_hostname : null,
153148
};
149+
const properties = {
150+
...trackProperties,
151+
...argsWithoutUriAndHostname,
152+
...atlasHostname,
153+
};
154+
154155
log.info(
155156
'MONGOSH',
156157
mongoLogId(1_000_000_004),
157158
'connect',
158159
'Connecting to server',
159-
params
160+
{
161+
userId,
162+
telemetryAnonymousId,
163+
connectionUri,
164+
...properties,
165+
}
160166
);
161167

162168
analytics.track({
163169
...getTelemetryUserIdentity(),
164170
event: 'New Connection',
165-
properties: {
166-
...trackProperties,
167-
...argsWithoutUri,
168-
},
171+
properties,
169172
});
170173
});
171174

packages/service-provider-core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"@mongosh/errors": "0.0.0-dev.0",
4848
"bson": "^6.7.0",
4949
"mongodb": "^6.8.0",
50-
"mongodb-build-info": "^1.7.2"
50+
"mongodb-build-info": "^1.7.2",
51+
"mongodb-connection-string-url": "^3.0.1"
5152
},
5253
"optionalDependencies": {
5354
"mongodb-client-encryption": "^6.0.0"

packages/service-provider-core/src/admin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface CheckMetadataConsistencyOptions {
4545

4646
export interface ConnectionInfo {
4747
buildInfo: Document | null;
48-
topology: any | null;
48+
resolvedHostname?: string;
4949
extraInfo: (ConnectionExtraInfo & { fcv?: string }) | null;
5050
}
5151

0 commit comments

Comments
 (0)