@bitblit/ratchet-epsilon-common
Version:
Tiny adapter to simplify building API gateway Lambda APIS
110 lines • 4.62 kB
JavaScript
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
import { ErrorRatchet } from '@bitblit/ratchet-common/lang/error-ratchet';
import { NumberRatchet } from '@bitblit/ratchet-common/lang/number-ratchet';
import { GlobalRatchet } from '@bitblit/ratchet-common/lang/global-ratchet';
export class ContextUtil {
static CONTEXT_DATA_GLOBAL_NAME = 'EpsilonGlobalContextData';
constructor() { }
static fetchContextData() {
return GlobalRatchet.fetchGlobalVar(ContextUtil.CONTEXT_DATA_GLOBAL_NAME, {
epsilonInstance: null,
context: null,
event: null,
logVariables: {},
processLabel: 'UNSET',
overrideTraceDepth: null,
overrideTraceId: null,
});
}
static initContext(epsilon, evt, ctx, processLabel) {
const cd = ContextUtil.fetchContextData();
cd.epsilonInstance = epsilon;
cd.context = ctx;
cd.event = evt;
cd.processLabel = processLabel;
}
static clearContext() {
GlobalRatchet.setGlobalVar(ContextUtil.CONTEXT_DATA_GLOBAL_NAME, null);
}
static setOverrideTrace(traceId, traceDepth) {
const cd = ContextUtil.fetchContextData();
cd.overrideTraceId = traceId || cd.overrideTraceId;
cd.overrideTraceDepth = traceDepth || cd.overrideTraceDepth;
}
static setOverrideTraceFromInternalBackgroundEntry(entry) {
ContextUtil.setOverrideTrace(entry.traceId, entry.traceDepth);
}
static setOverrideTraceFromInterApiEntry(interApiEntry) {
ContextUtil.setOverrideTrace(interApiEntry.traceId, interApiEntry.traceDepth);
}
static addHeadersToRecord(input, depthOffset = 0) {
if (input) {
input[ContextUtil.traceHeaderName()] = ContextUtil.currentTraceId();
input[ContextUtil.traceDepthHeaderName()] = StringRatchet.safeString(ContextUtil.currentTraceDepth() + depthOffset);
}
else {
ErrorRatchet.throwFormattedErr('Cannot add headers to null/undefined input');
}
}
static addTraceToProxyResult(pr) {
pr.headers = pr.headers || {};
ContextUtil.addHeadersToRecord(pr.headers);
}
static addTraceToHttpRequestInit(ri) {
ri.headers = ri.headers || {};
ContextUtil.addHeadersToRecord(ri.headers, 1);
}
static setProcessLabel(processLabel) {
const cd = ContextUtil.fetchContextData();
cd.processLabel = processLabel;
}
static currentRequestId() {
const cd = ContextUtil.fetchContextData();
return cd?.context?.awsRequestId;
}
static defaultedCurrentRequestId(defaultValueIfMissing = StringRatchet.createType4Guid()) {
return ContextUtil.currentRequestId() ?? defaultValueIfMissing;
}
static remainingTimeMS() {
const cd = ContextUtil.fetchContextData();
return cd?.context?.getRemainingTimeInMillis();
}
static currentProcessLabel() {
const cd = ContextUtil.fetchContextData();
return cd?.processLabel || 'unset';
}
static traceHeaderName() {
const cd = ContextUtil.fetchContextData();
const headerName = cd?.epsilonInstance?.config?.loggerConfig?.traceHeaderName || 'X-TRACE-ID';
return headerName;
}
static traceDepthHeaderName() {
const cd = ContextUtil.fetchContextData();
const headerName = cd?.epsilonInstance?.config?.loggerConfig?.traceDepthHeaderName || 'X-TRACE-DEPTH';
return headerName;
}
static currentTraceId() {
const cd = ContextUtil.fetchContextData();
const traceFn = cd?.epsilonInstance?.config?.loggerConfig?.traceIdGenerator || ContextUtil.defaultedCurrentRequestId;
const traceId = cd?.overrideTraceId || cd?.event?.headers?.[ContextUtil.traceHeaderName()] || traceFn(cd?.event, cd?.context);
return traceId;
}
static currentTraceDepth() {
const cd = ContextUtil.fetchContextData();
const caller = cd?.overrideTraceDepth || NumberRatchet.safeNumber(cd?.event?.headers?.[ContextUtil.traceDepthHeaderName()]) || 1;
return caller;
}
static addLogVariable(name, val) {
const cd = ContextUtil.fetchContextData();
cd.logVariables[name] = val;
}
static fetchLogVariable(name) {
const cd = ContextUtil.fetchContextData();
return cd?.logVariables?.[name];
}
static fetchLogVariables() {
const cd = ContextUtil.fetchContextData();
return Object.assign({}, cd?.logVariables || {});
}
}
//# sourceMappingURL=context-util.js.map