Commit e504d2f8 authored by 肖人杰's avatar 肖人杰

Merge branch 'master' of git-ssh.devops.yitong.com:yitong-public/nodejs-yitong-sdk

parents 8486d785 ce061846
{ {
"name": "nodejs-yitong-sdk", "name": "nodejs-yitong-sdk",
"version": "1.0.5", "version": "1.0.7",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
......
...@@ -28,8 +28,6 @@ class ApiError extends Error { ...@@ -28,8 +28,6 @@ class ApiError extends Error {
} }
} }
/** /**
* YiTong SDK HTTP * YiTong SDK HTTP
*/ */
...@@ -43,19 +41,42 @@ const http = axios.create({ ...@@ -43,19 +41,42 @@ const http = axios.create({
httpAgent: new require('http').Agent({ keepAlive: true }), httpAgent: new require('http').Agent({ keepAlive: true }),
httpsAgent: new require('https').Agent({ keepAlive: true }) httpsAgent: new require('https').Agent({ keepAlive: true })
}); });
// koa opentracing // URL转换 域名 转 k8s serviceName
// https://github.com/fapspirit/axios-opentracing if (process.env.YT_SERVICE_RULES) {
http.interceptors.request.use(function (config) { const { serviceRuleMap } = require(process.env.YT_SERVICE_RULES);
if(global.serviceMap) { if (_.size(serviceRuleMap) > 0) {
let paths = config.url.split("/") let rules = new Map();
if(paths.length >= 4){ for (const key of Object.keys(serviceRuleMap)) {
let pathKey = paths[0] + "//" + paths[2] + "/" + paths[3] + "/" const value = serviceRuleMap[key];
if(global.serviceMap[pathKey]) { rules.set(key, value);
config.url = config.url.replace(pathKey, global.serviceMap[pathKey]) if (key.startsWith("http://")) {
rules.set(`https://${key.substring(7)}`, value);
} }
} }
let rulesCache = new Map();
http.interceptors.request.use(function (config) {
let url = config.url;
if (url) {
let newUrl = rulesCache.get(config.url);
if (!newUrl) {
for (const [key, value] of rules) {
if (url.startsWith(key)) {
newUrl = `${value}${url.substring(key.length)}`
rulesCache.set(url, newUrl)
}
}
}
if (newUrl) {
config.url = newUrl;
}
}
return config;
});
} }
}
// koa opentracing
// https://github.com/fapspirit/axios-opentracing
http.interceptors.request.use(function (config) {
if (config.context && config.context.span) { if (config.context && config.context.span) {
let tracer = opentracing.globalTracer(); let tracer = opentracing.globalTracer();
let span = tracer.startSpan(`${config.method}: ${config.url}`, { let span = tracer.startSpan(`${config.method}: ${config.url}`, {
...@@ -188,7 +209,16 @@ function errorHandler() { ...@@ -188,7 +209,16 @@ function errorHandler() {
} catch (e) { } catch (e) {
ctx.status = 200; ctx.status = 200;
ctx.body = transformError(e); ctx.body = transformError(e);
log.error(e); log.error({
url: ctx.request.url,
body: ctx.request.body,
headers: {
"user-agent": ctx.headers["user-agent"],
"x-real-ip": ctx.headers["x-real-ip"],
"x-forwarded-for": ctx.headers["x-forwarded-for"]
},
error: e
});
} }
}; };
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment