Skip to content

Commit

Permalink
fixed style with eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
carolabadeer committed Nov 8, 2022
1 parent 5342ac9 commit 7cde0b2
Show file tree
Hide file tree
Showing 14 changed files with 2,782 additions and 2,794 deletions.
137 changes: 68 additions & 69 deletions packages/core/lib/context_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
* @module context_utils
*/

var cls = require('cls-hooked/context');
const cls = require('cls-hooked/context')

var logger = require('./logger');
var Segment = require('./segments/segment');
var Subsegment = require('./segments/attributes/subsegment');
const logger = require('./logger')
const Segment = require('./segments/segment')
const Subsegment = require('./segments/attributes/subsegment')

var cls_mode = true;
var NAMESPACE ='AWSXRay';
var SEGMENT = 'segment';
let cls_mode = true
const NAMESPACE = 'AWSXRay'
const SEGMENT = 'segment'

var contextOverride = false;
let contextOverride = false

var contextUtils = {
const contextUtils = {
CONTEXT_MISSING_STRATEGY: {
RUNTIME_ERROR: {
contextMissing: function contextMissingRuntimeError(message) {
throw new Error(message);
contextMissing: function contextMissingRuntimeError (message) {
throw new Error(message)
}
},
LOG_ERROR: {
contextMissing: function contextMissingLogError(message) {
var err = new Error(message);
logger.getLogger().error(err.stack);
contextMissing: function contextMissingLogError (message) {
const err = new Error(message)
logger.getLogger().error(err.stack)
}
},
IGNORE_ERROR: {
contextMissing: function contextMissingIgnoreError() {
contextMissing: function contextMissingIgnoreError () {
}
}
},
Expand All @@ -42,22 +42,22 @@ var contextUtils = {
* @alias module:context_utils.resolveManualSegmentParams
*/

resolveManualSegmentParams: function resolveManualSegmentParams(params) {
resolveManualSegmentParams: function resolveManualSegmentParams (params) {
if (params && !contextUtils.isAutomaticMode()) {
var xraySegment = params.XRaySegment || params.XraySegment;
var segment = params.Segment;
var found = null;
const xraySegment = params.XRaySegment || params.XraySegment
const segment = params.Segment
let found = null

if (xraySegment && (xraySegment instanceof Segment || xraySegment instanceof Subsegment)) {
found = xraySegment;
delete params.XRaySegment;
delete params.XraySegment;
found = xraySegment
delete params.XRaySegment
delete params.XraySegment
} else if (segment && (segment instanceof Segment || segment instanceof Subsegment)) {
found = segment;
delete params.Segment;
found = segment
delete params.Segment
}

return found;
return found
}
},

Expand All @@ -66,8 +66,8 @@ var contextUtils = {
* @returns {Namespace}
* @alias module:context_utils.getNamespace
*/
getNamespace: function getNamespace() {
return cls.getNamespace(NAMESPACE) || cls.createNamespace(NAMESPACE);
getNamespace: function getNamespace () {
return cls.getNamespace(NAMESPACE) || cls.createNamespace(NAMESPACE)
},

/**
Expand All @@ -77,13 +77,13 @@ var contextUtils = {
* @alias module:context_utils.resolveSegment
*/

resolveSegment: function resolveSegment(segment) {
resolveSegment: function resolveSegment (segment) {
if (cls_mode) {
return this.getSegment();
return this.getSegment()
} else if (segment && !cls_mode) {
return segment;
return segment
} else if (!segment && !cls_mode) {
contextUtils.contextMissingStrategy.contextMissing('No sub/segment specified. A sub/segment must be provided for manual mode.');
contextUtils.contextMissingStrategy.contextMissing('No sub/segment specified. A sub/segment must be provided for manual mode.')
}
},

Expand All @@ -93,19 +93,19 @@ var contextUtils = {
* @alias module:context_utils.getSegment
*/

getSegment: function getSegment() {
getSegment: function getSegment () {
if (cls_mode) {
var segment = contextUtils.getNamespace(NAMESPACE).get(SEGMENT);
const segment = contextUtils.getNamespace(NAMESPACE).get(SEGMENT)

if (!segment) {
contextUtils.contextMissingStrategy.contextMissing('Failed to get the current sub/segment from the context.');
contextUtils.contextMissingStrategy.contextMissing('Failed to get the current sub/segment from the context.')
} else if (segment instanceof Segment && process.env.LAMBDA_TASK_ROOT && segment.facade == true) {
segment.resolveLambdaTraceData();
segment.resolveLambdaTraceData()
}

return segment;
return segment
} else {
contextUtils.contextMissingStrategy.contextMissing('Cannot get sub/segment from context. Not supported in manual mode.');
contextUtils.contextMissingStrategy.contextMissing('Cannot get sub/segment from context. Not supported in manual mode.')
}
},

Expand All @@ -116,13 +116,13 @@ var contextUtils = {
* @alias module:context_utils.setSegment
*/

setSegment: function setSegment(segment) {
setSegment: function setSegment (segment) {
if (cls_mode) {
if (!contextUtils.getNamespace(NAMESPACE).set(SEGMENT, segment)) {
logger.getLogger().warn('Failed to set the current sub/segment on the context.');
logger.getLogger().warn('Failed to set the current sub/segment on the context.')
}
} else {
contextUtils.contextMissingStrategy.contextMissing('Cannot set sub/segment on context. Not supported in manual mode.');
contextUtils.contextMissingStrategy.contextMissing('Cannot set sub/segment on context. Not supported in manual mode.')
}
},

Expand All @@ -132,8 +132,8 @@ var contextUtils = {
* @alias module:context_utils.isAutomaticMode
*/

isAutomaticMode: function isAutomaticMode() {
return cls_mode;
isAutomaticMode: function isAutomaticMode () {
return cls_mode
},

/**
Expand All @@ -142,11 +142,11 @@ var contextUtils = {
* @alias module:context_utils.enableAutomaticMode
*/

enableAutomaticMode: function enableAutomaticMode() {
cls_mode = true;
contextUtils.getNamespace(NAMESPACE);
enableAutomaticMode: function enableAutomaticMode () {
cls_mode = true
contextUtils.getNamespace(NAMESPACE)

logger.getLogger().debug('Overriding AWS X-Ray SDK mode. Set to automatic mode.');
logger.getLogger().debug('Overriding AWS X-Ray SDK mode. Set to automatic mode.')
},

/**
Expand All @@ -155,14 +155,14 @@ var contextUtils = {
* @alias module:context_utils.enableManualMode
*/

enableManualMode: function enableManualMode() {
cls_mode = false;
enableManualMode: function enableManualMode () {
cls_mode = false

if (cls.getNamespace(NAMESPACE)) {
cls.destroyNamespace(NAMESPACE);
cls.destroyNamespace(NAMESPACE)
}

logger.getLogger().debug('Overriding AWS X-Ray SDK mode. Set to manual mode.');
logger.getLogger().debug('Overriding AWS X-Ray SDK mode. Set to manual mode.')
},

/**
Expand All @@ -173,47 +173,46 @@ var contextUtils = {
* Alternatively, a custom function can be supplied, which takes a error message string.
*/

setContextMissingStrategy: function setContextMissingStrategy(strategy) {
setContextMissingStrategy: function setContextMissingStrategy (strategy) {
if (!contextOverride) {
if (typeof strategy === 'string') {
var lookupStrategy = contextUtils.CONTEXT_MISSING_STRATEGY[strategy.toUpperCase()];
const lookupStrategy = contextUtils.CONTEXT_MISSING_STRATEGY[strategy.toUpperCase()]

if (lookupStrategy) {
contextUtils.contextMissingStrategy.contextMissing = lookupStrategy.contextMissing;
contextUtils.contextMissingStrategy.contextMissing = lookupStrategy.contextMissing

if (process.env.AWS_XRAY_CONTEXT_MISSING) {
logger.getLogger().debug('AWS_XRAY_CONTEXT_MISSING is set. Configured context missing strategy to ' +
process.env.AWS_XRAY_CONTEXT_MISSING + '.');
process.env.AWS_XRAY_CONTEXT_MISSING + '.')
} else {
logger.getLogger().debug('Configured context missing strategy to: ' + strategy);
logger.getLogger().debug('Configured context missing strategy to: ' + strategy)
}
} else {
throw new Error('Invalid context missing strategy: ' + strategy + '. Valid values are ' +
Object.keys(contextUtils.CONTEXT_MISSING_STRATEGY) + '.');
Object.keys(contextUtils.CONTEXT_MISSING_STRATEGY) + '.')
}
} else if (typeof strategy === 'function') {
contextUtils.contextMissingStrategy.contextMissing = strategy;
logger.getLogger().info('Configured custom context missing strategy to function: ' + strategy.name);
contextUtils.contextMissingStrategy.contextMissing = strategy
logger.getLogger().info('Configured custom context missing strategy to function: ' + strategy.name)
} else {
throw new Error('Context missing strategy must be either a string or a custom function.');
throw new Error('Context missing strategy must be either a string or a custom function.')
}

} else {
logger.getLogger().warn('Ignoring call to setContextMissingStrategy as AWS_XRAY_CONTEXT_MISSING is set. ' +
'The current context missing strategy will not be changed.');
'The current context missing strategy will not be changed.')
}
}
};
}

cls.createNamespace(NAMESPACE);
logger.getLogger().debug('Starting the AWS X-Ray SDK in automatic mode (default).');
cls.createNamespace(NAMESPACE)
logger.getLogger().debug('Starting the AWS X-Ray SDK in automatic mode (default).')

if (process.env.AWS_XRAY_CONTEXT_MISSING) {
contextUtils.setContextMissingStrategy(process.env.AWS_XRAY_CONTEXT_MISSING);
contextOverride = true;
contextUtils.setContextMissingStrategy(process.env.AWS_XRAY_CONTEXT_MISSING)
contextOverride = true
} else {
contextUtils.contextMissingStrategy.contextMissing = contextUtils.CONTEXT_MISSING_STRATEGY.RUNTIME_ERROR.contextMissing;
logger.getLogger().debug('Using default context missing strategy: RUNTIME_ERROR');
contextUtils.contextMissingStrategy.contextMissing = contextUtils.CONTEXT_MISSING_STRATEGY.RUNTIME_ERROR.contextMissing
logger.getLogger().debug('Using default context missing strategy: RUNTIME_ERROR')
}

module.exports = contextUtils;
module.exports = contextUtils
Loading

0 comments on commit 7cde0b2

Please sign in to comment.