Skip to content

Commit dc087d0

Browse files
authored
chore(data-service): add a log whenever connecting throws and include connection id COMPASS-9404 (#6953)
1 parent f675df5 commit dc087d0

File tree

3 files changed

+134
-4
lines changed

3 files changed

+134
-4
lines changed

packages/compass-e2e-tests/tests/logging.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,11 @@ describe('Logging and Telemetry integration', function () {
206206
c: 'COMPASS-DATA-SERVICE',
207207
id: 1_001_000_014,
208208
ctx: 'Connection 0',
209-
msg: 'Connecting',
209+
msg: 'Connecting Started',
210210
attr: (actual: any) => {
211211
expect(actual.url).to.match(/^mongodb:\/\/127.0.0.1:27091/);
212212
expect(actual.csfle).to.equal(null);
213+
expect(actual).to.have.property('connectionId', 0);
213214
},
214215
},
215216
{
@@ -284,8 +285,9 @@ describe('Logging and Telemetry integration', function () {
284285
c: 'COMPASS-DATA-SERVICE',
285286
id: 1_001_000_015,
286287
ctx: 'Connection 0',
287-
msg: 'Connected',
288+
msg: 'Connecting Succeeded',
288289
attr: {
290+
connectionId: 0,
289291
isMongos: false,
290292
isWritable: true,
291293
},

packages/data-service/src/data-service.spec.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { mochaTestServer } from '@mongodb-js/compass-test-server';
2323
import type { SearchIndex } from './search-index-detail-helper';
2424
import { range } from 'lodash';
2525
import ConnectionString from 'mongodb-connection-string-url';
26+
import type { DataServiceImplLogger, MongoLogId } from './logger';
2627

2728
const { expect } = chai;
2829
chai.use(chaiAsPromised);
@@ -93,6 +94,118 @@ describe('DataService', function () {
9394
.drop();
9495
});
9596

97+
describe('#connect', function () {
98+
const connectionStartedId = 1_001_000_014;
99+
const connectionSucceededId = 1_001_000_015;
100+
const connectionFailedId = 1_001_000_359;
101+
const logs: {
102+
info: [
103+
component: string,
104+
id: MongoLogId,
105+
context: string,
106+
message: string,
107+
attr?: unknown
108+
];
109+
}[] = [];
110+
const logCollector: DataServiceImplLogger = {
111+
error: () => {},
112+
info: (...args) => logs.push({ info: args }),
113+
debug: () => {},
114+
warn: () => {},
115+
fatal: () => {},
116+
};
117+
118+
let dataServiceLogTest;
119+
120+
beforeEach(function () {
121+
logs.length = 0;
122+
});
123+
124+
afterEach(async function () {
125+
await dataServiceLogTest?.disconnect();
126+
dataServiceLogTest = undefined;
127+
});
128+
129+
// The log and connection ids in this test are used by CompassWeb to measure connect time metrics.
130+
// The intention of these tests is not to restrict any changes to the logs or ids.
131+
// If a change to the ids is necessary please inform the appropriate team and they can adjust before pulling in the change.
132+
it('when connecting succeeds there is a start and success log with a connectionId', async function () {
133+
dataServiceLogTest = new DataServiceImpl(
134+
connectionOptions,
135+
logCollector
136+
);
137+
await dataServiceLogTest.connect();
138+
139+
expect(logs.map(({ info: [, id] }) => id.__value)).to.include(
140+
connectionStartedId
141+
);
142+
expect(logs.map(({ info: [, id] }) => id.__value)).to.include(
143+
connectionSucceededId
144+
);
145+
146+
const startedLog = logs.find(
147+
({ info: [, id] }) => id.__value === connectionStartedId
148+
);
149+
const succeededLog = logs.find(
150+
({ info: [, id] }) => id.__value === connectionSucceededId
151+
);
152+
153+
const { info: [, , , , startedAttr] = [] } = startedLog ?? {};
154+
expect(startedAttr).to.have.property(
155+
'connectionId',
156+
dataServiceLogTest._id
157+
);
158+
const { info: [, , , , succeededAttr] = [] } = succeededLog ?? {};
159+
expect(succeededAttr).to.have.property(
160+
'connectionId',
161+
dataServiceLogTest._id
162+
);
163+
});
164+
165+
it('when connecting fails there is a start and failure log with a connectionId', async function () {
166+
dataServiceLogTest = new DataServiceImpl(
167+
{
168+
connectionString:
169+
'mongodb://iLoveJavascript?serverSelectionTimeoutMS=5',
170+
lookup: () => {
171+
throw new Error('test error');
172+
},
173+
},
174+
logCollector
175+
);
176+
177+
const result = await dataServiceLogTest
178+
.connect()
179+
.catch((error) => error);
180+
expect(result).to.be.instanceOf(Error);
181+
182+
expect(logs.map(({ info: [, id] }) => id.__value)).to.include(
183+
connectionStartedId
184+
);
185+
expect(logs.map(({ info: [, id] }) => id.__value)).to.include(
186+
connectionFailedId
187+
);
188+
189+
const startedLog = logs.find(
190+
({ info: [, id] }) => id.__value === connectionStartedId
191+
);
192+
const failedLog = logs.find(
193+
({ info: [, id] }) => id.__value === connectionFailedId
194+
);
195+
196+
const { info: [, , , , startedAttr] = [] } = startedLog ?? {};
197+
expect(startedAttr).to.have.property(
198+
'connectionId',
199+
dataServiceLogTest._id
200+
);
201+
const { info: [, , , , succeededAttr] = [] } = failedLog ?? {};
202+
expect(succeededAttr).to.have.property(
203+
'connectionId',
204+
dataServiceLogTest._id
205+
);
206+
});
207+
});
208+
96209
describe('#isConnected', function () {
97210
let dataServiceIsConnected: DataService;
98211

packages/data-service/src/data-service.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,8 @@ class DataServiceImpl extends WithLogContext implements DataService {
15791579
debug('connecting...');
15801580
this._isConnecting = true;
15811581

1582-
this._logger.info(mongoLogId(1_001_000_014), 'Connecting', {
1582+
this._logger.info(mongoLogId(1_001_000_014), 'Connecting Started', {
1583+
connectionId: this._id,
15831584
url: redactConnectionString(this._connectionOptions.connectionString),
15841585
csfle: this._csfleLogInformation(this._connectionOptions.fleOptions),
15851586
});
@@ -1599,11 +1600,16 @@ class DataServiceImpl extends WithLogContext implements DataService {
15991600
});
16001601

16011602
const attr = {
1603+
connectionId: this._id,
16021604
isWritable: this.isWritable(),
16031605
isMongos: this.isMongos(),
16041606
};
16051607

1606-
this._logger.info(mongoLogId(1_001_000_015), 'Connected', attr);
1608+
this._logger.info(
1609+
mongoLogId(1_001_000_015),
1610+
'Connecting Succeeded',
1611+
attr
1612+
);
16071613
debug('connected!', attr);
16081614

16091615
state.oidcPlugin.logger.on('mongodb-oidc-plugin:state-updated', () => {
@@ -1626,6 +1632,15 @@ class DataServiceImpl extends WithLogContext implements DataService {
16261632
this,
16271633
this._crudClient
16281634
);
1635+
} catch (error) {
1636+
this._logger.info(mongoLogId(1_001_000_359), 'Connecting Failed', {
1637+
connectionId: this._id,
1638+
error:
1639+
error && typeof error === 'object' && 'message' in error
1640+
? error?.message
1641+
: 'unknown error',
1642+
});
1643+
throw error;
16291644
} finally {
16301645
this._isConnecting = false;
16311646
}

0 commit comments

Comments
 (0)