Skip to content

Commit

Permalink
chore: refactor lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayachand committed Jul 9, 2024
1 parent 456609f commit 5c30abf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/util/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ class Prometheus {
name: 'fetch_dns_resolve_time',
help: 'fetch_dns_resolve_time',
type: 'histogram',
labelNames: ['identifier', 'transformationId', 'workspaceId', 'error', 'dnsHit'],
labelNames: ['identifier', 'transformationId', 'workspaceId', 'error', 'cacheHit'],
},
{
name: 'geo_call_duration',
Expand Down
35 changes: 18 additions & 17 deletions src/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,42 @@ const dnsCache = new NodeCache({
checkperiod: DNS_CACHE_TTL,
});

const fetchResolvedIp = async (hostname) => {
const resolveHostName = async (hostname) => {
// ex: [{ address: '108.157.0.0', ttl: 600 }]
const addresses = await resolver.resolve4(hostname, { ttl: true });
return addresses.length > 0 ? addresses[0] : {};
};

const fetchAddressFromHostName = async (hostname) => {
if (!DNS_CACHE_ENABLED) {
const { address } = await resolveHostName(hostname);
return { address, cacheHit: false };
}
const cachedAddress = dnsCache.get(hostname);
if (cachedAddress !== undefined) {
return { address: cachedAddress, cacheHit: true };
}
const { address, ttl } = await resolveHostName(hostname);
dnsCache.set(hostname, address, Math.min(ttl, DNS_CACHE_TTL));
return { address, cacheHit: false };
};

const staticLookup = (transformationTags) => async (hostname, _, cb) => {
let ip;
const resolveStartTime = new Date();
let dnsHit = false;
try {
if (DNS_CACHE_ENABLED) {
ip = dnsCache.get(hostname);
if (ip !== undefined) {
dnsHit = true;
} else {
const { address, ttl } = await fetchResolvedIp(hostname);
ip = address;
dnsCache.set(hostname, ip, ttl || DNS_CACHE_TTL);
}
} else {
const { address } = await fetchResolvedIp(hostname);
ip = address;
}
const { address, cacheHit } = await fetchAddressFromHostName(hostname);
ip = address;
stats.timing('fetch_dns_resolve_time', resolveStartTime, { ...transformationTags, cacheHit });
} catch (error) {
logger.error(`DNS Error Code: ${error.code} | Message : ${error.message}`);
stats.timing('fetch_dns_resolve_time', resolveStartTime, {
...transformationTags,
error: 'true',
dnsHit,
});
cb(null, `unable to resolve IP address for ${hostname}`, RECORD_TYPE_A);
return;
}
stats.timing('fetch_dns_resolve_time', resolveStartTime, { ...transformationTags, dnsHit });

if (!ip) {
cb(null, `resolved empty list of IP address for ${hostname}`, RECORD_TYPE_A);
Expand Down

0 comments on commit 5c30abf

Please sign in to comment.