Skip to content

Commit 36eaed6

Browse files
szabostevedelvedor
andauthored
[DOCS] Adds Integrations section to Node.JS docs (#1407)
Co-authored-by: Tomas Della Vedova <[email protected]>
1 parent 22ece32 commit 36eaed6

8 files changed

+141
-117
lines changed

docs/advanced-config.asciidoc

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,11 @@
44
If you need to customize the client behavior heavily, you are in the right
55
place! The client enables you to customize the following internals:
66

7-
* `Transport` class
87
* `ConnectionPool` class
98
* `Connection` class
109
* `Serializer` class
1110

12-
13-
[discrete]
14-
==== `Transport`
15-
16-
This class is responsible for performing the request to {es} and handling
17-
errors, it also handles the sniffing.
18-
19-
[source,js]
20-
----
21-
const { Client, Transport } = require('@elastic/elasticsearch')
22-
23-
class MyTransport extends Transport {
24-
request (params, options, callback) {
25-
// your code
26-
}
27-
}
28-
29-
const client = new Client({
30-
Transport: MyTransport
31-
})
32-
----
33-
34-
Sometimes you need to inject a small snippet of your code and then continue to
35-
use the usual client code. In such cases, call `super.method`:
36-
37-
[source,js]
38-
----
39-
class MyTransport extends Transport {
40-
request (params, options, callback) {
41-
// your code
42-
return super.request(params, options, callback)
43-
}
44-
}
45-
----
11+
NOTE: For information about the `Transport` class, refer to <<transport>>.
4612

4713

4814
[discrete]

docs/configuration.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ section, you can see the possible options that you can use to configure it.
99
* <<advanced-config>>
1010
* <<child>>
1111
* <<extend>>
12+
* <<client-testing>>

docs/index.asciidoc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ include::basic-config.asciidoc[]
1111
include::advanced-config.asciidoc[]
1212
include::child.asciidoc[]
1313
include::extend.asciidoc[]
14+
include::testing.asciidoc[]
15+
include::integrations.asciidoc[]
16+
include::observability.asciidoc[]
17+
include::transport.asciidoc[]
18+
include::typescript.asciidoc[]
1419
include::reference.asciidoc[]
1520
include::breaking-changes.asciidoc[]
16-
include::observability.asciidoc[]
1721
include::helpers.asciidoc[]
18-
include::typescript.asciidoc[]
19-
include::testing.asciidoc[]
2022
include::examples/index.asciidoc[]

docs/integrations.asciidoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[[integrations]]
2+
== Integrations
3+
4+
The Client offers the following integration options for you:
5+
6+
* <<observability>>
7+
* <<transport>>
8+
* <<typescript>>

docs/observability.asciidoc

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
[[observability]]
2-
== Observability
2+
=== Observability
33

44
The client does not provide a default logger, but instead it offers an event
55
emitter interfaces to hook into internal events, such as `request` and
66
`response`.
77

8-
Correlating those events can be quite hard, especially if your applications have
9-
a large codebase with many events happening at the same time.
8+
Correlating those events can be hard, especially if your applications have a
9+
large codebase with many events happening at the same time.
1010

1111
To help you with this, the client offers you a correlation id system and other
1212
features. Let's see them in action.
1313

1414

1515
[discrete]
16-
=== Events
16+
==== Events
1717

1818
The client is an event emitter, this means that you can listen for its event and
1919
add additional logic to your code, without need to change the client internals
@@ -105,7 +105,8 @@ client.on('resurrect', (err, result) => {
105105

106106
|===
107107

108-
The values of `result` in `serialization`, `request`, `deserialization`, `response` and `sniff` will be:
108+
The values of `result` in `serialization`, `request`, `deserialization`,
109+
`response` and `sniff` are:
109110

110111
[source,ts]
111112
----
@@ -132,7 +133,7 @@ meta: {
132133
----
133134

134135

135-
While the `result` value in `resurrect` will be:
136+
While the `result` value in `resurrect` is:
136137

137138
[source,ts]
138139
----
@@ -146,10 +147,13 @@ request: {
146147
----
147148

148149
[discrete]
149-
==== Events order
150+
===== Events order
150151

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.
152+
The event order is described in the following graph, in some edge cases, the
153+
order is not guaranteed.
154+
You can find in
155+
https://github.com/elastic/elasticsearch-js/blob/master/test/acceptance/events-order.test.js[`test/acceptance/events-order.test.js`]
156+
how the order changes based on the situation.
153157

154158
[source]
155159
----
@@ -170,11 +174,11 @@ serialization
170174

171175

172176
[discrete]
173-
=== Correlation id
177+
==== Correlation id
174178

175-
Correlating events can be quite hard, especially if there are many events at the
176-
same time. The client offers you an automatic (and configurable) system to help
177-
you handle this problem.
179+
Correlating events can be hard, especially if there are many events at the same
180+
time. The client offers you an automatic (and configurable) system to help you
181+
handle this problem.
178182

179183
[source,js]
180184
----
@@ -204,8 +208,8 @@ client.search({
204208
----
205209

206210

207-
By default the id is an incremental integer, but you can easily configure that
208-
with the `generateRequestId` option:
211+
By default the id is an incremental integer, but you can configure it with the
212+
`generateRequestId` option:
209213

210214
[source,js]
211215
----
@@ -238,7 +242,7 @@ client.search({
238242

239243

240244
[discrete]
241-
=== Context object
245+
==== Context object
242246

243247
Sometimes, you might need to make some custom data available in your events, you
244248
can do that via the `context` option of a request:
@@ -275,8 +279,8 @@ client.search({
275279
----
276280

277281
The context object can also be configured as a global option in the client
278-
configuration. If you provide both, the two context object will be shallow merged,
279-
and the API level object will take precedece.
282+
configuration. If you provide both, the two context objects will be shallow
283+
merged, and the API level object will take precedence.
280284

281285
[source,js]
282286
----
@@ -314,12 +318,12 @@ client.search({
314318

315319

316320
[discrete]
317-
=== Client name
321+
==== Client name
318322

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

324328
[source,js]
325329
----
@@ -368,13 +372,13 @@ child.search({
368372

369373

370374
[discrete]
371-
=== X-Opaque-Id support
375+
==== X-Opaque-Id support
372376

373-
To improve the overall observability, the client offers an easy way to configure
374-
the `X-Opaque-Id` header. If you set the `X-Opaque-Id` in a specific request,
375-
this will allow you to discover this identifier in the
377+
To improve observability, the client offers an easy way to configure the
378+
`X-Opaque-Id` header. If you set the `X-Opaque-Id` in a specific request, this
379+
allows you to discover this identifier in the
376380
https://www.elastic.co/guide/en/elasticsearch/reference/master/logging.html#deprecation-logging[deprecation logs],
377-
help you with https://www.elastic.co/guide/en/elasticsearch/reference/master/index-modules-slowlog.html#_identifying_search_slow_log_origin[identifying search slow log origin]
381+
helps you with https://www.elastic.co/guide/en/elasticsearch/reference/master/index-modules-slowlog.html#_identifying_search_slow_log_origin[identifying search slow log origin]
378382
as well as https://www.elastic.co/guide/en/elasticsearch/reference/master/tasks.html#_identifying_running_tasks[identifying running tasks].
379383

380384
The `X-Opaque-Id` should be configured in each request, for doing that you can

docs/testing.asciidoc

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,58 @@
11
[[client-testing]]
2-
== Testing
2+
=== Testing
33

44
Testing is one of the most important parts of developing an application.
55
The client is very flexible when it comes to testing and is compatible with
66
most testing frameworks (such as https://www.npmjs.com/package/ava[`ava`],
77
which is used in the examples below).
88

9-
If you are using this client, you are very likely working with Elasticsearch,
10-
and one of the first issues you will face is how to test your application.
11-
A perfectly valid solution is to use the real Elasticsearch instance for
12-
testing your application, but you would be doing an integration test,
13-
while you want a unit test.
14-
There are many ways to solve this problem, you could create the database
15-
with docker, or use an in-memory compatible one, but if you are writing
16-
unit tests that can be easily parallelized this will become quite uncomfortable.
17-
A different way of improving your testing experience while doing unit tests
18-
is to use a mock.
19-
20-
The client is designed to be easy to extend and adapt to your needs.
21-
Thanks to its internal architecture it allows you to change some specific
22-
components while keeping the rest of it working as usual.
23-
Each Elasticsearch official client is composed of the following components:
24-
25-
* `API layer`: every Elasticsearch API that you can call
26-
* `Transport`: a component that takes care of preparing a request before sending it and handling all the retry and sniffing strategies
27-
* `ConnectionPool`: Elasticsearch is a cluster and might have multiple nodes, the * `ConnectionPool` takes care of them
28-
* `Serializer`: A class with all the serialization strategies, from the basic JSON to the new line delimited JSON.
9+
If you are using this client, you are most likely working with {es}, and one of
10+
the first issues you face is how to test your application. A perfectly valid
11+
solution is to use the real {es} instance for testing your application, but you
12+
would be doing an integration test, while you want a unit test. There are many
13+
ways to solve this problem, you could create the database with Docker, or use an
14+
in-memory compatible one, but if you are writing unit tests that can be easily
15+
parallelized this becomes quite uncomfortable. A different way of improving your
16+
testing experience while doing unit tests is to use a mock.
17+
18+
The client is designed to be easy to extend and adapt to your needs. Thanks to
19+
its internal architecture it allows you to change some specific components while
20+
keeping the rest of it working as usual. Each {es} official client is composed
21+
of the following components:
22+
23+
* `API layer`: every {es} API that you can call.
24+
* `Transport`: a component that takes care of preparing a request before sending
25+
it and handling all the retry and sniffing strategies.
26+
* `ConnectionPool`: {es} is a cluster and might have multiple nodes, the
27+
`ConnectionPool` takes care of them.
28+
* `Serializer`: A class with all the serialization strategies, from the basic
29+
JSON to the new line delimited JSON.
2930
* `Connection`: The actual HTTP library.
3031

31-
The best way to mock Elasticsearch with the official clients is to replace
32-
the `Connection` component since it has very few responsibilities and
33-
it does not interact with other internal components other than getting
34-
requests and returning responses.
32+
The best way to mock {es} with the official clients is to replace the
33+
`Connection` component since it has very few responsibilities and it does not
34+
interact with other internal components other than getting requests and
35+
returning responses.
3536

3637

3738
[discrete]
38-
=== `@elastic/elasticsearch-mock`
39+
==== `@elastic/elasticsearch-mock`
3940

40-
Writing each time a mock for your test can be annoying and error-prone,
41-
so we have built a simple yet powerful mocking library specifically designed
42-
for this client, and you can install it with the following command:
41+
Writing each time a mock for your test can be annoying and error-prone, so we
42+
have built a simple yet powerful mocking library specifically designed for this
43+
client, and you can install it with the following command:
4344

4445
[source,sh]
4546
----
4647
npm install @elastic/elasticsearch-mock --save-dev
4748
----
4849

49-
With this library you can easily create custom mocks for any request you can
50-
send to Elasticsearch. It offers a simple and intuitive API and it mocks only
51-
the HTTP layer, leaving the rest of the client working as usual.
50+
With this library you can create custom mocks for any request you can send to
51+
{es}. It offers a simple and intuitive API and it mocks only the HTTP layer,
52+
leaving the rest of the client working as usual.
5253

53-
Before showing all of its features, and what you can do with it, let’s see an example:
54+
Before showing all of its features, and what you can do with it, let’s see an
55+
example:
5456

5557
[source,js]
5658
----
@@ -73,16 +75,16 @@ mock.add({
7375
client.info(console.log)
7476
----
7577

76-
As you can see it works closely with the client itself, once you have created
77-
a new instance of the mock library you just need to call the mock.getConnection()
78-
method and pass its result to the Connection option of the client.
79-
From now on, every request will be handled by the mock library, and the HTTP
80-
layer will never be touched. As a result, your test will be significantly faster
81-
and you will be able to easily parallelize them!
78+
As you can see it works closely with the client itself, once you have created a
79+
new instance of the mock library you just need to call the mock.getConnection()
80+
method and pass its result to the Connection option of the client. From now on,
81+
every request is handled by the mock library, and the HTTP layer will never be
82+
touched. As a result, your test is significantly faster and you are able to
83+
easily parallelize them!
8284

83-
The library allows you to write both “strict” and “loose” mocks, which means
84-
that you can write a mock that will handle a very specific request or be looser
85-
and handle a group of request, let’s see this in action:
85+
The library allows you to write both “strict” and “loose” mocks, which means
86+
that you can write a mock that handles a very specific request or be looser and
87+
handle a group of request, let’s see this in action:
8688

8789
[source,js]
8890
----
@@ -112,9 +114,9 @@ mock.add({
112114
})
113115
----
114116

115-
In the example above every search request will get the first response,
116-
while every search request that uses the query described in the second mock,
117-
will get the second response.
117+
In the example above, every search request gets the first response, while every
118+
search request that uses the query described in the second mock gets the second
119+
response.
118120

119121
You can also specify dynamic paths:
120122

@@ -150,5 +152,6 @@ mock.add({
150152
})
151153
----
152154

153-
We have seen how simple is mocking Elasticsearch and testing your application,
154-
you can find many more features and examples in the https://github.com/elastic/elasticsearch-js-mock[module documentation].
155+
We have seen how simple is mocking {es} and testing your application, you can
156+
find many more features and examples in the
157+
https://github.com/elastic/elasticsearch-js-mock[module documentation].

docs/transport.asciidoc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[[transport]]
2+
=== Transport
3+
4+
This class is responsible for performing the request to {es} and handling
5+
errors, it also handles sniffing.
6+
7+
[source,js]
8+
----
9+
const { Client, Transport } = require('@elastic/elasticsearch')
10+
11+
class MyTransport extends Transport {
12+
request (params, options, callback) {
13+
// your code
14+
}
15+
}
16+
17+
const client = new Client({
18+
Transport: MyTransport
19+
})
20+
----
21+
22+
Sometimes you need to inject a small snippet of your code and then continue to
23+
use the usual client code. In such cases, call `super.method`:
24+
25+
[source,js]
26+
----
27+
class MyTransport extends Transport {
28+
request (params, options, callback) {
29+
// your code
30+
return super.request(params, options, callback)
31+
}
32+
}
33+
----
34+

0 commit comments

Comments
 (0)