diff --git a/src/index.ts b/src/index.ts index 9736cab..fe6a024 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,7 +20,7 @@ export enum Types { Object.freeze(Types); -export type Tags = { [key: string]: string } | string[]; +export type Tags = { [key: string]: string | null } | string[]; export interface Options { host?: string | URL; @@ -132,7 +132,7 @@ class Client { this.tags = tags.join(','); } else { this.tags = Object.keys(tags) - .map((tag) => `${tag}:${tags[tag]}`) + .map((tag) => (tags[tag] ? `${tag}:${tags[tag]}` : tag)) .join(','); } } diff --git a/test/index.spec.ts b/test/index.spec.ts index 59ef9d9..7509def 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -114,11 +114,14 @@ test('counter with sampling', (t) => { test('counter with tags', (t) => { const host = new URL(`udp://127.0.0.1:${t.context.address.port}`); const namespace = 'ns1'; - const tags = { tag1: 'value1', tag2: 'value2' }; + const tags = { tag1: 'value1', tag2: null, tag3: 'value3' }; const client = new Client({ host, namespace, tags }); return new Promise((resolve) => { t.context.server.on('metric', (metric) => { - t.is(`${namespace}.some.metric:1|c|@10|#tag1:value1,tag2:value2`, metric.toString()); + t.is( + `${namespace}.some.metric:1|c|@10|#tag1:value1,tag2,tag3:value3`, + metric.toString() + ); return resolve(0); }); client.counter('some.metric', 1, 10); @@ -154,11 +157,14 @@ test('timing with sampling', (t) => { test('timing with tags', (t) => { const host = new URL(`udp://127.0.0.1:${t.context.address.port}`); const namespace = 'ns1'; - const tags = { tag1: 'value1', tag2: 'value2' }; + const tags = { tag1: 'value1', tag2: null, tag3: 'value3' }; const client = new Client({ host, namespace, tags }); return new Promise((resolve) => { t.context.server.on('metric', (metric) => { - t.is(`${namespace}.some.metric:1|ms|@10|#tag1:value1,tag2:value2`, metric.toString()); + t.is( + `${namespace}.some.metric:1|ms|@10|#tag1:value1,tag2,tag3:value3`, + metric.toString() + ); return resolve(0); }); client.timing('some.metric', 1, 10); @@ -194,11 +200,14 @@ test('gauge should ignore sampling', (t) => { test('gauge with tags', (t) => { const host = new URL(`udp://127.0.0.1:${t.context.address.port}`); const namespace = 'ns1'; - const tags = { tag1: 'value1', tag2: 'value2' }; + const tags = { tag1: 'value1', tag2: null, tag3: 'value3' }; const client = new Client({ host, namespace, tags }); return new Promise((resolve) => { t.context.server.on('metric', (metric) => { - t.is(`${namespace}.some.metric:1|g|#tag1:value1,tag2:value2`, metric.toString()); + t.is( + `${namespace}.some.metric:1|g|#tag1:value1,tag2,tag3:value3`, + metric.toString() + ); return resolve(0); }); client.gauge('some.metric', 1); @@ -234,11 +243,14 @@ test('set should ignore sampling', (t) => { test('set with tags', (t) => { const host = new URL(`udp://127.0.0.1:${t.context.address.port}`); const namespace = 'ns1'; - const tags = { tag1: 'value1', tag2: 'value2' }; + const tags = { tag1: 'value1', tag2: null, tag3: 'value3' }; const client = new Client({ host, namespace, tags }); return new Promise((resolve) => { t.context.server.on('metric', (metric) => { - t.is(`${namespace}.some.metric:1|s|#tag1:value1,tag2:value2`, metric.toString()); + t.is( + `${namespace}.some.metric:1|s|#tag1:value1,tag2,tag3:value3`, + metric.toString() + ); return resolve(0); }); client.set('some.metric', 1);