Skip to content

Commit

Permalink
Report correct transport connection type in telemetry (#2599)
Browse files Browse the repository at this point in the history
Fixes #2324
  • Loading branch information
JoshMock authored Feb 3, 2025
1 parent 947e09e commit 172180c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 39 additions & 1 deletion test/unit/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('-')) {
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 172180c

Please sign in to comment.