|
1 | 1 | import type { DataService } from 'mongodb-data-service';
|
2 | 2 | import { getCloudInfo } from 'mongodb-cloud-info';
|
3 | 3 | import mongoDBBuildInfo from 'mongodb-build-info';
|
| 4 | +import resolveMongodbSrv from 'resolve-mongodb-srv'; |
4 | 5 |
|
5 | 6 | import { ConnectionTypes } from '../connectionController';
|
6 | 7 | import { createLogger } from '../logging';
|
| 8 | +import ConnectionString from 'mongodb-connection-string-url'; |
7 | 9 |
|
8 | 10 | const log = createLogger('connection telemetry helper');
|
9 | 11 | // eslint-disable-next-line @typescript-eslint/no-var-requires
|
@@ -34,14 +36,36 @@ type CloudInfo = {
|
34 | 36 | publicCloudName?: string | null;
|
35 | 37 | };
|
36 | 38 |
|
| 39 | +async function getHostnameForConnection( |
| 40 | + connectionStringData: ConnectionString |
| 41 | +): Promise<string | null> { |
| 42 | + if (connectionStringData.isSRV) { |
| 43 | + const uri = await resolveMongodbSrv(connectionStringData.toString()).catch( |
| 44 | + () => null |
| 45 | + ); |
| 46 | + if (!uri) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + connectionStringData = new ConnectionString(uri, { |
| 50 | + looseValidation: true, |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + const [hostname] = (connectionStringData.hosts[0] ?? '').split(':'); |
| 55 | + return hostname; |
| 56 | +} |
| 57 | + |
37 | 58 | async function getCloudInfoFromDataService(
|
38 |
| - firstServerHostname: string |
| 59 | + dataService: DataService |
39 | 60 | ): Promise<CloudInfo> {
|
| 61 | + const hostname = await getHostnameForConnection( |
| 62 | + dataService.getConnectionString() |
| 63 | + ); |
40 | 64 | const cloudInfo: {
|
41 | 65 | isAws?: boolean;
|
42 | 66 | isAzure?: boolean;
|
43 | 67 | isGcp?: boolean;
|
44 |
| - } = await getCloudInfo(firstServerHostname); |
| 68 | + } = await getCloudInfo(hostname); |
45 | 69 |
|
46 | 70 | if (cloudInfo.isAws) {
|
47 | 71 | return {
|
@@ -82,15 +106,13 @@ export async function getConnectionTelemetryProperties(
|
82 | 106 |
|
83 | 107 | try {
|
84 | 108 | const connectionString = dataService.getConnectionString();
|
85 |
| - const firstHost = connectionString.hosts[0] || ''; |
86 |
| - const [hostname] = firstHost.split(':'); |
87 | 109 | const authMechanism = connectionString.searchParams.get('authMechanism');
|
88 | 110 | const username = connectionString.username ? 'DEFAULT' : 'NONE';
|
89 | 111 | const authStrategy = authMechanism ?? username;
|
90 | 112 |
|
91 | 113 | const [instance, cloudInfo] = await Promise.all([
|
92 | 114 | dataService.instance(),
|
93 |
| - getCloudInfoFromDataService(hostname), |
| 115 | + getCloudInfoFromDataService(dataService), |
94 | 116 | ]);
|
95 | 117 |
|
96 | 118 | preparedProperties = {
|
|
0 commit comments