Skip to content

Commit b267daf

Browse files
authored
Merge pull request #419 from lutovich/1.7-connection-id
Include database connection ID in log messages
2 parents 85aa92f + 59fcc21 commit b267daf

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

src/v1/internal/connection.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ export default class Connection {
6868
this._chunker = new Chunker( channel );
6969
this._log = log;
7070

71+
// connection from the database, returned in response for HELLO message and might not be available
72+
this._dbConnectionId = null;
73+
7174
// bolt protocol is initially not initialized
7275
this._protocol = null;
7376

@@ -388,7 +391,8 @@ export default class Connection {
388391
}
389392

390393
toString() {
391-
return `Connection ${this.id}`;
394+
const dbConnectionId = this._dbConnectionId || '';
395+
return `Connection [${this.id}][${dbConnectionId}]`;
392396
}
393397

394398
_packable(value) {
@@ -424,13 +428,21 @@ class InitializationObserver {
424428
}
425429

426430
onCompleted(metadata) {
427-
// read server version from the response metadata
428-
const serverVersion = metadata ? metadata.server : null;
429-
if (!this._connection.server.version) {
430-
this._connection.server.version = serverVersion;
431-
const version = ServerVersion.fromString(serverVersion);
432-
if (version.compareTo(VERSION_3_2_0) < 0) {
433-
this._connection.protocol().packer().disableByteArrays();
431+
if (metadata) {
432+
// read server version from the response metadata, if it is available
433+
const serverVersion = metadata.server;
434+
if (!this._connection.server.version) {
435+
this._connection.server.version = serverVersion;
436+
const version = ServerVersion.fromString(serverVersion);
437+
if (version.compareTo(VERSION_3_2_0) < 0) {
438+
this._connection.protocol().packer().disableByteArrays();
439+
}
440+
}
441+
442+
// read database connection id from the response metadata, if it is available
443+
const dbConnectionId = metadata.connection_id;
444+
if (!this._connection._dbConnectionId) {
445+
this._connection._dbConnectionId = dbConnectionId;
434446
}
435447
}
436448

test/internal/node/direct.driver.boltkit.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,44 @@ describe('direct driver with stub server', () => {
332332
});
333333
});
334334
});
335+
336+
it('should include database connection id in logs', done => {
337+
if (!boltStub.supported) {
338+
done();
339+
return;
340+
}
341+
342+
const server = boltStub.start('./test/resources/boltstub/hello_run_exit.script', 9001);
343+
344+
boltStub.run(() => {
345+
const messages = [];
346+
const logging = {
347+
level: 'debug',
348+
logger: (level, message) => messages.push(message)
349+
};
350+
351+
const driver = boltStub.newDriver('bolt://127.0.0.1:9001', {logging: logging});
352+
const session = driver.session();
353+
354+
session.run('MATCH (n) RETURN n.name').then(result => {
355+
const names = result.records.map(record => record.get(0));
356+
expect(names).toEqual(['Foo', 'Bar']);
357+
session.close(() => {
358+
driver.close();
359+
server.exit(code => {
360+
expect(code).toEqual(0);
361+
362+
// logged messages should contain connection_id supplied by the database
363+
const containsDbConnectionIdMessage = messages.find(message => message.match(/Connection \[[0-9]+]\[bolt-123456789]/));
364+
if (!containsDbConnectionIdMessage) {
365+
console.log(messages);
366+
}
367+
expect(containsDbConnectionIdMessage).toBeTruthy();
368+
369+
done();
370+
});
371+
});
372+
}).catch(error => done.fail(error));
373+
});
374+
});
335375
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
!: BOLT 3
2+
!: AUTO RESET
3+
4+
C: HELLO {"credentials": "password", "scheme": "basic", "user_agent": "neo4j-javascript/0.0.0-dev", "principal": "neo4j"}
5+
S: SUCCESS {"server": "Neo4j/9.9.9", "connection_id": "bolt-123456789"}
6+
C: RUN "MATCH (n) RETURN n.name" {} {}
7+
PULL_ALL
8+
S: SUCCESS {"fields": ["n.name"]}
9+
RECORD ["Foo"]
10+
RECORD ["Bar"]
11+
SUCCESS {}
12+
S: <EXIT>

0 commit comments

Comments
 (0)