简体   繁体   English

如何使用微服务跟踪Node.js中的数据

[英]How to track data in nodejs with microservices

I'm dealing with an application using multiple microservices. 我正在处理使用多个微服务的应用程序。

The application receieves data and processes it, and I'd like to have a way to monitor everything the received data is going throughout the microservices. 该应用程序接收数据并对其进行处理,我希望有一种方法来监视接收到的数据在整个微服务中进行的所有操作。

Since the platform is nodejs we're dealing with asynchronous processing, logs wont always be written one after the other, and since the data does not have aמ ID the logs cannot be filtered in any convenient way. 由于平台是nodejs我们正在处理异步处理,因此日志不会总是一个接一个地写入,并且由于数据没有ID,因此无法以任何方便的方式过滤日志。

Is there any way to track the data from the first moment it enters the application till the end, moving between the microservices without having to pass a generate id between all the methods. 在微服务之间移动时,是否有任何方法可以跟踪数据从进入应用程序的第一刻到结束, 不必在所有方法之间传递生成ID。

Obviously that id will be passed between the microserives but within each one, I'd like to keep the code clean. 显然,id将在微指令之间传递,但是在每个微指令中,我想保持代码的清洁。

I'm using winston to log, maybe something could fit there. 我正在使用winston进行日志记录,也许那里可能有适合的记录。

Thanks in advance, any suggestion would be highly appreciated. 在此先感谢您的任何建议,我们将不胜感激。

Is there any way to track the data from the first moment it enters the application till the end, moving between the microservices without having to pass a generate id between all the methods. 在微服务之间移动,而不必在所有方法之间传递生成ID,有什么方法可以跟踪从数据进入应用程序的第一刻到结束的数据。

No, because none of your microservices could know which of the other requests are related to the current one, unless you generate some id and pass it between all of them. 不可以,因为除非您生成一些id并将其在所有请求之间传递,否则您的微服务都无法知道其他哪些请求与当前请求有关。

If you add some request ID that is optional, then the microservise that don't have it in the request may generate it randomly (using a UUID for example) and then include it in all of the logs and pass it in requests to all other microservices, that in turn would use the same ID instead of generating a random one. 如果添加一些可选的请求ID,则在请求中没有该请求的微服务可能会随机生成(例如,使用UUID),然后将其包含在所有日志中,并将其传递给其他所有请求微服务,它们将使用相同的ID而不是生成随机ID。

Without such an ID you will not have enough context to group all of the requests together, especially between different microservices. 没有这样的ID,您将没有足够的上下文将所有请求分组在一起,尤其是在不同的微服务之间。 Depending on the framework you're using it can be very easy to write a middleware to handle that for you. 根据您使用的框架,编写中间件来为您轻松地进行处理可能非常容易。 Since you didn't say which framework you're using it's impossible to give you a more specific answer. 由于您没有说明要使用哪个框架,因此无法为您提供更具体的答案。

By framework, rsp meant Express, Hapy, Restify, etc. Generically speaking, the idea would be that the service receives the request id in a header (x-request-id). 就框架而言,rsp表示Express,Hapy,Restify等。从广义上讲,这种想法是服务将在标头(x-request-id)中接收请求ID。 For each request a logger instance is initialised with the id which it will include in any logs. 对于每个请求,记录器实例都会使用ID初始化,该ID将包含在任何日志中。

In your app you need a way of accessing the single logger wherever you want to log from. 在您的应用中,您需要一种方法,无论您想从哪里登录,都可以访问单个记录器。 I would suggest logging only happens in the route handlers so attaching the logger to the request object would likely be the way to go. 我建议仅在路由处理程序中进行日志记录,因此将记录器附加到请求对象可能是可行的方法。

Restify handles a lot of this by default. Restify默认情况下会处理很多此类情况。 Hapi would need a plugin, and for Express you would use middleware. Hapi将需要一个插件,而对于Express,则将使用中间件。

You will also then need to ensure that any requests to your other services include the request id header. 然后,您还需要确保对其他服务的任何请求都包括请求ID标头。

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

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