Improve observability (#834)

* API generation

* Added correlation id support

* Updated docs

* Updated test

* Updated code generation

* API generation

* Updated code generation

* Added support for client name and custom context object

* Updated docs

* Updated test

* Fix docs

* Updated docs

* Added id support also for sniffing

* Updated test

* Update docs/observability.asciidoc

Co-Authored-By: delvedor <delvedor@users.noreply.github.com>

* Update docs/observability.asciidoc

Co-Authored-By: delvedor <delvedor@users.noreply.github.com>

* Apply suggestions

* Update docs/configuration.asciidoc

Co-Authored-By: delvedor <delvedor@users.noreply.github.com>

* Update docs/configuration.asciidoc

Co-Authored-By: delvedor <delvedor@users.noreply.github.com>

* Update docs/observability.asciidoc

Co-Authored-By: delvedor <delvedor@users.noreply.github.com>

* Update docs/observability.asciidoc

Co-Authored-By: delvedor <delvedor@users.noreply.github.com>

* Update docs/observability.asciidoc

Co-Authored-By: delvedor <delvedor@users.noreply.github.com>

* Apply suggestions

* Updated README.md

* Fixed test

* Addressed suggestions
This commit is contained in:
Tomas Della Vedova
2019-05-03 17:23:40 +02:00
committed by delvedor
parent 9dacd9d9ee
commit b1458e3511
279 changed files with 2436 additions and 142 deletions

View File

@ -211,3 +211,72 @@ test('Should share the event emitter', t => {
t.end()
})
test('Should create a child client (generateRequestId check)', t => {
t.plan(6)
function generateRequestId1 () {
var id = 0
return () => `trace-1-${id++}`
}
function generateRequestId2 () {
var id = 0
return () => `trace-2-${id++}`
}
const client = new Client({
node: 'http://localhost:9200',
Connection: MockConnection,
generateRequestId: generateRequestId1()
})
const child = client.child({
Connection: MockConnection,
generateRequestId: generateRequestId2()
})
var count = 0
client.on('request', (err, { meta }) => {
t.error(err)
t.strictEqual(
meta.request.id,
count++ === 0 ? 'trace-1-0' : 'trace-2-0'
)
})
client.info(err => {
t.error(err)
child.info(t.error)
})
})
test('Should create a child client (name check)', t => {
t.plan(8)
const client = new Client({
node: 'http://localhost:9200',
Connection: MockConnection,
name: 'parent'
})
const child = client.child({
Connection: MockConnection,
name: 'child'
})
t.strictEqual(client.name, 'parent')
t.strictEqual(child.name, 'child')
var count = 0
client.on('request', (err, { meta }) => {
t.error(err)
t.strictEqual(
meta.name,
count++ === 0 ? 'parent' : 'child'
)
})
client.info(err => {
t.error(err)
child.info(t.error)
})
})

View File

@ -118,7 +118,12 @@ test('API', t => {
const href = 'http://localhost:9200/'
var connection = pool.addConnection(href)
pool.markDead(connection)
pool.resurrect(Date.now() + 1000 * 60 * 3, (isAlive, connection) => {
const opts = {
now: Date.now() + 1000 * 60 * 3,
requestId: 1,
name: 'elasticsearch-js'
}
pool.resurrect(opts, (isAlive, connection) => {
t.true(isAlive)
connection = pool.connections.get(connection.id)
t.strictEqual(connection.deadCount, 0)
@ -139,7 +144,12 @@ test('API', t => {
const href = 'http://localhost:9200/'
var connection = pool.addConnection(href)
pool.markDead(connection)
pool.resurrect(Date.now() + 1000 * 60 * 3, (isAlive, connection) => {
const opts = {
now: Date.now() + 1000 * 60 * 3,
requestId: 1,
name: 'elasticsearch-js'
}
pool.resurrect(opts, (isAlive, connection) => {
t.false(isAlive)
connection = pool.connections.get(connection.id)
t.strictEqual(connection.deadCount, 2)
@ -162,7 +172,12 @@ test('API', t => {
const href = 'http://localhost:9200/'
var connection = pool.addConnection(href)
pool.markDead(connection)
pool.resurrect(Date.now() + 1000 * 60 * 3, (isAlive, connection) => {
const opts = {
now: Date.now() + 1000 * 60 * 3,
requestId: 1,
name: 'elasticsearch-js'
}
pool.resurrect(opts, (isAlive, connection) => {
t.true(isAlive)
connection = pool.connections.get(connection.id)
t.strictEqual(connection.deadCount, 1)
@ -182,7 +197,12 @@ test('API', t => {
const href = 'http://localhost:9200/'
var connection = pool.addConnection(href)
pool.markDead(connection)
pool.resurrect(Date.now() + 1000 * 60 * 3, (isAlive, connection) => {
const opts = {
now: Date.now() + 1000 * 60 * 3,
requestId: 1,
name: 'elasticsearch-js'
}
pool.resurrect(opts, (isAlive, connection) => {
t.ok(isAlive === null)
t.ok(connection === null)
connection = pool.connections.get(href)

View File

@ -40,6 +40,8 @@ test('Should emit a request event when a request is performed', t => {
headers: null,
warnings: null,
meta: {
context: null,
name: 'elasticsearch-js',
request: {
params: {
method: 'GET',
@ -59,7 +61,8 @@ test('Should emit a request event when a request is performed', t => {
headers: null,
compression: false,
warnings: null
}
},
id: 1
},
connection: {
id: 'http://localhost:9200'
@ -98,6 +101,8 @@ test('Should emit a response event in case of a successful response', t => {
},
warnings: null,
meta: {
context: null,
name: 'elasticsearch-js',
request: {
params: {
method: 'GET',
@ -117,7 +122,8 @@ test('Should emit a response event in case of a successful response', t => {
headers: null,
compression: false,
warnings: null
}
},
id: 1
},
connection: {
id: 'http://localhost:9200'
@ -154,6 +160,8 @@ test('Should emit a response event with the error set', t => {
headers: null,
warnings: null,
meta: {
context: null,
name: 'elasticsearch-js',
request: {
params: {
method: 'GET',
@ -173,7 +181,8 @@ test('Should emit a response event with the error set', t => {
headers: null,
compression: false,
warnings: null
}
},
id: 1
},
connection: {
id: 'http://localhost:9200'

View File

@ -704,11 +704,13 @@ test('Should call markAlive with a successful response', t => {
})
test('Should call resurrect on every request', t => {
t.plan(3)
t.plan(5)
class CustomConnectionPool extends ConnectionPool {
resurrect (now) {
resurrect ({ now, requestId, name }) {
t.type(now, 'number')
t.type(requestId, 'number')
t.type(name, 'string')
}
}
@ -725,7 +727,8 @@ test('Should call resurrect on every request', t => {
maxRetries: 3,
requestTimeout: 30000,
sniffInterval: false,
sniffOnStart: false
sniffOnStart: false,
name: 'elasticsearch-js'
})
transport.request({
@ -2107,3 +2110,30 @@ test('Should accept custom querystring in the optons object', t => {
t.end()
})
test('Should pass request params and options to generateRequestId', t => {
t.plan(3)
const pool = new ConnectionPool({ Connection: MockConnection })
pool.addConnection('http://localhost:9200')
const params = { method: 'GET', path: '/hello' }
const options = { context: { winter: 'is coming' } }
const transport = new Transport({
emit: () => {},
connectionPool: pool,
serializer: new Serializer(),
maxRetries: 3,
requestTimeout: 30000,
sniffInterval: false,
sniffOnStart: false,
generateRequestId: function (requestParams, requestOptions) {
t.deepEqual(requestParams, params)
t.deepEqual(requestOptions, options)
return 'id'
}
})
transport.request(params, options, t.error)
})