Report correct transport connection type in telemetry (#2599) (#2601)

Fixes #2324

(cherry picked from commit 172180cb21)

Co-authored-by: Josh Mock <joshua.mock@elastic.co>
This commit is contained in:
github-actions[bot]
2025-02-03 13:10:48 -06:00
committed by GitHub
parent 37e20ee85d
commit 355e108319
2 changed files with 47 additions and 2 deletions

View File

@ -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

View File

@ -24,7 +24,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('-')) {
@ -403,6 +403,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',