Skip to content

Include database connection ID in log messages #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2018
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
28 changes: 20 additions & 8 deletions src/v1/internal/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export default class Connection {
this._chunker = new Chunker( channel );
this._log = log;

// connection from the database, returned in response for HELLO message and might not be available
this._dbConnectionId = null;

// bolt protocol is initially not initialized
this._protocol = null;

Expand Down Expand Up @@ -388,7 +391,8 @@ export default class Connection {
}

toString() {
return `Connection ${this.id}`;
const dbConnectionId = this._dbConnectionId || '';
return `Connection [${this.id}][${dbConnectionId}]`;
}

_packable(value) {
Expand Down Expand Up @@ -424,13 +428,21 @@ class InitializationObserver {
}

onCompleted(metadata) {
// read server version from the response metadata
const serverVersion = metadata ? metadata.server : null;
if (!this._connection.server.version) {
this._connection.server.version = serverVersion;
const version = ServerVersion.fromString(serverVersion);
if (version.compareTo(VERSION_3_2_0) < 0) {
this._connection.protocol().packer().disableByteArrays();
if (metadata) {
// read server version from the response metadata, if it is available
const serverVersion = metadata.server;
if (!this._connection.server.version) {
this._connection.server.version = serverVersion;
const version = ServerVersion.fromString(serverVersion);
if (version.compareTo(VERSION_3_2_0) < 0) {
this._connection.protocol().packer().disableByteArrays();
}
}

// read database connection id from the response metadata, if it is available
const dbConnectionId = metadata.connection_id;
if (!this._connection._dbConnectionId) {
this._connection._dbConnectionId = dbConnectionId;
}
}

Expand Down
40 changes: 40 additions & 0 deletions test/internal/node/direct.driver.boltkit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,44 @@ describe('direct driver with stub server', () => {
});
});
});

it('should include database connection id in logs', done => {
if (!boltStub.supported) {
done();
return;
}

const server = boltStub.start('./test/resources/boltstub/hello_run_exit.script', 9001);

boltStub.run(() => {
const messages = [];
const logging = {
level: 'debug',
logger: (level, message) => messages.push(message)
};

const driver = boltStub.newDriver('bolt://127.0.0.1:9001', {logging: logging});
const session = driver.session();

session.run('MATCH (n) RETURN n.name').then(result => {
const names = result.records.map(record => record.get(0));
expect(names).toEqual(['Foo', 'Bar']);
session.close(() => {
driver.close();
server.exit(code => {
expect(code).toEqual(0);

// logged messages should contain connection_id supplied by the database
const containsDbConnectionIdMessage = messages.find(message => message.match(/Connection \[[0-9]+]\[bolt-123456789]/));
if (!containsDbConnectionIdMessage) {
console.log(messages);
}
expect(containsDbConnectionIdMessage).toBeTruthy();

done();
});
});
}).catch(error => done.fail(error));
});
});
});
12 changes: 12 additions & 0 deletions test/resources/boltstub/hello_run_exit.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
!: BOLT 3
!: AUTO RESET

C: HELLO {"credentials": "password", "scheme": "basic", "user_agent": "neo4j-javascript/0.0.0-dev", "principal": "neo4j"}
S: SUCCESS {"server": "Neo4j/9.9.9", "connection_id": "bolt-123456789"}
C: RUN "MATCH (n) RETURN n.name" {} {}
PULL_ALL
S: SUCCESS {"fields": ["n.name"]}
RECORD ["Foo"]
RECORD ["Bar"]
SUCCESS {}
S: <EXIT>