diff --git a/README.md b/README.md index 0210c21..9161890 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,9 @@ Parameters (specified as one object passed into hot-shots): * `prefix`: What to prefix each stat name with `default: ''` * `suffix`: What to suffix each stat name with `default: ''` * `globalize`: Expose this StatsD instance globally. `default: false` -* `cacheDns`: Caches dns lookup to *host* for *dnsTtlMilliseconds*, only used +* `cacheDns`: Caches dns lookup to *host* for *cacheDnsTtl*, only used when protocol is `udp`, `default: false` -* `dnsTtlMilliseconds`: time-to-live of dns lookups in milliseconds, when *cacheDns* is enabled. `default: 60000` +* `cacheDnsTtl`: time-to-live of dns lookups in milliseconds, when *cacheDns* is enabled. `default: 60000` * `mock`: Create a mock StatsD instance, sending no stats to the server and allowing data to be read from mockBuffer. Note that mockBuffer will keep growing, so only use for testing or clear out periodically. `default: false` diff --git a/lib/statsd.js b/lib/statsd.js index 7285d07..2f11ef9 100644 --- a/lib/statsd.js +++ b/lib/statsd.js @@ -6,6 +6,7 @@ const { PROTOCOL } = require('./constants'); const createTransport = require('./transport'); const UDS_DEFAULT_GRACEFUL_RESTART_LIMIT = 1000; +const CACHE_DNS_TTL_DEFAULT = 60000; /** * The Client for StatsD. The main entry-point for hot-shots. Note adding new parameters @@ -44,7 +45,7 @@ const Client = function (host, port, prefix, suffix, globalize, cacheDns, mock, this.protocol = PROTOCOL.UDP; } this.cacheDns = options.cacheDns === true; - this.dnsTtlMilliseconds = options.dnsTtlMilliseconds || 60000; + this.cacheDnsTtl = options.cacheDnsTtl || CACHE_DNS_TTL_DEFAULT; this.host = options.host || process.env.DD_AGENT_HOST || 'localhost'; this.port = options.port || parseInt(process.env.DD_DOGSTATSD_PORT, 10) || 8125; this.prefix = options.prefix || ''; @@ -93,7 +94,7 @@ const Client = function (host, port, prefix, suffix, globalize, cacheDns, mock, this.socket = createTransport(this, { host: this.host, cacheDns: this.cacheDns, - dnsTtlMilliseconds: this.dnsTtlMilliseconds, + cacheDnsTtl: this.cacheDnsTtl, path: options.path, port: this.port, protocol: this.protocol, diff --git a/lib/transport.js b/lib/transport.js index ec0c46e..969ebab 100644 --- a/lib/transport.js +++ b/lib/transport.js @@ -50,7 +50,7 @@ const createUdpTransport = args => { const sendUsingDnsCache = (callback, buf) => { const now = new Date(); - if (dnsResolutionData.resolvedAddress === undefined || (now - dnsResolutionData.timestamp > args.dnsTtlMilliseconds)) { + if (dnsResolutionData.resolvedAddress === undefined || (now - dnsResolutionData.timestamp > args.cacheDnsTtl)) { dns.lookup(args.host, (error, address) => { if (error) { callback(error); diff --git a/test/udpDnsCacheTransport.js b/test/udpDnsCacheTransport.js index f06bab7..b522b1e 100644 --- a/test/udpDnsCacheTransport.js +++ b/test/udpDnsCacheTransport.js @@ -121,10 +121,10 @@ describe('#udpDnsCacheTransport', () => { server = createServer(udpServerType, opts => { const socketMock = mockDgramSocket(); - const dnsTtlMilliseconds = 100; + const cacheDnsTtl = 100; statsd = createHotShotsClient(Object.assign(opts, { cacheDns: true, - dnsTtlMilliseconds: dnsTtlMilliseconds + cacheDnsTtl: cacheDnsTtl }), 'client'); const resolvedHostAddress = '1.1.1.1'; @@ -151,7 +151,7 @@ describe('#udpDnsCacheTransport', () => { done(); }, 1000); }); - }, dnsTtlMilliseconds + 1); + }, cacheDnsTtl + 1); }); }); }); diff --git a/types.d.ts b/types.d.ts index 1452216..537da10 100644 --- a/types.d.ts +++ b/types.d.ts @@ -6,7 +6,7 @@ declare module "hot-shots" { bufferFlushInterval?: number; bufferHolder?: { buffer: string }; cacheDns?: boolean; - dnsTtlMilliseconds?: number; + cacheDnsTtl?: number; errorHandler?: (err: Error) => void; globalTags?: Tags; globalize?: boolean;