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

feat: report hostname for atlas COMPASS-8093 #2113

Merged
merged 22 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
53162e6
feat: report hostname for atlas COMPASS-8093
alenakhineika Jul 31, 2024
dcffd84
Merge remote-tracking branch 'origin/main' into COMPASS-8093-report-h…
alenakhineika Jul 31, 2024
9104962
fix: do not call on method in worker-thread environment
alenakhineika Jul 31, 2024
585a02e
refactor: dynamically import mongodb-cloud-info
alenakhineika Jul 31, 2024
18d288c
Merge branch 'main' into COMPASS-8093-report-hostname-for-atlas
alenakhineika Aug 1, 2024
6a90556
refactor: read resolved srv from topology
alenakhineika Aug 2, 2024
b18bb84
fix: add getTopology to service provider default functions
alenakhineika Aug 2, 2024
d4c7906
refactor: make s optional
alenakhineika Aug 2, 2024
82994dd
refactor: use uri if cannot resolve host
alenakhineika Aug 2, 2024
3b7f680
refactor: move mongodb-cloud-info
alenakhineika Aug 2, 2024
28e3e91
Merge remote-tracking branch 'origin/main' into COMPASS-8093-report-h…
alenakhineika Aug 9, 2024
86bbdb4
feat: handle IPv6 hostnames
alenakhineika Aug 9, 2024
dda499e
refactor: import mongodb-cloud-info at startup
alenakhineika Aug 9, 2024
e9e6e29
Merge remote-tracking branch 'origin/main' into COMPASS-8093-report-h…
alenakhineika Aug 9, 2024
5395570
refactor: remove mongodb-cloud-info
alenakhineika Aug 12, 2024
3cff763
Merge remote-tracking branch 'origin/main' into COMPASS-8093-report-h…
alenakhineika Aug 12, 2024
a425e88
refactor: listen on topologyDescriptionChanged
alenakhineika Aug 13, 2024
2af513f
Merge remote-tracking branch 'origin/main' into COMPASS-8093-report-h…
alenakhineika Aug 13, 2024
e3b6c79
refactor: move getHostnameForConnection to service-provider-server
alenakhineika Aug 13, 2024
b0f86fd
refactor: clean up
alenakhineika Aug 13, 2024
193de2e
fix: connectionInfo can be undefined
alenakhineika Aug 13, 2024
ca3dd1e
chore: add dependency
alenakhineika Aug 13, 2024
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
12 changes: 6 additions & 6 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/cli-repl/src/mongosh-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,7 @@ describe('MongoshNodeRepl', function () {
'getConnectionInfo',
'getFleOptions',
'getRawClient',
'getTopology',
'getURI',
'runCommandWithCheck',
]);
Expand Down
137 changes: 110 additions & 27 deletions packages/logging/src/setup-logger-and-telemetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { expect } from 'chai';
import { MongoLogWriter } from 'mongodb-log-writer';
import { setupLoggerAndTelemetry } from './';
import { EventEmitter } from 'events';
import { promisify } from 'util';
alenakhineika marked this conversation as resolved.
Show resolved Hide resolved
import { MongoshInvalidInputError } from '@mongosh/errors';
import type { MongoshBus } from '@mongosh/types';
import { toSnakeCase } from './setup-logger-and-telemetry';

const wait = promisify(setTimeout);

alenakhineika marked this conversation as resolved.
Show resolved Hide resolved
describe('toSnakeCase', function () {
const useCases = [
{ input: 'MongoDB REPL', output: 'mongo_db_repl' },
Expand Down Expand Up @@ -64,7 +67,7 @@ describe('setupLoggerAndTelemetry', function () {
bus = new EventEmitter();
});

it('works', function () {
it('tracks new local connection events', async function () {
setupLoggerAndTelemetry(
bus,
logger,
Expand All @@ -78,14 +81,116 @@ 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', {
mongosh_version: '1.0.0',
uri: 'mongodb://localhost/',
is_localhost: true,
is_atlas: false,
resolved_hostname: 'localhost',
node_version: 'v12.19.0',
});

// Make sure cloud info is resolved.
await wait(500);

alenakhineika marked this conversation as resolved.
Show resolved Hide resolved
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', async 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', {
mongosh_version: '1.0.0',
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);
});

// Make sure cloud info is resolved.
await wait(500);

alenakhineika marked this conversation as resolved.
Show resolved Hide resolved
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 +335,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 +498,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
52 changes: 29 additions & 23 deletions packages/logging/src/setup-logger-and-telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,32 +141,38 @@ 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,
};
log.info(
'MONGOSH',
mongoLogId(1_000_000_004),
'connect',
'Connecting to server',
params
);

analytics.track({
...getTelemetryUserIdentity(),
event: 'New Connection',
properties: {
(async () => {
alenakhineika marked this conversation as resolved.
Show resolved Hide resolved
const properties = {
...trackProperties,
...argsWithoutUri,
},
});
...argsWithoutUriAndHostname,
...atlasHostname,
};

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

analytics.track({
...getTelemetryUserIdentity(),
event: 'New Connection',
properties,
});
})().catch(() => undefined);
});

bus.on('mongosh:start-session', function (args: SessionStartedEvent) {
Expand Down
Loading
Loading