Skip to content

Commit

Permalink
chore: add transformation tags to dns resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayachand committed Jun 25, 2024
2 parents 5560821 + 8129a06 commit 658537d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/util/customTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function runUserTransform(
new ivm.Reference(async (resolve, ...args) => {
try {
const fetchStartTime = new Date();
const res = await fetchWithDnsWrapper(transformationId, ...args);
const res = await fetchWithDnsWrapper(trTags, ...args);
const data = await res.json();
stats.timing('fetch_call_duration', fetchStartTime, trTags);

Check warning on line 43 in src/util/customTransformer.js

View check run for this annotation

Codecov / codecov/patch

src/util/customTransformer.js#L43

Added line #L43 was not covered by tests
resolve.applyIgnored(undefined, [new ivm.ExternalCopy(data).copyInto()]);
Expand All @@ -53,7 +53,7 @@ async function runUserTransform(
new ivm.Reference(async (resolve, reject, ...args) => {
try {
const fetchStartTime = new Date();
const res = await fetchWithDnsWrapper(transformationId, ...args);
const res = await fetchWithDnsWrapper(trTags, ...args);
const headersContent = {};
res.headers.forEach((value, header) => {
headersContent[header] = value;
Expand Down Expand Up @@ -254,7 +254,7 @@ async function runUserTransform(

const tags = {
errored: transformationError ? true : false,
...(events.length && events[0].metadata ? getMetadata(events[0].metadata) : {}),
...(Object.keys(eventsMetadata).length ? getMetadata(Object.values(eventsMetadata)[0]) : {}),
...trTags,
};

Expand Down
4 changes: 2 additions & 2 deletions src/util/ivmFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async function createIvm(
new ivm.Reference(async (resolve, ...args) => {
try {
const fetchStartTime = new Date();
const res = await fetchWithDnsWrapper(transformationId, ...args);
const res = await fetchWithDnsWrapper(trTags, ...args);
const data = await res.json();
stats.timing('fetch_call_duration', fetchStartTime, trTags);

Check warning on line 192 in src/util/ivmFactory.js

View check run for this annotation

Codecov / codecov/patch

src/util/ivmFactory.js#L192

Added line #L192 was not covered by tests
resolve.applyIgnored(undefined, [new ivm.ExternalCopy(data).copyInto()]);
Expand All @@ -202,7 +202,7 @@ async function createIvm(
new ivm.Reference(async (resolve, reject, ...args) => {
try {
const fetchStartTime = new Date();
const res = await fetchWithDnsWrapper(transformationId, ...args);
const res = await fetchWithDnsWrapper(trTags, ...args);
const headersContent = {};
res.headers.forEach((value, header) => {
headersContent[header] = value;
Expand Down
14 changes: 7 additions & 7 deletions src/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ const LOCAL_HOST_NAMES_LIST = ['localhost', '127.0.0.1', '[::]', '[::1]'];
const LOCALHOST_OCTET = '127.';
const RECORD_TYPE_A = 4; // ipv4

const staticLookup = (transformationId) => async (hostname, _, cb) => {
const staticLookup = (transformationTags) => async (hostname, _, cb) => {
let ips;
const resolveStartTime = new Date();
try {
ips = await resolver.resolve4(hostname);
} catch (error) {
logger.error(`DNS Error Code: ${error.code} | Message : ${error.message}`);
stats.timing('fetch_dns_resolve_time', resolveStartTime, {
transformationId,
...transformationTags,
error: 'true',
});
cb(null, `unable to resolve IP address for ${hostname}`, RECORD_TYPE_A);
return;
}
stats.timing('fetch_dns_resolve_time', resolveStartTime, { transformationId });
stats.timing('fetch_dns_resolve_time', resolveStartTime, transformationTags);

if (ips.length === 0) {
cb(null, `resolved empty list of IP address for ${hostname}`, RECORD_TYPE_A);
Expand All @@ -48,9 +48,9 @@ const staticLookup = (transformationId) => async (hostname, _, cb) => {
cb(null, ips[0], RECORD_TYPE_A);
};

const httpAgentWithDnsLookup = (scheme, transformationId) => {
const httpAgentWithDnsLookup = (scheme, transformationTags) => {
const httpModule = scheme === 'http' ? http : https;
return new httpModule.Agent({ lookup: staticLookup(transformationId) });
return new httpModule.Agent({ lookup: staticLookup(transformationTags) });
};

const blockLocalhostRequests = (url) => {
Expand All @@ -74,7 +74,7 @@ const blockInvalidProtocolRequests = (url) => {
}
};

const fetchWithDnsWrapper = async (transformationId, ...args) => {
const fetchWithDnsWrapper = async (transformationTags, ...args) => {
if (process.env.DNS_RESOLVE_FETCH_HOST !== 'true') {
return await fetch(...args);
}
Expand All @@ -88,7 +88,7 @@ const fetchWithDnsWrapper = async (transformationId, ...args) => {
const fetchOptions = args[1] || {};
const schemeName = fetchURL.startsWith('https') ? 'https' : 'http';
// assign resolved agent to fetch
fetchOptions.agent = httpAgentWithDnsLookup(schemeName, transformationId);
fetchOptions.agent = httpAgentWithDnsLookup(schemeName, transformationTags);
return await fetch(fetchURL, fetchOptions);
};

Expand Down
4 changes: 2 additions & 2 deletions src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1447,13 +1447,13 @@ const getTrackingPlanMetadata = (metadata) => ({
workspaceId: metadata.workspaceId,
});

const getMetadata = (metadata) => ({
const getMetadata = (metadata = {}) => ({
sourceType: metadata.sourceType,
destinationType: metadata.destinationType,
k8_namespace: metadata.namespace,
});

const getTransformationMetadata = (metadata) => ({
const getTransformationMetadata = (metadata = {}) => ({
transformationId: metadata.transformationId,
workspaceId: metadata.workspaceId,
});
Expand Down

0 comments on commit 658537d

Please sign in to comment.