Skip to content

Commit 6443938

Browse files
authored
Add docs for updated Undici Agent functionality in Transport (#2957)
1 parent 3cfeca7 commit 6443938

File tree

9 files changed

+88
-25
lines changed

9 files changed

+88
-25
lines changed

docs/reference/advanced-config.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ For information about the `Transport` class, refer to [Transport](/reference/tra
1919

2020
## `ConnectionPool` [_connectionpool]
2121

22-
This class is responsible for keeping in memory all the {{es}} Connection that you are using. There is a single Connection for every node. The connection pool handles the resurrection strategies and the updates of the pool.
22+
This class is responsible for keeping in memory all the {{es}} connections that you are using. There is a single `Connection` for every node. The connection pool handles the resurrection strategies and the updates of the pool.
2323

2424
```js
2525
const { Client, ConnectionPool } = require('@elastic/elasticsearch')
@@ -41,7 +41,7 @@ const client = new Client({
4141

4242
## `Connection` [_connection]
4343

44-
This class represents a single node, it holds every information we have on the node, such as roles, id, URL, custom headers and so on. The actual HTTP request is performed here, this means that if you want to swap the default HTTP client (Node.js core), you should override the `request` method of this class.
44+
This class represents a single node, it holds every information we have on the node, such as roles, id, URL, custom headers and so on. The actual HTTP request is performed here, this means that if you want to swap the default HTTP client ([Undici `Pool`](https://undici.nodejs.org/#/docs/api/Pool.md)), you should override the `request` method of this class.
4545

4646
```js
4747
const { Client, BaseConnection } = require('@elastic/elasticsearch')
@@ -59,6 +59,10 @@ const client = new Client({
5959
})
6060
```
6161

62+
`@elastic/transport` provides two `Connection` implementations:
63+
64+
- `UndiciConnection`: manages HTTP connections using [Undici](https://undici.nodejs.org/), Node.js's high-performance HTTP client implementation; this is the default value of `Connection` and is recommended unless you have a use case that is not yet supported by Undici or `UndiciConnection`
65+
- `HttpConnection`: manages HTTP connections using [the `http` package](https://nodejs.org/api/http.html) from Node.js's standard library
6266

6367
## `Serializer` [_serializer]
6468

@@ -175,5 +179,5 @@ try {
175179
176180
## Migrate to v8 [_migrate_to_v8]
177181
178-
The Node.js client can be configured to emit an HTTP header `Accept: application/vnd.elasticsearch+json; compatible-with=7` which signals to Elasticsearch that the client is requesting `7.x` version of request and response bodies. This allows for upgrading from 7.x to 8.x version of Elasticsearch without upgrading everything at once. Elasticsearch should be upgraded first after the compatibility header is configured and clients should be upgraded second. To enable to setting, configure the environment variable `ELASTIC_CLIENT_APIVERSIONING` to `true`.
182+
The Node.js client can be configured to emit an HTTP header `Accept: application/vnd.elasticsearch+json; compatible-with=7` which signals to {{es}} that the client is requesting `7.x` version of request and response bodies. This allows for upgrading from 7.x to 8.x version of {{es}} without upgrading everything at once. {{es}} should be upgraded first after the compatibility header is configured and clients should be upgraded second. To enable to setting, configure the environment variable `ELASTIC_CLIENT_APIVERSIONING` to `true`.
179183

docs/reference/basic-config.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,21 @@ const client = new Client({
184184
})
185185
```
186186

187-
### `agent`
187+
### `agent` [agent-config]
188188

189-
Type: `http.AgentOptions, function`<br>
189+
Type: `http.AgentOptions, undici.PoolOptions, function, false`<br>
190190
Default: `null`
191191

192-
http agent [options](https://nodejs.org/api/http.html#http_new_agent_options), or a function that returns an actual http agent instance. If you want to disable the http agent use entirely (and disable the `keep-alive` feature), set the agent to `false`.
192+
If using the default `UndiciConnection` from `@elastic/transport`, this value can be:
193+
194+
- an [Undici `PoolOptions` object](https://undici.nodejs.org/#/docs/api/Pool?id=parameter-pooloptions)
195+
- a function that receives all connection-related options and returns an [Undici `Agent`](https://undici.nodejs.org/#/docs/api/Agent.md) instance (or any other object that follows [Undici's `Dispatch.request()`](https://undici.nodejs.org/#/docs/api/Dispatcher?id=dispatcherrequestoptions-callback) conventions)
196+
197+
If using the legacy `HttpConnection` from `@elastic/transport`, this value can be:
198+
199+
- [the options object passed to an `http.Agent`](https://nodejs.org/api/http.html#new-agentoptions)
200+
- a function that returns an `http.Agent` (and thus also an [`https.Agent`](https://nodejs.org/api/https.html#class-httpsagent), or any implementation that follows the same conventions, like [`hpagent`](https://www.npmjs.com/package/hpagent))
201+
- `false` to disable all agent usage, including the `keep-alive` feature
193202

194203
```js
195204
const client = new Client({
@@ -211,6 +220,10 @@ const client = new Client({
211220
})
212221
```
213222

223+
::::{warning}
224+
If you have set [the `agent` option](/reference/basic-config.md#agent-config) on your client instance to a function and are using `UndiciConnection`&mdash;the default [`Connection`](/reference/advanced-config.md#_connection) value starting in 8.0&mdash;all `caFingerprint` and `tls` options will be ignored. It is your responsibility to ensure that your custom agent will properly verify HTTPS connections.
225+
::::
226+
214227
### `nodeFilter`
215228

216229
Type: `function`

docs/reference/client-helpers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ To create a new instance of the Bulk helper, access it as shown in the example a
9393
```
9494

9595
`onSuccess`
96-
: A function that is called for each successful operation in the bulk request, which includes the result from Elasticsearch along with the original document that was sent, or `null` for delete operations.
96+
: A function that is called for each successful operation in the bulk request, which includes the result from {{es}} along with the original document that was sent, or `null` for delete operations.
9797

9898
```js
9999
const b = client.helpers.bulk({
@@ -307,7 +307,7 @@ console.log(result)
307307

308308
Added in `v8.8.2`
309309

310-
If you need to modify documents in your datasource before it is sent to Elasticsearch, you can return an array in the `onDocument` function rather than an operation object. The first item in the array must be the operation object, and the second item must be the document or partial document object as you’d like it to be sent to Elasticsearch.
310+
If you need to modify documents in your datasource before it is sent to {{es}}, you can return an array in the `onDocument` function rather than an operation object. The first item in the array must be the operation object, and the second item must be the document or partial document object as you’d like it to be sent to {{es}}.
311311

312312
```js
313313
const { Client } = require('@elastic/elasticsearch')

docs/reference/connecting.md

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ When you start {{es}} for the first time you’ll see a distinct block like the
7777

7878
Depending on the circumstances there are two options for verifying the HTTPS connection, either verifying with the CA certificate itself or via the HTTP CA certificate fingerprint.
7979

80+
::::{warning}
81+
If you have set [the `agent` option](/reference/basic-config.md#agent-config) on your client instance to a function and are using `UndiciConnection`&mdash;the default `Connection` value starting in 8.0&mdash;all `caFingerprint` and `tls` options will be ignored. It is your responsibility to ensure that your custom agent will properly verify HTTPS connections.
82+
::::
83+
8084
### TLS configuration [auth-tls]
8185

8286
The generated root CA certificate can be found in the `certs` directory in your {{es}} config location (`$ES_CONF_PATH/certs/http_ca.crt`). If you’re running {{es}} in Docker there is [additional documentation for retrieving the CA certificate](docs-content://deploy-manage/deploy/self-managed/install-elasticsearch-with-docker.md).
@@ -332,7 +336,7 @@ The supported request specific options are:
332336
| Option | Description |
333337
| --- | ----------- |
334338
| `ignore` | `number[]` -  HTTP status codes which should not be considered errors for this request.<br>*Default:* `null` |
335-
| `requestTimeout` | `number` or `string` - Max request timeout for the request in milliseconds. This overrides the client default, which is to not time out at all. See [Elasticsearch best practices for HTML clients](elasticsearch://reference/elasticsearch/configuration-reference/networking-settings.md#_http_client_configuration) for more info.<br>_Default:_ No timeout |
339+
| `requestTimeout` | `number` or `string` - Max request timeout for the request in milliseconds. This overrides the client default, which is to not time out at all. See [{{es}} best practices for HTML clients](elasticsearch://reference/elasticsearch/configuration-reference/networking-settings.md#_http_client_configuration) for more info.<br>_Default:_ No timeout |connecting
336340
| `retryOnTimeout` | `boolean` - Retry requests that have timed out.*Default:* `false` |
337341
| `maxRetries` | `number` - Max number of retries for the request, it overrides the client default.<br>*Default:* `3` |
338342
| `compression` | `string` or `boolean` - Enables body compression for the request.<br>*Options:* `false`, `'gzip'`<br>*Default:* `false` |
@@ -477,9 +481,9 @@ You can find the errors exported by the client in the table below.
477481

478482
## Keep-alive connections [keep-alive]
479483

480-
By default, the client uses persistent, keep-alive connections to reduce the overhead of creating a new HTTP connection for each Elasticsearch request. If you are using the default `UndiciConnection` connection class, it maintains a pool of 256 connections with a keep-alive of 10 minutes. If you are using the legacy `HttpConnection` connection class, it maintains a pool of 256 connections with a keep-alive of 1 minute.
484+
By default, the client uses persistent, keep-alive connections to reduce the overhead of creating a new HTTP connection for each {{es}} request. If you are using the default `UndiciConnection` connection class, it maintains a pool of 256 connections with a keep-alive of 10 minutes. If you are using the legacy `HttpConnection` connection class, it maintains a pool of 256 connections with a keep-alive of 1 minute.
481485

482-
If you need to disable keep-alive connections, you can override the HTTP agent with your preferred [HTTP agent options](https://nodejs.org/api/http.md#http_new_agent_options):
486+
If you need to disable keep-alive connections, you can override the HTTP agent with your preferred [HTTP agent options](/reference/basic-config.md#agent-config):
483487

484488
```js
485489
const client = new Client({
@@ -500,6 +504,48 @@ const client = new Client({
500504
})
501505
```
502506

507+
## Managing open connection limits [limit-open-connections]
508+
509+
Starting in client 9.0, when using `@elastic/transport` 9.2.0 or later, you can provide a custom `agent` function to share a singleton [Undici `Agent`](https://undici.nodejs.org/#/docs/api/Agent.md) instance that can enforce client-wide connection limits.
510+
511+
```typescript
512+
import { Agent } from 'undici'
513+
import { HttpConnection } from '@elastic/transport'
514+
515+
// `maxOrigins * connections` (50 in this case) is the total connection limit
516+
const maxSocketAgent = new Agent({
517+
keepAliveTimeout: 1000,
518+
maxOrigins: 5,
519+
connections: 10
520+
})
521+
522+
const client = new Client({
523+
node: '...',
524+
auth: { ... },
525+
agent: () => maxSocketAgent
526+
})
527+
```
528+
529+
If using the legacy `HttpConnection`, you can use an [`Agent`](https://nodejs.org/api/https.html#class-httpsagent) singleton that enforces `maxTotalSockets`:
530+
531+
```typescript
532+
import { Agent } from 'node:http'
533+
import { HttpConnection } from '@elastic/transport'
534+
535+
const maxSocketAgent = new Agent({
536+
keepAlive: true,
537+
keepAliveMsecs: 1000,
538+
maxTotalSockets: 50
539+
})
540+
541+
const client = new Client({
542+
node: '...',
543+
auth: { ... },
544+
Connection: HttpConnection,
545+
agent: () => maxSocketAgent
546+
})
547+
```
548+
503549
## Closing a client’s connections [close-connections]
504550

505551
If you would like to close all open connections being managed by an instance of the client, use the `close()` function:
@@ -513,4 +559,4 @@ client.close();
513559

514560
## Automatic product check [product-check]
515561

516-
Since v7.14.0, the client performs a required product check before the first call. This pre-flight product check allows the client to establish the version of Elasticsearch that it is communicating with. The product check requires one additional HTTP request to be sent to the server as part of the request pipeline before the main API call is sent. In most cases, this will succeed during the very first API call that the client sends. Once the product check completes, no further product check HTTP requests are sent for subsequent API calls.
562+
Since v7.14.0, the client performs a required product check before the first call. This pre-flight product check allows the client to establish the version of {{es}} that it is communicating with. The product check requires one additional HTTP request to be sent to the server as part of the request pipeline before the main API call is sent. In most cases, this will succeed during the very first API call that the client sends. Once the product check completes, no further product check HTTP requests are sent for subsequent API calls.

docs/reference/getting-started.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mapped_pages:
66

77
# Getting started [getting-started-js]
88

9-
This page guides you through the installation process of the Node.js client, shows you how to instantiate the client, and how to perform basic Elasticsearch operations with it.
9+
This page guides you through the installation process of the Node.js client, shows you how to instantiate the client, and how to perform basic {{es}} operations with it.
1010

1111

1212
### Requirements [_requirements]
@@ -28,7 +28,7 @@ Refer to the [*Installation*](/reference/installation.md) page to learn more.
2828

2929
### Connecting [_connecting]
3030

31-
You can connect to the Elastic Cloud using an API key and the Elasticsearch endpoint.
31+
You can connect to the Elastic Cloud using an API key and the {{es}} endpoint.
3232

3333
```js
3434
const { Client } = require('@elastic/elasticsearch')
@@ -43,9 +43,9 @@ const client = new Client({
4343
})
4444
```
4545

46-
Your Elasticsearch endpoint can be found on the **My deployment** page of your deployment:
46+
Your {{es}} endpoint can be found on the **My deployment** page of your deployment:
4747

48-
![Finding Elasticsearch endpoint](images/es-endpoint.jpg)
48+
![Finding {{es}} endpoint](images/es-endpoint.jpg)
4949

5050
You can generate an API key on the **Management** page under Security.
5151

@@ -56,7 +56,7 @@ For other connection options, refer to the [*Connecting*](/reference/connecting.
5656

5757
### Operations [_operations]
5858

59-
Time to use Elasticsearch! This section walks you through the basic, and most important, operations of Elasticsearch.
59+
Time to use {{es}}! This section walks you through the basic, and most important, operations of {{es}}.
6060

6161

6262
#### Creating an index [_creating_an_index]

docs/reference/observability.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mapped_pages:
55

66
# Observability [observability]
77

8-
To observe and measure Elasticsearch client usage, several client features are provided.
8+
To observe and measure {{es}} client usage, several client features are provided.
99

1010
First, as of 8.15.0, the client provides native support for OpenTelemetry, which allows you to send client usage data to any endpoint that supports OpenTelemetry without having to make any changes to your JavaScript codebase.
1111

@@ -17,9 +17,9 @@ All of these observability features are documented below.
1717

1818
## OpenTelemetry [_opentelemetry]
1919

20-
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`.
20+
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 {{es}}](https://opentelemetry.io/docs/specs/semconv/database/elasticsearch/) except for `db.query.text`.
2121

22-
To start sending Elasticsearch trace data to your OpenTelemetry endpoint, follow [OpenTelemetry’s zero-code instrumentation guide](https://opentelemetry.io/docs/zero-code/js/), or the following steps:
22+
To start sending {{es}} trace data to your OpenTelemetry endpoint, follow [OpenTelemetry’s zero-code instrumentation guide](https://opentelemetry.io/docs/zero-code/js/), or the following steps:
2323

2424
1. Install `@opentelemetry/api` and `@opentelemetry/auto-instrumentations-node` as Node.js dependencies
2525
2. Export the following environment variables with the appropriate values:

docs/reference/timeout-best-practices.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ Starting in 9.0.0, this client is configured to not time out any HTTP request by
99

1010
Prior to 9.0, this client was configured by default to operate like many HTTP client libraries do, by using a relatively short (30 second) timeout on all requests sent to {{es}}, raising a `TimeoutError` when that time period elapsed without receiving a response.
1111

12-
If you need to set timeouts on Elasticsearch requests, setting the `requestTimeout` value to a millisecond value will cause this client to operate as it did prior to 9.0.
12+
If you need to set timeouts on {{es}} requests, setting the `requestTimeout` value to a millisecond value will cause this client to operate as it did prior to 9.0.
1313

docs/reference/typescript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mapped_pages:
55

66
# TypeScript support [typescript]
77

8-
The client offers a first-class support for TypeScript, shipping a complete set of type definitions of Elasticsearch’s API surface.
8+
The client offers a first-class support for TypeScript, shipping a complete set of type definitions of {{es}}'s API surface.
99

1010
The types are not 100% complete yet. Some APIs are missing (the newest ones, e.g. EQL), and others may contain some errors, but we are continuously pushing fixes & improvements. Contribute type fixes and improvements to [elasticsearch-specification github repository](https://github.com/elastic/elasticsearch-specification).
1111

src/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export interface ClientOptions {
164164
* @defaultValue true */
165165
enableMetaHeader?: boolean
166166
/** @property cloud Custom configuration for connecting to Elastic Cloud, in lieu of a `node` or `nodes` configuration
167-
* @remarks Read https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/client-connecting.html#client-usage for more details
167+
* @remarks Read https://www.elastic.co/docs/reference/elasticsearch/clients/javascript/connecting#client-usage for more details
168168
* @defaultValue null */
169169
cloud?: {
170170
id: string
@@ -182,7 +182,7 @@ export interface ClientOptions {
182182
* @defaultValue null */
183183
maxCompressedResponseSize?: number
184184
/** @property redaction Options for how to redact potentially sensitive data from metadata attached to `Error` objects
185-
* @remarks Read https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/advanced-config.html#redaction for more details
185+
* @remarks Read https://www.elastic.co/docs/reference/elasticsearch/clients/javascript/advanced-config#redaction for more details
186186
* @defaultValue Configuration that will replace known sources of sensitive data */
187187
redaction?: RedactionOptions
188188
/** @property serverMode Setting to "serverless" will change some default behavior, like enabling compression and disabling features that assume the possibility of multiple Elasticsearch nodes.
@@ -443,7 +443,7 @@ export default class Client extends API {
443443

444444
/**
445445
* Creates a child client instance that shared its connection pool with the parent client
446-
* @see {@link https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/child.html}
446+
* @see {@link https://www.elastic.co/docs/reference/elasticsearch/clients/javascript/child}
447447
*/
448448
child (opts: ClientOptions): Client {
449449
// Merge the new options with the initial ones

0 commit comments

Comments
 (0)