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 all commits
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
3 changes: 3 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ versions:
changeNotes: Add follow redirect.
- sha: 944786d0c39e6b14ab514577e5578f7317e9e3a2
changeNotes: Initial release.
- sha: 973b1dbae5cf2240afb720e1400f0e6548e91f65
changeNotes: Added log settings.

52 changes: 50 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,38 @@ 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: 'Request',
TraceId: traceId,
EventName: 'HttpLookupRequest',
RequestMethod: data.requestMethod,
RequestUrl: url,
RequestBody: postBody,
})
);
}
return sendHttpRequest(url, requestOptions, postBody).then((successResult) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'HTTPLookup',
Type: 'Response',
TraceId: traceId,
EventName: 'HttpLookupRequest',
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 +174,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';
}
176 changes: 174 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,38 @@ 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: 'Request',
TraceId: traceId,
EventName: 'HttpLookupRequest',
RequestMethod: data.requestMethod,
RequestUrl: url,
RequestBody: postBody,
})
);
}
return sendHttpRequest(url, requestOptions, postBody).then((successResult) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'HTTPLookup',
Type: 'Response',
TraceId: traceId,
EventName: 'HttpLookupRequest',
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 +477,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 +530,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