From acbcb032bd84e7abfe6f2690016404ceeda9f1b7 Mon Sep 17 00:00:00 2001 From: Josh Mock Date: Mon, 3 Feb 2025 12:41:50 -0600 Subject: [PATCH] Report correct transport connection type in telemetry Fixes #2324 --- src/client.ts | 9 ++++++++- test/unit/client.test.ts | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index fcbc5d35b..7f9f8fabe 100644 --- a/src/client.ts +++ b/src/client.ts @@ -287,7 +287,14 @@ export default class Client extends API { } if (options.enableMetaHeader) { - options.headers['x-elastic-client-meta'] = `es=${clientVersion},js=${nodeVersion},t=${transportVersion},hc=${nodeVersion}` + let clientMeta = `es=${clientVersion},js=${nodeVersion},t=${transportVersion}` + if (options.Connection === UndiciConnection) { + clientMeta += `,un=${nodeVersion}` + } else { + // assumes HttpConnection + clientMeta += `,hc=${nodeVersion}` + } + options.headers['x-elastic-client-meta'] = clientMeta } this.name = options.name diff --git a/test/unit/client.test.ts b/test/unit/client.test.ts index fc2af683c..2e64e5927 100644 --- a/test/unit/client.test.ts +++ b/test/unit/client.test.ts @@ -25,7 +25,7 @@ import FakeTimers from '@sinonjs/fake-timers' import { buildServer, connection } from '../utils' import { Client, errors } from '../..' import * as symbols from '@elastic/transport/lib/symbols' -import { BaseConnectionPool, CloudConnectionPool, WeightedConnectionPool } from '@elastic/transport' +import { BaseConnectionPool, CloudConnectionPool, WeightedConnectionPool, HttpConnection } from '@elastic/transport' let clientVersion: string = require('../../package.json').version // eslint-disable-line if (clientVersion.includes('-')) { @@ -404,6 +404,44 @@ test('Meta header disabled', async t => { await client.transport.request({ method: 'GET', path: '/' }) }) +test('Meta header indicates when UndiciConnection is used', async t => { + t.plan(1) + + function handler (req: http.IncomingMessage, res: http.ServerResponse) { + t.equal(req.headers['x-elastic-client-meta'], `es=${clientVersion},js=${nodeVersion},t=${transportVersion},un=${nodeVersion}`) + res.end('ok') + } + + const [{ port }, server] = await buildServer(handler) + + const client = new Client({ + node: `http://localhost:${port}`, + // Connection: UndiciConnection is the default + }) + + await client.transport.request({ method: 'GET', path: '/' }) + server.stop() +}) + +test('Meta header indicates when HttpConnection is used', async t => { + t.plan(1) + + function handler (req: http.IncomingMessage, res: http.ServerResponse) { + t.equal(req.headers['x-elastic-client-meta'], `es=${clientVersion},js=${nodeVersion},t=${transportVersion},hc=${nodeVersion}`) + res.end('ok') + } + + const [{ port }, server] = await buildServer(handler) + + const client = new Client({ + node: `http://localhost:${port}`, + Connection: HttpConnection, + }) + + await client.transport.request({ method: 'GET', path: '/' }) + server.stop() +}) + test('caFingerprint', t => { const client = new Client({ node: 'https://localhost:9200',