Skip to content

Commit

Permalink
feat(client): support simple tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanrishi committed May 17, 2024
1 parent d1f4915 commit 8a753ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(',');
}
}
Expand Down
28 changes: 20 additions & 8 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>((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);
Expand Down Expand Up @@ -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<number>((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);
Expand Down Expand Up @@ -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<number>((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);
Expand Down Expand Up @@ -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<number>((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);
Expand Down

0 comments on commit 8a753ff

Please sign in to comment.