Skip to content

Commit 3303590

Browse files
authored
Added new observability events (#1365)
1 parent 9fea1ae commit 3303590

File tree

6 files changed

+522
-4
lines changed

6 files changed

+522
-4
lines changed

docs/observability.asciidoc

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ client.on('response', (err, result) => {
4949
The client emits the following events:
5050
[cols=2*]
5151
|===
52+
|`serialization`
53+
a|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`.
54+
[source,js]
55+
----
56+
client.on('serialization', (err, result) => {
57+
console.log(err, result)
58+
})
59+
----
60+
5261
|`request`
5362
a|Emitted before sending the actual request to {es} _(emitted multiple times in case of retries)_.
5463
[source,js]
@@ -58,6 +67,15 @@ client.on('request', (err, result) => {
5867
})
5968
----
6069

70+
|`deserialization`
71+
a|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)_.
72+
[source,js]
73+
----
74+
client.on('deserialization', (err, result) => {
75+
console.log(err, result)
76+
})
77+
----
78+
6179
|`response`
6280
a|Emitted once {es} response has been received and parsed.
6381
[source,js]
@@ -87,7 +105,7 @@ client.on('resurrect', (err, result) => {
87105

88106
|===
89107

90-
The values of `result` in `request`, `response` and `sniff` will be:
108+
The values of `result` in `serialization`, `request`, `deserialization`, `response` and `sniff` will be:
91109

92110
[source,ts]
93111
----
@@ -127,6 +145,29 @@ request: {
127145
};
128146
----
129147

148+
[discrete]
149+
==== Events order
150+
151+
The event order is described in the following graph, in some edge cases, the order is not guaranteed.
152+
You can find in https://github.com/elastic/elasticsearch-js/blob/master/test/acceptance/events-order.test.js[`test/acceptance/events-order.test.js`] how the order changes based on the situation.
153+
154+
[source]
155+
----
156+
serialization
157+
158+
│ (serialization and compression happens between those two events)
159+
160+
└─▶ request
161+
162+
│ (actual time spent over the wire)
163+
164+
└─▶ deserialization
165+
166+
│ (deserialization and decompression happens between those two events)
167+
168+
└─▶ response
169+
----
170+
130171

131172
[discrete]
132173
=== Correlation id

index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2573,8 +2573,10 @@ declare class Client {
25732573
}
25742574

25752575
declare const events: {
2576-
RESPONSE: string;
2576+
SERIALIZATION: string;
25772577
REQUEST: string;
2578+
DESERIALIZATION: string;
2579+
RESPONSE: string;
25782580
SNIFF: string;
25792581
RESURRECT: string;
25802582
};

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,9 @@ const events = {
314314
RESPONSE: 'response',
315315
REQUEST: 'request',
316316
SNIFF: 'sniff',
317-
RESURRECT: 'resurrect'
317+
RESURRECT: 'resurrect',
318+
SERIALIZATION: 'serialization',
319+
DESERIALIZATION: 'deserialization'
318320
}
319321

320322
module.exports = {

lib/Transport.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ class Transport {
268268
if (!isCompressed) {
269269
response.setEncoding('utf8')
270270
}
271+
272+
this.emit('deserialization', null, result)
271273
response.on('data', onData)
272274
response.on('error', onEnd)
273275
response.on('end', onEnd)
@@ -340,6 +342,7 @@ class Transport {
340342
}
341343
}
342344

345+
this.emit('serialization', null, result)
343346
const headers = Object.assign({}, this.headers, lowerCaseHeaders(options.headers))
344347

345348
if (options.opaqueId !== undefined) {
@@ -354,6 +357,7 @@ class Transport {
354357
try {
355358
params.body = this.serializer.serialize(params.body)
356359
} catch (err) {
360+
this.emit('request', err, result)
357361
process.nextTick(callback, err, result)
358362
return transportReturn
359363
}
@@ -369,6 +373,7 @@ class Transport {
369373
try {
370374
params.body = this.serializer.ndserialize(params.bulkBody)
371375
} catch (err) {
376+
this.emit('request', err, result)
372377
process.nextTick(callback, err, result)
373378
return transportReturn
374379
}
@@ -408,6 +413,7 @@ class Transport {
408413
gzip(params.body, (err, buffer) => {
409414
/* istanbul ignore next */
410415
if (err) {
416+
this.emit('request', err, result)
411417
return callback(err, result)
412418
}
413419
params.headers['content-encoding'] = compression

0 commit comments

Comments
 (0)