Skip to content

Commit

Permalink
refactor: use resolved srv from topology
Browse files Browse the repository at this point in the history
  • Loading branch information
alenakhineika committed Aug 1, 2024
1 parent 20fceb3 commit b34ec1a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 24 deletions.
11 changes: 9 additions & 2 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,6 @@
"numeral": "^2.0.6",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"resolve-mongodb-srv": "^1.1.5",
"ts-log": "^2.2.5",
"uuid": "^8.3.2",
"vscode-languageclient": "^8.1.0",
Expand Down
27 changes: 7 additions & 20 deletions src/telemetry/connectionTelemetry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { DataService } from 'mongodb-data-service';
import { getCloudInfo } from 'mongodb-cloud-info';
import mongoDBBuildInfo from 'mongodb-build-info';
import resolveMongodbSrv from 'resolve-mongodb-srv';

import { ConnectionTypes } from '../connectionController';
import { createLogger } from '../logging';
import ConnectionString from 'mongodb-connection-string-url';
import type { TopologyType } from 'mongodb';

const log = createLogger('connection telemetry helper');
Expand Down Expand Up @@ -40,22 +37,10 @@ export type HostInformation = {
public_cloud_name?: string;
};

async function getHostnameForConnection(
connectionStringData: ConnectionString
): Promise<string | null> {
if (connectionStringData.isSRV) {
const uri = await resolveMongodbSrv(connectionStringData.toString()).catch(
() => null
);
if (!uri) {
return null;
}
connectionStringData = new ConnectionString(uri, {
looseValidation: true,
});
}

const [hostname] = (connectionStringData.hosts[0] ?? '').split(':');
function getHostnameForConnection(dataService: DataService): string | null {
const lastSeenTopology = dataService.getLastSeenTopology();
const resolvedHost = lastSeenTopology?.servers.values().next().value.address;
const [hostname] = (resolvedHost ?? '').split(':');
return hostname;
}

Expand All @@ -64,6 +49,7 @@ async function getPublicCloudInfo(host: string): Promise<{
is_public_cloud?: boolean;
}> {
try {
const { getCloudInfo } = await import('mongodb-cloud-info');
const { isAws, isAzure, isGcp } = await getCloudInfo(host);
let publicCloudName;

Expand All @@ -84,6 +70,7 @@ async function getPublicCloudInfo(host: string): Promise<{
public_cloud_name: publicCloudName,
};
} catch (err) {
// Cannot resolve dns used by mongodb-cloud-info in the browser environment.
return {};
}
}
Expand Down Expand Up @@ -144,7 +131,7 @@ export async function getConnectionTelemetryProperties(
const authMechanism = connectionString.searchParams.get('authMechanism');
const username = connectionString.username ? 'DEFAULT' : 'NONE';
const authStrategy = authMechanism ?? username;
const resolvedHostname = await getHostnameForConnection(connectionString);
const resolvedHostname = getHostnameForConnection(dataService);
const { dataLake, genuineMongoDB, host, build, isAtlas, isLocalAtlas } =
await dataService.instance();
const atlasHostname = isAtlas ? resolvedHostname : null;
Expand Down
80 changes: 79 additions & 1 deletion src/test/suite/telemetry/connectionTelemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
const sandbox = sinon.createSandbox();
let dataServiceStub;
let getConnectionStringStub;
let getLastSeenTopology;
let instanceStub;

before(() => {
getConnectionStringStub = sandbox.stub();
getLastSeenTopology = sandbox.stub();
instanceStub = sandbox.stub();
dataServiceStub = {
getCurrentTopologyType: sandbox.stub(),
getConnectionString: getConnectionStringStub,
getLastSeenTopology: getLastSeenTopology,
instance: instanceStub,
} as unknown as DataService;
});
Expand Down Expand Up @@ -51,6 +54,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://13.64.151.161')
);
getLastSeenTopology.returns({
servers: new Map().set('13.64.151.161', {
address: '13.64.151.161',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -80,6 +88,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://localhost:27017')
);
getLastSeenTopology.returns({
servers: new Map().set('localhost:27017', {
address: 'localhost:27017',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -111,6 +124,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://localhost:27017')
);
getLastSeenTopology.returns({
servers: new Map().set('localhost:27017', {
address: 'localhost:27017',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -145,6 +163,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://test-data-sets-a011bb.mongodb.net')
);
getLastSeenTopology.returns({
servers: new Map().set('test-data-sets-00-02-a011bb.mongodb.net', {
address: 'test-data-sets-00-02-a011bb.mongodb.net',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand All @@ -153,7 +176,7 @@ suite('ConnectionTelemetry Controller Test Suite', function () {

expect(instanceTelemetry.is_atlas).to.equal(true);
expect(instanceTelemetry.atlas_hostname).to.equal(
'test-data-sets-a011bb.mongodb.net'
'test-data-sets-00-02-a011bb.mongodb.net'
);
expect(instanceTelemetry.is_atlas_url).to.equal(true);
expect(instanceTelemetry.is_local_atlas).to.equal(false);
Expand All @@ -178,6 +201,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://example.mongo.ondigitalocean.com:27017')
);
getLastSeenTopology.returns({
servers: new Map().set('example.mongo.ondigitalocean.com:27017', {
address: 'example.mongo.ondigitalocean.com:27017',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -209,6 +237,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://localhost:27017')
);
getLastSeenTopology.returns({
servers: new Map().set('localhost:27017', {
address: 'localhost:27017',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -239,6 +272,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://localhost:27017')
);
getLastSeenTopology.returns({
servers: new Map().set('localhost:27017', {
address: 'localhost:27017',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -269,6 +307,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://localhost:27017')
);
getLastSeenTopology.returns({
servers: new Map().set('localhost:27017', {
address: 'localhost:27017',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -299,6 +342,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://example.mongo.ondigitalocean.com:27017')
);
getLastSeenTopology.returns({
servers: new Map().set('example.mongo.ondigitalocean.com:27017', {
address: 'example.mongo.ondigitalocean.com:27017',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -327,6 +375,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://localhost:27017')
);
getLastSeenTopology.returns({
servers: new Map().set('localhost:27017', {
address: 'localhost:27017',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -361,6 +414,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://127.0.0.1')
);
getLastSeenTopology.returns({
servers: new Map().set('127.0.0.1', {
address: '127.0.0.1',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -397,6 +455,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://127.0.0.1')
);
getLastSeenTopology.returns({
servers: new Map().set('127.0.0.1', {
address: '127.0.0.1',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -427,6 +490,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://artishok:pass@localhost:27017')
);
getLastSeenTopology.returns({
servers: new Map().set('localhost:27017', {
address: 'localhost:27017',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -455,6 +523,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
getConnectionStringStub.returns(
new ConnectionString('mongodb://localhost:27017')
);
getLastSeenTopology.returns({
servers: new Map().set('localhost:27017', {
address: 'localhost:27017',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down Expand Up @@ -485,6 +558,11 @@ suite('ConnectionTelemetry Controller Test Suite', function () {
'mongodb://foo:bar@localhost:27017/?authSource=source&authMechanism=SCRAM-SHA-1'
)
);
getLastSeenTopology.returns({
servers: new Map().set('localhost:27017', {
address: 'localhost:27017',
}),
});

const instanceTelemetry = await getConnectionTelemetryProperties(
dataServiceStub,
Expand Down

0 comments on commit b34ec1a

Please sign in to comment.