Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: filtered logging and log for all destinations #3484

Merged
merged 19 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 113 additions & 15 deletions src/adapters/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
const http = require('http');
const https = require('https');
const axios = require('axios');
const log = require('../logger');
const logger = require('../logger');
const stats = require('../util/stats');
const { removeUndefinedValues } = require('../v0/util');
const { removeUndefinedValues, isDefinedAndNotNullAndNotEmpty } = require('../v0/util');
const { processAxiosResponse } = require('./utils/networkUtils');
// Only for tests
const { setResponsesForMockAxiosAdapter } = require('../../test/testHelper');
Expand Down Expand Up @@ -56,7 +56,7 @@
statusCode,
clientResponse,
}) => {
const logMetaInfo = log.getLogMetadata(metadata);
const logMetaInfo = logger.getLogMetadata(metadata);
stats.timing('outgoing_request_latency', startTime, {
...logMetaInfo,
feature,
Expand Down Expand Up @@ -118,6 +118,28 @@
return requestOptions;
};

const getResponseDetails = (clientResponse) => {
if (!clientResponse.success) {
const { response } = clientResponse;
// non 2xx status handling for axios response
if (response) {
const { data, status, headers } = response;
return {
body: data || '',
status: status || 500,
...(isDefinedAndNotNullAndNotEmpty(headers) ? { headers } : {}),
};
}
return {};

Check warning on line 133 in src/adapters/network.js

View check run for this annotation

Codecov / codecov/patch

src/adapters/network.js#L133

Added line #L133 was not covered by tests
}
const { data, status, headers } = clientResponse.response;
return {
body: data || '',
status: status || 500,
...(isDefinedAndNotNullAndNotEmpty(headers) ? { headers } : {}),
};
};

/**
* sends an http request with underlying client, expects request options
* @param {*} options
Expand All @@ -128,14 +150,27 @@
// here the options argument K-Vs will take priority over the default options
const requestOptions = enhanceRequestOptions(options);

const startTime = new Date();
const { url, data, method } = requestOptions;
const commonMsg = `[${statTags?.destType?.toUpperCase?.() || ''}] ${statTags?.endpointPath}`;
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
logger.requestLog(`${commonMsg} request`, {
metadata: statTags?.metadata,
requestDetails: {
url,
body: options?.data,
method: options?.method || statTags?.requestMethod,
},
});
const startTime = new Date();
try {
const response = await axios(requestOptions);
clientResponse = { success: true, response };
} catch (err) {
clientResponse = { success: false, response: err };
} finally {
logger.responseLog(`${commonMsg} response`, {
metadata: statTags?.metadata,
responseDetails: getResponseDetails(clientResponse),
});
fireHTTPStats(clientResponse, startTime, statTags);
}

Expand All @@ -156,13 +191,26 @@
// here the options argument K-Vs will take priority over the default options
const requestOptions = enhanceRequestOptions(options);

const commonMsg = `[${statTags?.destType?.toUpperCase?.() || ''}] ${statTags?.endpointPath}`;
logger.requestLog(`${commonMsg} request`, {
metadata: statTags?.metadata,
requestDetails: {
url,
body: requestOptions?.data,
method: 'get',
},
});
const startTime = new Date();
try {
const response = await axios.get(url, requestOptions);
clientResponse = { success: true, response };
} catch (err) {
clientResponse = { success: false, response: err };
} finally {
logger.responseLog(`${commonMsg} response`, {
metadata: statTags?.metadata,
responseDetails: getResponseDetails(clientResponse),
});
fireHTTPStats(clientResponse, startTime, statTags);
}
setResponsesForMockAxiosAdapter({ url, options, method: 'GET' }, clientResponse);
Expand All @@ -182,13 +230,26 @@
// here the options argument K-Vs will take priority over the default options
const requestOptions = enhanceRequestOptions(options);

const commonMsg = `[${statTags?.destType?.toUpperCase?.() || ''}] ${statTags?.endpointPath}`;
logger.requestLog(`${commonMsg} request`, {
metadata: statTags?.metadata,
requestDetails: {
url,
body: requestOptions?.data,
method: 'delete',
},
});
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
const startTime = new Date();
try {
const response = await axios.delete(url, requestOptions);
clientResponse = { success: true, response };
} catch (err) {
clientResponse = { success: false, response: err };
} finally {
logger.responseLog(`${commonMsg} response`, {
metadata: statTags?.metadata,
responseDetails: getResponseDetails(clientResponse),
});
fireHTTPStats(clientResponse, startTime, statTags);
}
setResponsesForMockAxiosAdapter({ url, options, method: 'DELETE' }, clientResponse);
Expand All @@ -209,13 +270,26 @@
// here the options argument K-Vs will take priority over the default options
const requestOptions = enhanceRequestOptions(options);

const commonMsg = `[${statTags?.destType?.toUpperCase?.() || ''}] ${statTags?.endpointPath}`;
logger.requestLog(`${commonMsg} request`, {
metadata: statTags?.metadata,
requestDetails: {
url,
body: data,
method: 'post',
},
});
const startTime = new Date();
try {
const response = await axios.post(url, data, requestOptions);
clientResponse = { success: true, response };
} catch (err) {
clientResponse = { success: false, response: err };
} finally {
logger.responseLog(`${commonMsg} response`, {
metadata: statTags?.metadata,
responseDetails: getResponseDetails(clientResponse),
});
fireHTTPStats(clientResponse, startTime, statTags);
}
setResponsesForMockAxiosAdapter({ url, data, options, method: 'POST' }, clientResponse);
Expand All @@ -235,14 +309,26 @@
let clientResponse;
// here the options argument K-Vs will take priority over the default options
const requestOptions = enhanceRequestOptions(options);

const commonMsg = `[${statTags?.destType?.toUpperCase?.() || ''}] ${statTags?.endpointPath}`;
logger.requestLog(`${commonMsg} request`, {
metadata: statTags?.metadata,
requestDetails: {
url,
body: data,
method: 'put',
},
});
const startTime = new Date();
try {
const response = await axios.put(url, data, requestOptions);
clientResponse = { success: true, response };
} catch (err) {
clientResponse = { success: false, response: err };
} finally {
logger.responseLog(`${commonMsg} response`, {
metadata: statTags?.metadata,
responseDetails: getResponseDetails(clientResponse),
});
fireHTTPStats(clientResponse, startTime, statTags);
}
setResponsesForMockAxiosAdapter({ url, data, options, method: 'PUT' }, clientResponse);
Expand All @@ -262,14 +348,26 @@
let clientResponse;
// here the options argument K-Vs will take priority over the default options
const requestOptions = enhanceRequestOptions(options);

const commonMsg = `[${statTags?.destType?.toUpperCase?.() || ''}] ${statTags?.endpointPath}`;
logger.requestLog(`${commonMsg} request`, {

Check warning on line 352 in src/adapters/network.js

View check run for this annotation

Codecov / codecov/patch

src/adapters/network.js#L352

Added line #L352 was not covered by tests
metadata: statTags?.metadata,
requestDetails: {
url,
body: data,
method: 'patch',
},
});
const startTime = new Date();
try {
const response = await axios.patch(url, data, requestOptions);
clientResponse = { success: true, response };
} catch (err) {
clientResponse = { success: false, response: err };
} finally {
logger.responseLog(`${commonMsg} response`, {

Check warning on line 367 in src/adapters/network.js

View check run for this annotation

Codecov / codecov/patch

src/adapters/network.js#L367

Added line #L367 was not covered by tests
metadata: statTags?.metadata,
responseDetails: getResponseDetails(clientResponse),
});
fireHTTPStats(clientResponse, startTime, statTags);
}
setResponsesForMockAxiosAdapter({ url, data, options, method: 'PATCH' }, clientResponse);
Expand Down Expand Up @@ -352,7 +450,7 @@
// TODO:
break;
default:
log.debug(`body format ${payloadFormat} not supported`);
logger.debug(`body format ${payloadFormat} not supported`);
}
// Ref: https://github.com/rudderlabs/rudder-server/blob/master/router/network.go#L164
headers['User-Agent'] = 'RudderLabs';
Expand Down Expand Up @@ -426,14 +524,14 @@
headers,
method,
};
log.requestLog(`[${destType.toUpperCase()}] delivering data`, {
metadata,
requestDetails: {
body: data,
url: endpoint,
method,
},
});
// log.requestLog(`[${destType.toUpperCase()}] delivering data`, {
// metadata,
// requestDetails: {
// body: data,
// url: endpoint,
// method,
// },
// });
const response = await httpSend(requestOptions, {
feature: 'proxy',
destType,
Expand Down
34 changes: 26 additions & 8 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const { LOGLEVELS, structuredLogger } = require('@rudderstack/integrations-lib')

// LOGGER_IMPL can be `console` or `winston`
const loggerImpl = process.env.LOGGER_IMPL ?? 'winston';
const logDestIds = (process.env.LOG_DEST_IDS ?? '').split(',')?.map?.((s) => s?.trim?.()); // should be comma separated
const logWspIds = (process.env.LOG_WSP_IDS ?? '').split(',')?.map?.((s) => s?.trim?.()); // should be comma separated

let logLevel = process.env.LOG_LEVEL ?? 'error';

Expand Down Expand Up @@ -71,6 +73,24 @@ const formLogArgs = (args) => {
return [msg, ...otherArgs];
};

const isMetadataMatching = (m) => {
const isDestIdConfigured = logDestIds?.find?.((envDId) => envDId && envDId === m?.destinationId);
const isWspIdConfigured = logWspIds?.find?.(
(envWspId) => envWspId && envWspId === m?.workspaceId,
);
return Boolean(isDestIdConfigured || isWspIdConfigured);
};

const getMatchedMetadata = (metadata) => {
if (!Array.isArray(metadata)) {
if (isMetadataMatching(metadata)) {
return [metadata];
}
return [];
}
return metadata.filter((m) => isMetadataMatching(m));
};

/**
* Perform logging operation on logMethod passed
*
Expand Down Expand Up @@ -143,8 +163,9 @@ const error = (...args) => {

const requestLog = (identifierMsg, { metadata, requestDetails: { url, body, method } }) => {
const logger = getLogger();
if (LOGLEVELS[logLevel] === LOGLEVELS.warn) {
const reqLogArgs = [identifierMsg, { metadata, url, body, method }];
const filteredMetadata = getMatchedMetadata(metadata);
if (LOGLEVELS[logLevel] === LOGLEVELS.warn && filteredMetadata.length > 0) {
krishna2020 marked this conversation as resolved.
Show resolved Hide resolved
const reqLogArgs = [identifierMsg, { metadata: filteredMetadata, url, body, method }];
log(logger.warn, reqLogArgs);
}
};
Expand All @@ -154,8 +175,9 @@ const responseLog = (
{ metadata, responseDetails: { response: body, status, headers } },
) => {
const logger = getLogger();
if (LOGLEVELS[logLevel] === LOGLEVELS.warn) {
const resLogArgs = [identifierMsg, { metadata, body, status, headers }];
const filteredMetadata = getMatchedMetadata(metadata);
if (LOGLEVELS[logLevel] === LOGLEVELS.warn && filteredMetadata.length > 0) {
const resLogArgs = [identifierMsg, { metadata: filteredMetadata, body, status, headers }];
log(logger.warn, resLogArgs);
}
};
Expand All @@ -166,10 +188,6 @@ module.exports = {
warn,
error,
setLogLevel,
// levelDebug,
// levelInfo,
// levelWarn,
// levelError,
responseLog,
getLogMetadata,
requestLog,
Expand Down
Loading
Loading