Skip to content

Commit

Permalink
fix(types): Allow null metricsServer and version (#286)
Browse files Browse the repository at this point in the history
null can be nicer as an explicit "this is not set".
  • Loading branch information
72636c authored May 19, 2024
1 parent d26b111 commit c23cf73
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/AppConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export interface AppConfig {
/**
* Version of the application
*
* This is used to tag custom metrics. It will typically be a CI build
* number.
* This is used to tag custom metrics. It will typically include a CI build
* number and/or a commit hash.
*/
version?: string;
version?: string | null | undefined;

/**
* Environment of the application
Expand Down
12 changes: 12 additions & 0 deletions src/createStatsDClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@ describe('createStatsDClient', () => {
}),
).toBeInstanceOf(Object);
});

it('should handle null config values', () => {
expect(
createStatsDClient(StatsD, {
name: 'test',
environment: 'jest',
version: null,

metricsServer: null,
}),
).toBeInstanceOf(Object);
});
});
7 changes: 5 additions & 2 deletions src/createStatsDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface StatsDConfig extends AppConfig {
* For Gantry this is `localhost`. If this is not specified then a mock
* client will be constructed.
*/
metricsServer?: string;
metricsServer?: string | null | undefined;
}

/**
Expand All @@ -44,10 +44,13 @@ export const createStatsDClient = <T extends InternalStatsD>(
config: StatsDConfig,
errorHandler?: (err: Error) => void,
): T => {
// istanbul ignore next: Jest is not picking up coalesce coverage
const host = config.metricsServer ?? undefined;

const client = new StatsD({
// Disable ourselves if there's no configured metrics server
mock: !config.metricsServer,
host: config.metricsServer,
host,
errorHandler,

prefix: `${config.name}.`,
Expand Down

0 comments on commit c23cf73

Please sign in to comment.