Skip to content
Merged
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
19 changes: 16 additions & 3 deletions packages/compass/src/app/utils/telemetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ describe('connection tracking', function () {
is_srv: false,
topology_type: 'Unknown',
is_atlas: false,
atlas_hostname: null,
is_local_atlas: false,
is_dataLake: false,
is_enterprise: false,
Expand Down Expand Up @@ -200,6 +201,7 @@ describe('connection tracking', function () {
is_srv: false,
topology_type: 'Unknown',
is_atlas: false,
atlas_hostname: null,
is_local_atlas: false,
is_dataLake: false,
is_enterprise: false,
Expand All @@ -226,12 +228,12 @@ describe('connection tracking', function () {
{
url: 'mongodb://compass-data-sets-shard-00-00.e06dc.mongodb.net',
is_srv: false,
title: 'is atlas, no srv',
title: 'no srv',
},
{
url: 'mongodb+srv://compass-data-sets.e06dc.mongodb.net',
is_srv: true,
title: 'is atlas, is srv',
title: 'is srv',
},
]) {
it(`tracks a new connection event - ${title}`, async function () {
Expand All @@ -255,6 +257,7 @@ describe('connection tracking', function () {
is_srv: is_srv,
topology_type: 'Unknown',
is_atlas: false,
atlas_hostname: null,
is_local_atlas: false,
is_dataLake: false,
is_enterprise: false,
Expand Down Expand Up @@ -329,6 +332,7 @@ describe('connection tracking', function () {
is_srv: false,
topology_type: 'Unknown',
is_atlas: false,
atlas_hostname: null,
is_local_atlas: true,
is_dataLake: false,
is_enterprise: false,
Expand Down Expand Up @@ -373,6 +377,7 @@ describe('connection tracking', function () {
is_srv: false,
topology_type: 'Unknown',
is_atlas: false,
atlas_hostname: null,
is_local_atlas: false,
is_dataLake: false,
is_enterprise: false,
Expand Down Expand Up @@ -416,6 +421,7 @@ describe('connection tracking', function () {
is_srv: false,
topology_type: 'Unknown',
is_atlas: false,
atlas_hostname: null,
is_local_atlas: false,
is_dataLake: false,
is_enterprise: false,
Expand Down Expand Up @@ -458,6 +464,7 @@ describe('connection tracking', function () {
is_srv: false,
topology_type: 'Unknown',
is_atlas: false,
atlas_hostname: null,
is_local_atlas: false,
is_dataLake: false,
is_enterprise: false,
Expand Down Expand Up @@ -500,6 +507,7 @@ describe('connection tracking', function () {
is_srv: true,
topology_type: 'Unknown',
is_atlas: false,
atlas_hostname: null,
is_local_atlas: false,
is_dataLake: false,
is_enterprise: false,
Expand Down Expand Up @@ -632,13 +640,16 @@ describe('connection tracking', function () {
const connection: ConnectionInfo = {
...connectionInfo,
connectionOptions: {
connectionString: 'mongodb://127.0.0.1',
connectionString: 'mongodb://test-data-sets-a011bb.mongodb.net',
},
};
trackNewConnectionEvent(connection, mockDataService, logger, track);
const [{ properties }] = await trackEvent;

expect(properties.is_atlas).to.equal(true);
expect(properties.atlas_hostname).to.equal(
'test-data-sets-a011bb.mongodb.net'
);
expect(properties.is_local_atlas).to.equal(false);
expect(properties.is_dataLake).to.equal(true);
expect(properties.is_enterprise).to.equal(true);
Expand Down Expand Up @@ -690,6 +701,7 @@ describe('connection tracking', function () {
const [{ properties }] = await trackEvent;

expect(properties.is_atlas).to.equal(false);
expect(properties.atlas_hostname).to.equal(null);
expect(properties.is_local_atlas).to.equal(false);
expect(properties.is_dataLake).to.equal(false);
expect(properties.is_enterprise).to.equal(false);
Expand Down Expand Up @@ -772,6 +784,7 @@ describe('connection tracking', function () {
is_srv: false,
topology_type: 'Unknown',
is_atlas: false,
atlas_hostname: null,
is_local_atlas: false,
is_dataLake: false,
is_enterprise: false,
Expand Down
38 changes: 27 additions & 11 deletions packages/compass/src/app/utils/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,14 @@ function getCsfleInformation(
}

async function getHostnameForConnection(
connectionStringData: ConnectionString
connectionInfo: ConnectionInfo
): Promise<string | null> {
let connectionStringData = new ConnectionString(
connectionInfo.connectionOptions.connectionString,
{
looseValidation: true,
}
);
if (connectionStringData.isSRV) {
const uri = await resolveMongodbSrv(connectionStringData.toString()).catch(
() => {
Expand All @@ -120,11 +126,12 @@ async function getHostnameForConnection(
return connectionStringData.hosts[0];
}

async function getConnectionData({
connectionOptions: { connectionString, sshTunnel, fleOptions },
}: Pick<ConnectionInfo, 'connectionOptions'>): Promise<
Record<string, unknown>
> {
async function getConnectionData(
{
connectionOptions: { connectionString, sshTunnel, fleOptions },
}: Pick<ConnectionInfo, 'connectionOptions'>,
resolvedHostname: string | null
): Promise<Record<string, unknown>> {
const connectionStringData = new ConnectionString(connectionString, {
looseValidation: true,
});
Expand All @@ -139,8 +146,6 @@ async function getConnectionData({
: 'NONE';
const proxyHost = searchParams.get('proxyHost');

const resolvedHostname = await getHostnameForConnection(connectionStringData);

return {
...(await getHostInformation(resolvedHostname)),
auth_type: authType.toUpperCase(),
Expand Down Expand Up @@ -179,10 +184,15 @@ export function trackNewConnectionEvent(
const callback = async () => {
const { dataLake, genuineMongoDB, host, build, isAtlas, isLocalAtlas } =
await dataService.instance();
const connectionData = await getConnectionData(connectionInfo);
const resolvedHostname = await getHostnameForConnection(connectionInfo);
const connectionData = await getConnectionData(
connectionInfo,
resolvedHostname
);
const trackEvent = {
...connectionData,
is_atlas: isAtlas,
atlas_hostname: isAtlas ? resolvedHostname : null,
is_local_atlas: isLocalAtlas,
is_dataLake: dataLake.isDataLake,
is_enterprise: build.isEnterprise,
Expand All @@ -209,8 +219,14 @@ export function trackConnectionFailedEvent(
): void {
try {
const callback = async () => {
const connectionData =
connectionInfo !== null ? await getConnectionData(connectionInfo) : {};
let connectionData: Record<string, unknown> = {};
if (connectionInfo !== null) {
const resolvedHostname = await getHostnameForConnection(connectionInfo);
connectionData = await getConnectionData(
connectionInfo,
resolvedHostname
);
}
const trackEvent = {
...connectionData,
error_code: connectionError.code,
Expand Down