简体   繁体   English

我们如何进行动态范围分析以在NodeJ中实现事务跟踪?

[英]How to we do dynamic-scoping to implement transaction tracing in NodeJs?

I am building a client and a server side framework (NodeJs) in which I want to trace transactions. 我正在建立一个客户端和服务器端框架(NodeJs),我想在其中跟踪事务。 I have the ability to pass headers (transaction_id) between client and server, however, I want to be able to set the transaction_id automatically. 我可以在客户端和服务器之间传递标头(transaction_id),但是,我希望能够自动设置transaction_id。 That means that if a header is defined, a middleware should be able to read and set the transaction id in the context so that down-stream calls can read it. 这意味着,如果定义了标头,则中间件应该能够在上下文中读取和设置事务ID,以便下游调用可以读取它。

Building wrappers is outside the scope of the question. 建筑包装材料不在问题范围内。 What I am struggling with is being able to create a scope dynamically and storing values there. 我正在努力的是能够动态创建范围并在那里存储值。

NOTE – I am using 'strict' mode which disallows dynamic scoping in node. 注–我正在使用“严格”模式,该模式不允许在节点中进行动态作用域。 So need another way. 因此需要另一种方式。 NOTE – I am using Promises to make client-server calls. 注–我正在使用Promises进行客户端-服务器呼叫。

Here is how I solved it finally – 这是我最终解决的方式–

I used CLS which allows us to keep track of dynamic scope. 我使用了CLS,它使我们能够跟踪动态范围。

Reading through all the text around CLS took a while, so here is a summary of what I did (layman terms) 阅读有关CLS的所有文本都花了一段时间,所以这里是我所做的摘要(外行术语)

NOTE - I am using NodeJs with 'strict' mode. 注意-我正在以“严格”模式使用NodeJ。 This means I can't use dynamic scoping. 这意味着我不能使用动态作用域。 Given its a production system, i'd want to keep strict mode. 鉴于其生产系统,我想保持严格的模式。 Hence an alternate way to achieve dynamic scoping. 因此是实现动态作用域的另一种方法。

1) CLS creates a dynamic context/scope. 1)CLS创建动态上下文/范围。 That allows us to then set / get key-value pairs that are visible only till we are within the created scope. 这样,我们便可以设置/获取只有在我们创建的范围内才可见的键-值对。

2) Since I am using Bluebird's Promises, CLS required me to use the patch to keep the context/scope available from within Promises. 2)由于我使用的是Bluebird的Promises,CLS要求我使用补丁以使Promises中的上下文/范围保持可用。 https://www.npmjs.com/package/cls-bluebird https://www.npmjs.com/package/cls-bluebird

3) Using CLS with Promises took time to figure out. 3)将CLS与Promises结合使用需要花费一些时间才能弄清楚。 Here is a great discussion on how different libraries have used CLS to produce different outcomes. 这是有关不同库如何使用CLS产生不同结果的精彩讨论。 https://github.com/TimBeyer/cls-bluebird/issues/6 https://github.com/TimBeyer/cls-bluebird/issues/6

4) This is how I used CLS (paraphrased and simplified) – 4)这就是我使用CLS(措辞和简化)的方式–

var cls = require('continuation-local-storage');
var clsbluebird = require('cls-bluebird');
var namespace = cls.createNamespace('ns');
clsbluebird( namespace );

var result;
namespace.run(function() {
  namespace.set('key', 'value');
  result = abc(); // returns 'value'
});

// versus doing –
result = abc(); // returns undefined

function abc() {
  return namespace.get('key');
}

5) Usecase – This way, I implemented basic tracing of transactions. 5)用例–这样,我实现了事务的基本跟踪。 Eg. 例如。 NewRelic, Trace, etc. NewRelic,Trace等

Sequelize, one of the most popular orm does this using a module continuation local storage Sequelize,最流行的orm之一,使用模块延续本地存储来实现

Hope that helps you out 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM