@healthzkit/elasticsearch 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +122 -0
- package/dist/elasticsearch-1igrfgjc.mjs +1 -0
- package/dist/elasticsearch-Cp7sTdkM.d.mts +27 -0
- package/dist/elasticsearch.d.mts +2 -0
- package/dist/elasticsearch.mjs +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +1 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# @healthzkit/elasticsearch
|
|
2
|
+
|
|
3
|
+
Elasticsearch health adapters for [healthzkit](https://healthzkit.dev). The adapter calls **`cluster.health()`** and maps cluster status to probe status: **`green`** → `ok`, **`yellow`** → `degraded`, **`red`** → `fail`.
|
|
4
|
+
|
|
5
|
+
Install **`@elastic/elasticsearch`** as a peer dependency (v8 or v9).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install healthzkit @healthzkit/elasticsearch @elastic/elasticsearch
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The package is ESM-only (`"type": "module"`).
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createHealthKit } from "healthzkit";
|
|
19
|
+
import { elasticsearchAdapter } from "@healthzkit/elasticsearch";
|
|
20
|
+
|
|
21
|
+
const kit = createHealthKit({
|
|
22
|
+
checks: [
|
|
23
|
+
{
|
|
24
|
+
name: "elasticsearch",
|
|
25
|
+
type: ["readiness"],
|
|
26
|
+
adapter: elasticsearchAdapter({
|
|
27
|
+
config: { node: process.env.ELASTICSEARCH_URL! },
|
|
28
|
+
}),
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Use a subpath import when you want a dedicated entry for bundlers:
|
|
35
|
+
|
|
36
|
+
| Import | Exports |
|
|
37
|
+
| ----------------------------------------- | ---------------------- |
|
|
38
|
+
| `@healthzkit/elasticsearch` | `elasticsearchAdapter` |
|
|
39
|
+
| `@healthzkit/elasticsearch/elasticsearch` | `elasticsearchAdapter` |
|
|
40
|
+
|
|
41
|
+
Both resolve to the same factory.
|
|
42
|
+
|
|
43
|
+
## `elasticsearchAdapter`
|
|
44
|
+
|
|
45
|
+
**Peer:** `@elastic/elasticsearch` >= 8 or >= 9.
|
|
46
|
+
|
|
47
|
+
Each check runs **`client.cluster.health()`** and records round-trip latency.
|
|
48
|
+
|
|
49
|
+
### Config
|
|
50
|
+
|
|
51
|
+
Pass **`config`** as [`ClientOptions`](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/client-connecting.html) for the official client. The adapter lazily imports **`@elastic/elasticsearch`**, creates a shared **`Client`**, and reuses it across checks. When **`config`** is omitted, the default node is **`http://localhost:9200`**.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { elasticsearchAdapter } from "@healthzkit/elasticsearch";
|
|
55
|
+
|
|
56
|
+
const adapter = elasticsearchAdapter({
|
|
57
|
+
config: {
|
|
58
|
+
node: process.env.ELASTICSEARCH_URL ?? "http://localhost:9200",
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Existing client
|
|
64
|
+
|
|
65
|
+
Pass **`client`** as an existing **`Client`** instance. The adapter reuses that client across checks (no internal lifecycle management).
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { Client } from "@elastic/elasticsearch";
|
|
69
|
+
import { elasticsearchAdapter } from "@healthzkit/elasticsearch/elasticsearch";
|
|
70
|
+
|
|
71
|
+
const client = new Client({ node: process.env.ELASTICSEARCH_URL! });
|
|
72
|
+
|
|
73
|
+
const adapter = elasticsearchAdapter({
|
|
74
|
+
client,
|
|
75
|
+
metadata: async (c) => {
|
|
76
|
+
const info = await c.info();
|
|
77
|
+
return { version: info.version.number };
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Provide **`config`** or **`client`**, not both.
|
|
83
|
+
|
|
84
|
+
## Options
|
|
85
|
+
|
|
86
|
+
| Option | Description |
|
|
87
|
+
| -------------- | ----------------------------------------------------------------------------------------------------- |
|
|
88
|
+
| **`config`** | `ClientOptions` for a lazily created client. Default node: `http://localhost:9200`. |
|
|
89
|
+
| **`client`** | Existing `@elastic/elasticsearch` `Client`. |
|
|
90
|
+
| **`metadata`** | Optional `(client) => Record<string, unknown>` (sync or async) merged into metadata with `latencyMs`. |
|
|
91
|
+
|
|
92
|
+
## Check result
|
|
93
|
+
|
|
94
|
+
On success (cluster status **`green`**):
|
|
95
|
+
|
|
96
|
+
```json
|
|
97
|
+
{
|
|
98
|
+
"status": "ok",
|
|
99
|
+
"metadata": {
|
|
100
|
+
"latencyMs": 18,
|
|
101
|
+
"clusterStatus": "green",
|
|
102
|
+
"clusterName": "docker-cluster",
|
|
103
|
+
"numberOfNodes": 3
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
When the cluster reports **`yellow`**, the check status is **`degraded`** (metadata still includes `clusterStatus`, `clusterName`, and `numberOfNodes`). When the cluster is **`red`**, the check status is **`fail`**.
|
|
109
|
+
|
|
110
|
+
On transport or API errors, `status` is **`fail`** and `error` is set (see [healthzkit](https://healthzkit.dev) for how that rolls up into probe responses).
|
|
111
|
+
|
|
112
|
+
## Development (this repo)
|
|
113
|
+
|
|
114
|
+
From the package directory:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
vp install
|
|
118
|
+
vp test
|
|
119
|
+
vp pack
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
See the repo root `AGENTS.md` for Vite+ / `vp` conventions.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e){return{status:`fail`,error:e instanceof Error?e:Error(String(e))}}function t(t){let n=null,r=null;async function i(){return`client`in t&&t.client?t.client:n||(r||=(async()=>{try{let{Client:e}=await import(`@elastic/elasticsearch`),r=new e(t.config??{node:`http://localhost:9200`});return n=r,r}finally{r=null}})(),r)}return{async check(){try{let e=await i(),n=Date.now(),r=await e.cluster.health(),a=Date.now()-n,o=r.status,s=o===`red`?`fail`:o===`yellow`?`degraded`:`ok`,c=t.metadata?t.metadata(e):void 0,l=c instanceof Promise?await c:c;return{status:s,metadata:{latencyMs:a,clusterStatus:o,clusterName:r.cluster_name,numberOfNodes:r.number_of_data_nodes,...l}}}catch(t){return e(t)}}}}export{t};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Client } from "@elastic/elasticsearch";
|
|
2
|
+
import { ClientOptions } from "@elastic/elasticsearch/lib/client";
|
|
3
|
+
import { HealthAdapter } from "healthzkit";
|
|
4
|
+
|
|
5
|
+
//#region src/shared.d.ts
|
|
6
|
+
type MetadataFn<TClient> = (client: TClient) => Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
7
|
+
interface BaseElasticsearchOptions<TClient> {
|
|
8
|
+
/**
|
|
9
|
+
* Optional function to populate metadata in the check result.
|
|
10
|
+
* Receives the resolved client so you can run additional operations.
|
|
11
|
+
*/
|
|
12
|
+
metadata?: MetadataFn<TClient>;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/elasticsearch.d.ts
|
|
16
|
+
interface ElasticsearchAdapterOptionsWithClient extends BaseElasticsearchOptions<Client> {
|
|
17
|
+
client: Client;
|
|
18
|
+
config?: never;
|
|
19
|
+
}
|
|
20
|
+
interface ElasticsearchAdapterOptionsWithConfig extends BaseElasticsearchOptions<Client> {
|
|
21
|
+
config?: ClientOptions;
|
|
22
|
+
client?: never;
|
|
23
|
+
}
|
|
24
|
+
type ElasticsearchAdapterOptions = ElasticsearchAdapterOptionsWithClient | ElasticsearchAdapterOptionsWithConfig;
|
|
25
|
+
declare function elasticsearchAdapter(options: ElasticsearchAdapterOptions): HealthAdapter;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { elasticsearchAdapter as i, ElasticsearchAdapterOptionsWithClient as n, ElasticsearchAdapterOptionsWithConfig as r, ElasticsearchAdapterOptions as t };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { i as elasticsearchAdapter, n as ElasticsearchAdapterOptionsWithClient, r as ElasticsearchAdapterOptionsWithConfig, t as ElasticsearchAdapterOptions } from "./elasticsearch-Cp7sTdkM.mjs";
|
|
2
|
+
export { ElasticsearchAdapterOptions, ElasticsearchAdapterOptionsWithClient, ElasticsearchAdapterOptionsWithConfig, elasticsearchAdapter };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{t as e}from"./elasticsearch-1igrfgjc.mjs";export{e as elasticsearchAdapter};
|
package/dist/index.d.mts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{t as e}from"./elasticsearch-1igrfgjc.mjs";export{e as elasticsearchAdapter};
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@healthzkit/elasticsearch",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "AGPL-3.0-only",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/alasti-company/healthzkit.dev",
|
|
8
|
+
"directory": "packages/elasticsearch"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": "./dist/index.mjs",
|
|
16
|
+
"./elasticsearch": "./dist/elasticsearch.mjs",
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "vp pack",
|
|
24
|
+
"dev": "vp pack --watch",
|
|
25
|
+
"test": "vp test",
|
|
26
|
+
"check": "vp check",
|
|
27
|
+
"prepublishOnly": "vp run build"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@elastic/elasticsearch": "^9.4.1",
|
|
31
|
+
"@types/node": "^25.6.2",
|
|
32
|
+
"@typescript/native-preview": "7.0.0-dev.20260509.2",
|
|
33
|
+
"bumpp": "^11.1.0",
|
|
34
|
+
"healthzkit": "workspace:*",
|
|
35
|
+
"typescript": "^6.0.3",
|
|
36
|
+
"vite-plus": "^0.1.20"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@elastic/elasticsearch": ">=9.0.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependenciesMeta": {
|
|
42
|
+
"@elastic/elasticsearch": {
|
|
43
|
+
"optional": true
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|