From 1a7727588eff564760789408309e870d28398476 Mon Sep 17 00:00:00 2001 From: Tomas Della Vedova Date: Mon, 13 Jul 2020 12:38:16 +0200 Subject: [PATCH] Allow the client name to be a symbol (#1254) --- docs/configuration.asciidoc | 2 +- index.d.ts | 2 +- lib/Transport.d.ts | 2 +- test/acceptance/observability.test.js | 27 ++++++++++++++++++++++++++- test/types/client-options.test-d.ts | 7 +++++++ test/unit/client.test.js | 23 +++++++++++++++++++++++ 6 files changed, 59 insertions(+), 4 deletions(-) diff --git a/docs/configuration.asciidoc b/docs/configuration.asciidoc index f090e9f81..089cf02ce 100644 --- a/docs/configuration.asciidoc +++ b/docs/configuration.asciidoc @@ -178,7 +178,7 @@ function generateRequestId (params, options) { ---- |`name` -|`string` - The name to identify the client instance in the events. + +|`string | symbol` - The name to identify the client instance in the events. + _Default:_ `elasticsearch-js` |`opaqueIdPrefix` diff --git a/index.d.ts b/index.d.ts index f63aed8f5..416f386b4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -88,7 +88,7 @@ interface ClientOptions { headers?: Record; opaqueIdPrefix?: string; generateRequestId?: generateRequestIdFn; - name?: string; + name?: string | symbol; auth?: BasicAuth | ApiKeyAuth; cloud?: { id: string; diff --git a/lib/Transport.d.ts b/lib/Transport.d.ts index c35d57aa6..4956e57da 100644 --- a/lib/Transport.d.ts +++ b/lib/Transport.d.ts @@ -52,7 +52,7 @@ export interface RequestEvent, TContext = unknow warnings: string[] | null; meta: { context: TContext; - name: string; + name: string | symbol; request: { params: TransportRequestParams; options: TransportRequestOptions; diff --git a/test/acceptance/observability.test.js b/test/acceptance/observability.test.js index 491e0817a..c27c72483 100644 --- a/test/acceptance/observability.test.js +++ b/test/acceptance/observability.test.js @@ -205,7 +205,7 @@ test('Client name', t => { t.end() }) - t.test('Is present in the event metadata', t => { + t.test('Is present in the event metadata (as string)', t => { t.plan(6) const client = new Client({ node: 'http://localhost:9200', @@ -229,6 +229,31 @@ test('Client name', t => { }) }) + t.test('Is present in the event metadata (as symbol)', t => { + t.plan(6) + const symbol = Symbol('cluster') + const client = new Client({ + node: 'http://localhost:9200', + Connection: MockConnection, + name: symbol + }) + + client.on('request', (err, { meta }) => { + t.error(err) + t.strictEqual(meta.name, symbol) + }) + + client.on('response', (err, { meta }) => { + t.error(err) + t.strictEqual(meta.name, symbol) + }) + + client.info((err, { meta }) => { + t.error(err) + t.strictEqual(meta.name, symbol) + }) + }) + t.test('Sniff and client name', t => { t.test('sniffOnStart', t => { t.plan(2) diff --git a/test/types/client-options.test-d.ts b/test/types/client-options.test-d.ts index d19a6a707..f9ac78d32 100644 --- a/test/types/client-options.test-d.ts +++ b/test/types/client-options.test-d.ts @@ -300,6 +300,13 @@ expectType( }) ) +expectType( + new Client({ + node: 'http://localhost:9200', + name: Symbol('foo') + }) +) + expectError( new Client({ node: 'http://localhost:9200', diff --git a/test/unit/client.test.js b/test/unit/client.test.js index 1678b3999..8ab7de040 100644 --- a/test/unit/client.test.js +++ b/test/unit/client.test.js @@ -1092,3 +1092,26 @@ test('Random selector', t => { }) }) }) + +test('name property as string', t => { + t.plan(1) + + const client = new Client({ + node: 'http://localhost:9200', + name: 'client-name' + }) + + t.strictEqual(client.name, 'client-name') +}) + +test('name property as symbol', t => { + t.plan(1) + + const symbol = Symbol('client-name') + const client = new Client({ + node: 'http://localhost:9200', + name: symbol + }) + + t.strictEqual(client.name, symbol) +})