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

DEV-1207-Add-log-settings-to-async-variables to HTTPLookup tag #5

Merged
merged 7 commits into from
Nov 29, 2023
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
49 changes: 47 additions & 2 deletions template.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const encodeUriComponent = require('encodeUriComponent');
const templateDataStorage = require('templateDataStorage');
const sha256Sync = require('sha256Sync');
const Promise = require('Promise');
const logToConsole = require('logToConsole');
const getRequestHeader = require('getRequestHeader');
const getContainerVersion = require('getContainerVersion');

const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = isLoggingEnabled ? getRequestHeader('trace-id') : undefined;

let requestHeaders = {};
let requestBody = {};
Expand Down Expand Up @@ -70,14 +76,35 @@ function sendRequest(url, requestOptions, postBody) {
const cachedBody = templateDataStorage.getItemCopy(cacheKey);
if (cachedBody) return Promise.create((resolve) => resolve(cachedBody));
}

if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'HTTPLookup',
Type: 'Response',
TraceId: traceId,
EventName: 'CreateOrUpdateContact',
dmtsolovey marked this conversation as resolved.
Show resolved Hide resolved
})
);
}
return sendHttpRequest(url, requestOptions, postBody).then((successResult) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'HTTPLookup',
Type: 'Response',
TraceId: traceId,
EventName: 'CreateOrUpdateContact',
ResponseStatusCode: successResult.statusCode,
ResponseHeaders: successResult.headers,
ResponseBody: successResult.body,
})
);
}
if (successResult.statusCode === 301 || successResult.statusCode === 302) {
return sendRequest(successResult.headers['location'], requestOptions, postBody);
}

if (data.storeResponse) templateDataStorage.setItemCopy(cacheKey, successResult.body);

return successResult.body;
});
}
Expand Down Expand Up @@ -144,3 +171,21 @@ function enc(data) {
data = data || '';
return encodeUriComponent(data);
}
function determinateIsLoggingEnabled() {
const containerVersion = getContainerVersion();
const isDebug = !!(containerVersion && (containerVersion.debugMode || containerVersion.previewMode));

if (!data.logType) {
return isDebug;
}

if (data.logType === 'no') {
return false;
}

if (data.logType === 'debug') {
return isDebug;
}

return data.logType === 'always';
}
173 changes: 171 additions & 2 deletions template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,34 @@ ___TEMPLATE_PARAMETERS___
]
}
]
},
{
"displayName": "Logs Settings",
"name": "logsGroup",
"groupStyle": "ZIPPY_CLOSED",
"type": "GROUP",
"subParams": [
{
"type": "RADIO",
"name": "logType",
"radioItems": [
{
"value": "no",
"displayValue": "Do not log"
},
{
"value": "debug",
"displayValue": "Log to console during debug and preview"
},
{
"value": "always",
"displayValue": "Always log to console"
}
],
"simpleValueType": true,
"defaultValue": "debug"
}
]
}
]

Expand All @@ -281,6 +309,12 @@ const encodeUriComponent = require('encodeUriComponent');
const templateDataStorage = require('templateDataStorage');
const sha256Sync = require('sha256Sync');
const Promise = require('Promise');
const logToConsole = require('logToConsole');
const getRequestHeader = require('getRequestHeader');
const getContainerVersion = require('getContainerVersion');

const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = isLoggingEnabled ? getRequestHeader('trace-id') : undefined;

let requestHeaders = {};
let requestBody = {};
Expand Down Expand Up @@ -345,14 +379,35 @@ function sendRequest(url, requestOptions, postBody) {
const cachedBody = templateDataStorage.getItemCopy(cacheKey);
if (cachedBody) return Promise.create((resolve) => resolve(cachedBody));
}

if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'HTTPLookup',
Type: 'Response',
TraceId: traceId,
EventName: 'CreateOrUpdateContact',
})
);
}
return sendHttpRequest(url, requestOptions, postBody).then((successResult) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'HTTPLookup',
Type: 'Response',
TraceId: traceId,
EventName: 'CreateOrUpdateContact',
ResponseStatusCode: successResult.statusCode,
ResponseHeaders: successResult.headers,
ResponseBody: successResult.body,
})
);
}
if (successResult.statusCode === 301 || successResult.statusCode === 302) {
return sendRequest(successResult.headers['location'], requestOptions, postBody);
}

if (data.storeResponse) templateDataStorage.setItemCopy(cacheKey, successResult.body);

return successResult.body;
});
}
Expand Down Expand Up @@ -419,6 +474,24 @@ function enc(data) {
data = data || '';
return encodeUriComponent(data);
}
function determinateIsLoggingEnabled() {
const containerVersion = getContainerVersion();
const isDebug = !!(containerVersion && (containerVersion.debugMode || containerVersion.previewMode));

if (!data.logType) {
return isDebug;
}

if (data.logType === 'no') {
return false;
}

if (data.logType === 'debug') {
return isDebug;
}

return data.logType === 'always';
}


___SERVER_PERMISSIONS___
Expand Down Expand Up @@ -454,6 +527,102 @@ ___SERVER_PERMISSIONS___
"param": []
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "read_request",
"versionId": "1"
},
"param": [
{
"key": "headerWhitelist",
"value": {
"type": 2,
"listItem": [
{
"type": 3,
"mapKey": [
{
"type": 1,
"string": "headerName"
}
],
"mapValue": [
{
"type": 1,
"string": "trace-id"
}
]
}
]
}
},
{
"key": "headersAllowed",
"value": {
"type": 8,
"boolean": true
}
},
{
"key": "requestAccess",
"value": {
"type": 1,
"string": "specific"
}
},
{
"key": "headerAccess",
"value": {
"type": 1,
"string": "specific"
}
},
{
"key": "queryParameterAccess",
"value": {
"type": 1,
"string": "any"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "logging",
"versionId": "1"
},
"param": [
{
"key": "environments",
"value": {
"type": 1,
"string": "all"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "read_container_data",
"versionId": "1"
},
"param": []
},
"isRequired": true
}
]

Expand Down
Loading