关联度
如何使用
创建关联 id 实例
import {createCorrelationId} from '@vizydrop/correlation-id';
const correlationId = createCorrelationId();
与 @fibery/vizydrop-logger
const {createLogger} = require(`@fibery/vizydrop-logger`);
const logger = createLogger({
correlationId: {
enabled: true,
getCorrelationId: () => correlationId.correlator.getId(),
emptyValue: `nocorrelation`,
},
});
注册中间软件。 支助 koa 财务报告和审定财务报表 express
// koa
app.use(correlationId.koaMiddleware);
// express
app.use(correlationId.expressMiddleware);
增强请求, 因此每个请求将包含关联 id http header .
const request = require(`request`);
const correlatedRequest = correlationId.enhanceRequest(request);
correlatedRequest.get(`http://anotherservice:10020/data`);
增强 http 代理服务器, 因此每个代理请求将包含关联 id http header .
const httpProxy = require(`http-proxy`);
const proxy = httpProxy.createProxyServer({
target: `http://anotherservice:10020/`,
});
correlationId.enhanceHttpProxy(proxy);
运行工作。 工作通常不会完成 express/koa 中间软件的关联性 id 应该手动生成.
function jobTask() {}
function runJob() {
correlationId.correlator.withId(correlator.generateDefaultId(), () => {
jobTask();
});
}
设置
自定义设置可以作为对象传递到 createCorrelationId 函数.
generateDefaultId- 函数,该函数应返回新的相关性 ID。 默认crypto.randomUUID已使用.
相关标识 API
expressMiddleware- 表示在关联范围内运行下个中间软件的中间软件 id async hookkoaMiddleware- 运行关联范围内下个中间软件的可口中间软件enhanceHttpRequest- 接收请求并返回默认添加关联性 id 头的新请求实例enhanceHttpProxy- 默认添加关联 id 头的附加收听器correlator.getId()- 返回当前相关性 idcorrelator.withId(id, fn)- 运行函数和所有后继函数,并指定相关性 idcorrelator.bind()- 将所提供的功能与当前执行上下文绑定在一起。 其实现在它是一个简单的化名 对AsyncResource。 绑定 https://nodejs.org/api/async context.html#整合-asyncresources- with-eventemitter (中文(简体) )correlator.generateId()- 生成新的关联 id
5.0.0后已知问题
自兹 bindEmitter 方法被取消了,我们没有绑定 req 在快取和可口中间软件中 可能有一些边缘情况 关联识别丢失.
根据 https://github.com/nodejs/node/issues/33723 页面存档备份,存于互联网档案馆 不建议绑定整个发射体,因为在不同上下文中执行可能是有意的,因此包件应该保持正确的同步上下文。 它实际上被固定在许多包 像快递.js,身体-parser, 完成时,收紧等.
在大多数情况下,如果这仍然是个问题,这就可以解决了 correlator.bind (见 https://nodejs.org/api/async context.html#整合-asyncresources- with-eventemitter (中文(简体) )) (中文(简体) ). 以下例子说明如何为已死的koa-morgan包工作:
<!-- WAS -->
app.use(morgan(
`:method :status :url (:res[content-length] bytes) :response-time ms`,
{
stream: {
write: (text) => logger.info(text.trim()), // no correlation id here, context is lost
},
immediate: false,
},
));
<!-- Should become -->
app.use(async (ctx, next) => {
await morgan(
`:method :status :url (:res[content-length] bytes) :response-time ms`,
{
stream: {
write: correlator.bind((text) => logger.info(text.trim())), // correlation-id is here as we bound the callback to current async context
},
immediate: false,
},
)(ctx, next);
});
已知问题 4.3.0.
- 工作不顺利
bluebird.promisifyAll. 替代办法是明确使用本地承诺进行混合
const redis = require(`redis`);
const util = require(`util`);
const client = redis.createClient();
client.setAsync = util.promisify(client.set).bind(client);
client.getAsync = util.promisify(client.get).bind(client);
- 工作不顺利
mongoose召回电话。 另一种解决办法是使用混合函数.
const mongoose = require(`mongooose`);
mongoose.Promise = global.Promise;
EntityModel.find({name: `name`}).then((value) => {
// do something
});