Skip to content

Commit d540d7f

Browse files
Report correct transport connection type in telemetry (#2599) (#2603)
Fixes #2324 (cherry picked from commit 172180c) Co-authored-by: Josh Mock <[email protected]>
1 parent 07f75a4 commit d540d7f

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/client.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,14 @@ export default class Client extends API {
287287
}
288288

289289
if (options.enableMetaHeader) {
290-
options.headers['x-elastic-client-meta'] = `es=${clientVersion},js=${nodeVersion},t=${transportVersion},hc=${nodeVersion}`
290+
let clientMeta = `es=${clientVersion},js=${nodeVersion},t=${transportVersion}`
291+
if (options.Connection === UndiciConnection) {
292+
clientMeta += `,un=${nodeVersion}`
293+
} else {
294+
// assumes HttpConnection
295+
clientMeta += `,hc=${nodeVersion}`
296+
}
297+
options.headers['x-elastic-client-meta'] = clientMeta
291298
}
292299

293300
this.name = options.name

test/unit/client.test.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import FakeTimers from '@sinonjs/fake-timers'
2424
import { buildServer, connection } from '../utils'
2525
import { Client, errors } from '../..'
2626
import * as symbols from '@elastic/transport/lib/symbols'
27-
import { BaseConnectionPool, CloudConnectionPool, WeightedConnectionPool } from '@elastic/transport'
27+
import { BaseConnectionPool, CloudConnectionPool, WeightedConnectionPool, HttpConnection } from '@elastic/transport'
2828

2929
let clientVersion: string = require('../../package.json').version // eslint-disable-line
3030
if (clientVersion.includes('-')) {
@@ -403,6 +403,44 @@ test('Meta header disabled', async t => {
403403
await client.transport.request({ method: 'GET', path: '/' })
404404
})
405405

406+
test('Meta header indicates when UndiciConnection is used', async t => {
407+
t.plan(1)
408+
409+
function handler (req: http.IncomingMessage, res: http.ServerResponse) {
410+
t.equal(req.headers['x-elastic-client-meta'], `es=${clientVersion},js=${nodeVersion},t=${transportVersion},un=${nodeVersion}`)
411+
res.end('ok')
412+
}
413+
414+
const [{ port }, server] = await buildServer(handler)
415+
416+
const client = new Client({
417+
node: `http://localhost:${port}`,
418+
// Connection: UndiciConnection is the default
419+
})
420+
421+
await client.transport.request({ method: 'GET', path: '/' })
422+
server.stop()
423+
})
424+
425+
test('Meta header indicates when HttpConnection is used', async t => {
426+
t.plan(1)
427+
428+
function handler (req: http.IncomingMessage, res: http.ServerResponse) {
429+
t.equal(req.headers['x-elastic-client-meta'], `es=${clientVersion},js=${nodeVersion},t=${transportVersion},hc=${nodeVersion}`)
430+
res.end('ok')
431+
}
432+
433+
const [{ port }, server] = await buildServer(handler)
434+
435+
const client = new Client({
436+
node: `http://localhost:${port}`,
437+
Connection: HttpConnection,
438+
})
439+
440+
await client.transport.request({ method: 'GET', path: '/' })
441+
server.stop()
442+
})
443+
406444
test('caFingerprint', t => {
407445
const client = new Client({
408446
node: 'https://localhost:9200',

0 commit comments

Comments
 (0)