ITADN

bug(opensearch): putIndexTemplate crashes — OS v2 client uses ES v7 body API, flat params discarded

#5791Closedborgius 创建于 2026-04-05
B
borgiuscommented
## Summary When `NANGO_LOGS_ES_TYPE=opensearch` the server crashes at startup with `failed_to_init_elasticsearch` because `client.indices.putIndexTemplate()` silently sends an empty request body to OpenSearch. ## Root cause `@opensearch-project/opensearch` v2 is based on the ES **v7** client API where the request body must be passed as a `body` property: ```ts // OS v2 / ES v7 style ✅ client.indices.putIndexTemplate({ name: 'tmpl', body: { index_patterns: ['...'], template: {...} } }) ``` But `migrateOpenSearch()` in `packages/logs/lib/es/helpers.ts` uses the **ES v8** flat-param style (no `body` wrapper): ```ts // ES v8 style — body params ignored by OS v2 client ❌ await client.indices.putIndexTemplate({ name: `${index.index}-template`, index_patterns: [`${index.index}*`], template: { settings: {...}, mappings: index.mappings!, aliases: { [index.index]: {} } } }); ``` Because `index_patterns` and `template` are not inside `body`, the OS v2 client discards them. The HTTP call reaches OpenSearch with an empty JSON body `{}` and the server responds: ```json {"error":{"reason":"Required [index_patterns]"},"status":400} ``` This `ResponseError` is caught in `migrateMapping()` and promoted to a fatal crash. ## Reproduction Start the stack with: ``` NANGO_LOGS_ENABLED=true NANGO_LOGS_ES_TYPE=opensearch ``` Observed log output: ``` info Migrating OpenSearch index "20240528_messages"... info Updating ISM policy "..." info updating index template "20240528_messages"... error failed_to_init_elasticsearch error ❌ OpenSearch initialization failed ``` Confirmed by direct curl — empty body returns 400: ```sh curl -X PUT 'https://<endpoint>/_index_template/test' -d '{}' -H 'Content-Type: application/json' # -> {"error":{"reason":"Required [index_patterns]"},"status":400} ``` ## Fix Use `(client as any).transport.request()` for `putIndexTemplate` in the OpenSearch branch (as already done for the ISM policy call), or pass body params via the `body` key when calling the OS v2 client. Affected calls in `helpers.ts`: - `client.indices.putIndexTemplate()` — **broken**, body discarded by OS v2 client - `client.indices.existsIndexTemplate()` — path-only param, likely fine - `client.indices.exists()` — path-only param, likely fine ## Environment - `@opensearch-project/opensearch`: ^2.12.0 - OpenSearch cluster: 3.3.2 (Aiven)
关闭于 2026-04-05 1 条评论