gadget-trace-middleware
Advanced tools
Comparing version 1.0.0 to 1.1.0
34
index.js
@@ -5,3 +5,6 @@ const axios = require('axios') | ||
module.exports = function (config) { | ||
const baseUrl = `http${config.https ? 's' : ''}://${config.host}:${config.port || 4368}` | ||
const publicUrl = `http${config.https ? 's' : ''}://${config.publicHost || config.host}:${config.port || 4368}` | ||
return function (req, res, next) { | ||
const secretKeys = req.get('x-gt-secret-keys') ? req.get('x-gt-secret-keys').split(',') : [] | ||
const requestId = req.requestId || res.locals.requestId || req.get('x-request-id') || uuid() | ||
@@ -19,4 +22,20 @@ const requestUid = req.requestUid || res.locals.requestUid || uuid() | ||
if (config.secret && !secretKeys.includes(config.secret)) { | ||
req.gt.wrap = () => {} | ||
req.gt.log = () => {} | ||
return next() | ||
} | ||
res.setHeader('x-gt-host', publicUrl) | ||
const startTime = process.hrtime() | ||
req.gt.vars = function gadgetTraceVars (vars) { | ||
let variables = [] | ||
for (let name in vars) { | ||
variables.push({ name, value: vars[name] }) | ||
} | ||
req.gt.log({ variables }, requestUid) | ||
} | ||
req.gt.log = function gadgetTraceLog (data, requestUid = null) { | ||
@@ -33,7 +52,12 @@ if (!requestUid) { | ||
} | ||
if (config.secret) { | ||
data.secret = config.secret | ||
} | ||
axios({ | ||
method: 'post', | ||
url: `http://${config.host}:${config.port || 4368}/request/${requestId}`, | ||
url: `${baseUrl}/request/${requestId}`, | ||
data: data | ||
}).catch(() => {}) | ||
}).catch(() => { | ||
// TODO log the failure | ||
}) | ||
} | ||
@@ -63,7 +87,9 @@ | ||
req.gt.wrap = function (fn, name, depth = 0, maxDepth = 4) { | ||
req.gt.wrap = function (fn, name, options = {}, maxDepth = 4, depth = 0) { | ||
if (fn !== null && typeof fn === 'object') { | ||
if (depth >= maxDepth) return | ||
Object.keys(fn).forEach(key => { | ||
const wrapped = req.gt.wrap(fn[key], `${name}.${key}`, depth + 1, maxDepth) | ||
if (options.blacklist && new RegExp(options.blacklist).test(key)) return | ||
if (options.whitelist && !(new RegExp(options.whitelist).test(key))) return | ||
const wrapped = req.gt.wrap(fn[key], `${name}.${key}`, options, maxDepth, depth + 1) | ||
if (typeof fn[key] === 'function') { | ||
@@ -70,0 +96,0 @@ fn[key] = wrapped |
{ | ||
"name": "gadget-trace-middleware", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6213
133