|
1 | 1 | import { promisify } from 'util' |
2 | 2 |
|
3 | 3 | import slugify from '@sindresorhus/slugify' |
4 | | -import StatsdClient from 'statsd-client' |
| 4 | +import { StatsD } from 'hot-shots' |
5 | 5 |
|
6 | | -// TODO: replace with `timers/promises` after dropping Node < 15.0.0 |
7 | | -const pSetTimeout = promisify(setTimeout) |
| 6 | +export interface InputStatsDOptions { |
| 7 | + host?: string |
| 8 | + port?: number |
| 9 | +} |
8 | 10 |
|
9 | | -// See https://github.com/msiebuhr/node-statsd-client/blob/45a93ee4c94ca72f244a40b06cb542d4bd7c3766/lib/EphemeralSocket.js#L81 |
10 | | -const CLOSE_TIMEOUT = 11 |
| 11 | +export type StatsDOptions = Required<InputStatsDOptions> |
11 | 12 |
|
12 | | -// The socket creation is delayed until the first packet is sent. In our |
13 | | -// case, this happens just before `client.close()` is called, which is too |
14 | | -// late and make it not send anything. We need to manually create it using |
15 | | -// the internal API. |
16 | | -export const startClient = async function (host: string, port: number): Promise<StatsdClient> { |
17 | | - const client = new StatsdClient({ host, port, socketTimeout: 0 }) |
| 13 | +export const validateStatsDOptions = function (statsdOpts: InputStatsDOptions): statsdOpts is StatsDOptions { |
| 14 | + return !!(statsdOpts && statsdOpts.host && statsdOpts.port) |
| 15 | +} |
18 | 16 |
|
19 | | - // @ts-expect-error using internals :D |
20 | | - await promisify(client._socket._createSocket.bind(client._socket))() |
| 17 | +/** |
| 18 | + * Start a new StatsD Client and a new UDP socket |
| 19 | + */ |
| 20 | +export const startClient = async function (statsdOpts: StatsDOptions): Promise<StatsD> { |
| 21 | + const { host, port } = statsdOpts |
21 | 22 |
|
22 | | - return client |
| 23 | + return new StatsD({ |
| 24 | + host, |
| 25 | + port, |
| 26 | + // This caches the dns resolution for subsequent sends of metrics for this instance |
| 27 | + // Because we only try to send the metrics on close, this comes only into effect if `bufferFlushInterval` time is exceeded |
| 28 | + cacheDns: true, |
| 29 | + // set the maxBufferSize to infinite and the bufferFlushInterval very high, so that we only |
| 30 | + // send the metrics on close or if more than 10 seconds past by |
| 31 | + maxBufferSize: Infinity, |
| 32 | + bufferFlushInterval: 10_000, |
| 33 | + }) |
23 | 34 | } |
24 | 35 |
|
25 | | -// UDP packets are buffered and flushed at regular intervals by statsd-client. |
26 | | -// Closing force flushing all of them. |
27 | | -export const closeClient = async function (client: StatsdClient): Promise<void> { |
28 | | - client.close() |
29 | | - |
30 | | - // statsd-client does not provide a way of knowing when the socket is done |
31 | | - // closing, so we need to use the following hack. |
32 | | - await pSetTimeout(CLOSE_TIMEOUT) |
33 | | - await pSetTimeout(CLOSE_TIMEOUT) |
| 36 | +/** |
| 37 | + * Close the StatsD Client |
| 38 | + * |
| 39 | + * UDP packets are buffered and flushed every 10 seconds and |
| 40 | + * closing the client forces all buffered metrics to be flushed. |
| 41 | + */ |
| 42 | +export const closeClient = async function (client: StatsD): Promise<void> { |
| 43 | + await promisify(client.close.bind(client))() |
34 | 44 | } |
35 | 45 |
|
36 | | -// Make sure the timer name does not include special characters. |
37 | | -// For example, the `packageName` of local plugins includes dots. |
| 46 | +/** |
| 47 | + * Make sure the timer name does not include special characters. |
| 48 | + * For example, the `packageName` of local plugins includes dots. |
| 49 | + */ |
38 | 50 | export const normalizeTagName = function (name: string): string { |
39 | 51 | return slugify(name, { separator: '_' }) |
40 | 52 | } |
| 53 | + |
| 54 | +/** |
| 55 | + * Formats and flattens the tags as array |
| 56 | + * We do this because we might send the same tag with different values `{ tag: ['a', 'b'] }` |
| 57 | + * which cannot be represented in an flattened object and `hot-shots` also supports tags as array in the format `['tag:a', 'tag:b']` |
| 58 | + */ |
| 59 | +export const formatTags = function (tags: Record<string, string | string[]>): string[] { |
| 60 | + return Object.entries(tags) |
| 61 | + .map(([key, value]) => { |
| 62 | + if (Array.isArray(value)) { |
| 63 | + return value.map((subValue) => `${key}:${subValue}`) |
| 64 | + } else { |
| 65 | + return `${key}:${value}` |
| 66 | + } |
| 67 | + }) |
| 68 | + .flat() |
| 69 | +} |
0 commit comments