Skip to content

Commit 25933c0

Browse files
authored
Improve docs about observability event emitter (#2765)
1 parent 98b3802 commit 25933c0

File tree

1 file changed

+69
-18
lines changed

1 file changed

+69
-18
lines changed

docs/reference/observability.md

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Correlating events can be hard, especially if your applications have a large cod
1515

1616
All of these observability features are documented below.
1717

18-
1918
## OpenTelemetry [_opentelemetry]
2019

2120
The client supports OpenTelemetry’s [zero-code instrumentation](https://opentelemetry.io/docs/zero-code/js/) to enable tracking each client request as an [OpenTelemetry span](https://opentelemetry.io/docs/concepts/signals/traces/#spans). These spans follow all of the [semantic OpenTelemetry conventions for Elasticsearch](https://opentelemetry.io/docs/specs/semconv/database/elasticsearch/) except for `db.query.text`.
@@ -36,7 +35,6 @@ To start sending Elasticsearch trace data to your OpenTelemetry endpoint, follow
3635
node --require '@opentelemetry/auto-instrumentations-node/register' index.js
3736
```
3837

39-
4038
## Events [_events]
4139

4240
The client is an event emitter. This means that you can listen for its events to add additional logic to your code, without needing to change the client’s internals or how you use the client. You can find the events' names by accessing the `events` key of the client:
@@ -65,16 +63,75 @@ client.diagnostic.on('response', (err, result) => {
6563
})
6664
```
6765

66+
### Event types
67+
6868
The client emits the following events:
6969

70-
| | |
71-
| --- | --- |
72-
| `serialization` | Emitted before starting serialization and compression. If you want to measure this phase duration, you should measure the time elapsed between this event and `request`.<br><br>```js<br>client.diagnostic.on('serialization', (err, result) => {<br> console.log(err, result)<br>})<br>```<br> |
73-
| `request` | Emitted before sending the actual request to {{es}} *(emitted multiple times in case of retries)*.<br><br>```js<br>client.diagnostic.on('request', (err, result) => {<br> console.log(err, result)<br>})<br>```<br> |
74-
| `deserialization` | Emitted before starting deserialization and decompression. If you want to measure this phase duration, you should measure the time elapsed between this event and `response`. *(This event might not be emitted in certain situations)*.<br><br>```js<br>client.diagnostic.on('deserialization', (err, result) => {<br> console.log(err, result)<br>})<br>```<br> |
75-
| `response` | Emitted once {{es}} response has been received and parsed.<br><br>```js<br>client.diagnostic.on('response', (err, result) => {<br> console.log(err, result)<br>})<br>```<br> |
76-
| `sniff` | Emitted when the client ends a sniffing request.<br><br>```js<br>client.diagnostic.on('sniff', (err, result) => {<br> console.log(err, result)<br>})<br>```<br> |
77-
| `resurrect` | Emitted if the client is able to resurrect a dead node.<br><br>```js<br>client.diagnostic.on('resurrect', (err, result) => {<br> console.log(err, result)<br>})<br>```<br> |
70+
#### `serialization`
71+
72+
Emitted before starting serialization and compression. If you want to measure this phase duration, you should measure the time elapsed between this event and `request`.
73+
74+
```js
75+
client.diagnostic.on("serialization", (err, result) => {
76+
console.log(err, result)
77+
})
78+
```
79+
80+
#### `request`
81+
82+
Emitted before sending the actual request to {{es}} _(emitted multiple times in case of retries)_.
83+
84+
```js
85+
client.diagnostic.on("request", (err, result) => {
86+
console.log(err, result)
87+
})
88+
```
89+
90+
#### `deserialization`
91+
92+
Emitted before starting deserialization and decompression. If you want to measure this phase duration, you should measure the time elapsed between this event and `response`.
93+
94+
This event might not be emitted in certain situations:
95+
96+
* When `asStream` is set to true, the response is returned in its raw stream form before deserialization occurs
97+
* When a response is terminated early due to content length being too large
98+
* When a response is terminated early by an `AbortController`
99+
100+
```js
101+
client.diagnostic.on("deserialization", (err, result) => {
102+
console.log(err, result)
103+
})
104+
```
105+
106+
#### `response`
107+
108+
Emitted once {{es}} response has been received and parsed.
109+
110+
```js
111+
client.diagnostic.on("response", (err, result) => {
112+
console.log(err, result)
113+
})
114+
```
115+
116+
#### `sniff`
117+
118+
Emitted when the client ends a sniffing request.
119+
120+
```js
121+
client.diagnostic.on("sniff", (err, result) => {
122+
console.log(err, result)
123+
})
124+
```
125+
126+
#### `resurrect`
127+
128+
Emitted if the client is able to resurrect a dead node.
129+
130+
```js
131+
client.diagnostic.on("resurrect", (err, result) => {
132+
console.log(err, result)
133+
})
134+
```
78135

79136
The values of `result` in `serialization`, `request`, `deserialization`, `response` and `sniff` are:
80137

@@ -113,7 +170,6 @@ request: {
113170
};
114171
```
115172

116-
117173
### Events order [_events_order]
118174

119175
The event order is described in the following graph, in some edge cases, the order is not guaranteed. You can find in [`test/acceptance/events-order.test.js`](https://github.com/elastic/elasticsearch-js/blob/main/test/acceptance/events-order.test.js) how the order changes based on the situation.
@@ -134,7 +190,6 @@ serialization
134190
└─▶ response
135191
```
136192

137-
138193
## Correlation ID [_correlation_id]
139194

140195
Correlating events can be hard, especially if there are many events at the same time. The client offers you an automatic (and configurable) system to help you handle this problem.
@@ -176,7 +231,7 @@ const client = new Client({
176231
// it takes two parameters, the request parameters and options
177232
generateRequestId: function (params, options) {
178233
// your id generation logic
179-
// must be syncronous
234+
// must be synchronous
180235
return 'id'
181236
}
182237
})
@@ -193,7 +248,6 @@ client.search({
193248
}).then(console.log, console.log)
194249
```
195250

196-
197251
## Context object [_context_object]
198252

199253
Sometimes, you might need to make some custom data available in your events, you can do that via the `context` option of a request:
@@ -263,10 +317,9 @@ client.search({
263317
}).then(console.log, console.log)
264318
```
265319

266-
267320
## Client name [_client_name]
268321

269-
If you are using multiple instances of the client or if you are using multiple child clients *(which is the recommended way to have multiple instances of the client)*, you might need to recognize which client you are using. The `name` options help you in this regard.
322+
If you are using multiple instances of the client or if you are using multiple child clients _(which is the recommended way to have multiple instances of the client)_, you might need to recognize which client you are using. The `name` options help you in this regard.
270323

271324
```js
272325
const { Client } = require('@elastic/elasticsearch')
@@ -309,7 +362,6 @@ child.search({
309362
}).then(console.log, console.log)
310363
```
311364

312-
313365
## X-Opaque-Id support [_x_opaque_id_support]
314366

315367
To improve observability, the client offers an easy way to configure the `X-Opaque-Id` header. If you set the `X-Opaque-Id` in a specific request, this allows you to discover this identifier in the [deprecation logs](docs-content://deploy-manage/monitor/logging-configuration/update-elasticsearch-logging-levels.md#deprecation-logging), helps you with [identifying search slow log origin](elasticsearch://reference/elasticsearch/index-settings/slow-log.md) as well as [identifying running tasks](https://www.elastic.co/docs/api/doc/elasticsearch/group/endpoint-tasks).
@@ -348,4 +400,3 @@ client.search({
348400
opaqueId: 'my-search'
349401
}).then(console.log, console.log)
350402
```
351-

0 commit comments

Comments
 (0)