Skip to content

Commit 172180c

Browse files
authored
Report correct transport connection type in telemetry (#2599)
Fixes #2324
1 parent 947e09e commit 172180c

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/client.ts

Lines changed: 8 additions & 1 deletion
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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import FakeTimers from '@sinonjs/fake-timers'
2525
import { buildServer, connection } from '../utils'
2626
import { Client, errors } from '../..'
2727
import * as symbols from '@elastic/transport/lib/symbols'
28-
import { BaseConnectionPool, CloudConnectionPool, WeightedConnectionPool } from '@elastic/transport'
28+
import { BaseConnectionPool, CloudConnectionPool, WeightedConnectionPool, HttpConnection } from '@elastic/transport'
2929

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

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

0 commit comments

Comments
 (0)