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 GitHub
parent 1261e60d41
commit 269c0fc96a
279 changed files with 1428 additions and 142 deletions

View File

@ -164,6 +164,14 @@ _Default:_ `null`
|`querystring`
|`object` - Custom querystring for the request. +
_Default:_ `null`
|`id`
|`any` - Custom request id. _(overrides the top level request id generator)_ +
_Default:_ `null`
|`context`
|`any` - Custom object per request. _(you can use it to pass some data to the clients events)_ +
_Default:_ `null`
|===
=== Error handling
@ -203,103 +211,3 @@ Following you can find the errors exported by the client.
|`ResponseError`
|Generated when in case of a `4xx` or `5xx` response.
|===
=== Events
The client is an event emitter, this means that you can listen for its event and add additional logic to your code, without need to change the client internals or your normal usage. +
You can find the events names by access the `events` key of the client.
[source,js]
----
const { events } = require('@elastic/elasticsearch')
console.log(events)
----
The event emitter functionality can be useful if you want to log every request, response and error that is happening during the use of the client.
[source,js]
----
const logger = require('my-logger')()
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })
client.on('response', (err, req) => {
if (err) {
logger.error(err)
} else {
logger.info(req)
}
})
----
The client emits the following events:
[cols=2*]
|===
|`request`
a|Emitted before to send the actual request to Elasticsearch.
[source,js]
----
client.on('request', (err, req) => {
console.log(err, req)
})
----
|`response`
a|Emitted before to send the actual request to Elasticsearch.
[source,js]
----
client.on('response', (err, req) => {
console.log(err, req)
})
----
|`sniff`
a|Emitted before to send the actual request to Elasticsearch.
[source,js]
----
client.on('sniff', (err, req) => {
console.log(err, req)
})
----
|`resurrect`
a|Emitted before to send the actual request to Elasticsearch.
[source,js]
----
client.on('resurrect', (err, req) => {
console.log(err, req)
})
----
|===
The values of `req` in `request`, `response` and `sniff` will be:
[source,ts]
----
body: any;
statusCode: number | null;
headers: anyObject | null;
warnings: string[] | null;
meta: {
request: {
params: TransportRequestParams;
options: TransportRequestOptions;
};
connection: Connection;
attempts: number;
aborted: boolean;
sniff?: {
hosts: any[];
reason: string;
};
};
----
While the `req` value in `resurrect` will be:
[source,ts]
----
export interface ResurrectEvent {
strategy: string;
isAlive: boolean;
connection: Connection;
}
----