go-kit 入门(三)日志和仪表化

日志和仪表化

任何服务在日志和仪表化没有就绪的情况下,都不能说是生产环境就绪的。

传输日志

任何需要日志记录的组件都需要将 logger 作为依赖,就像数据库连接一样。因此,我们在 main 函数中构造 logger 对象,然后将其传入需要使用它的组件中。我们始终不去使用一个全局的 logger 对象。

我们可以直接将 logger 传入到 stringService 的实现代码中,但是,还有一个更好的方式。我们可以使用 中间件 (middleware) ,也常常被称为 装饰者。

middleware 是一个函数,它接收一个 endpoint 作为参数,并且返回一个 endpoint。

在函数中,它可以做任何事情。下面就让我们来创建一个基本的日志中间件。

然后,我们将它加入到每一个处理函数中。

事实证明,这项技术是非常有价值的,远远不止于记录日志,Go kit 的很多模块都被实现为端点中间件。

应用日志

那么,在我们的应用中,应该如何记录日志呢?比如那些需要被传入的参数等。事实上,我们能够为我们的服务定义一个中间件,从而获得同样好的组合效果。由于我们的 StringService被定义为一个接口,我们只需要作出一个新的类型,来包装先有的 StringService,让它来执行扩充的记录日志的任务。

然后,将新的类型引入到下面的代码中:

在传输环节使用端点中间件,比如回路断路器和速率限制。在业务环节使用服务中间件,比如日志和仪表化。

仪表化

在 Go kit 中,仪表化意味着使用 包指标 来记录关于服务运行行为的状态。统计执行的任务的数量,在请求完成后记录消耗的时间,以及跟踪所有正在执行的操作的数量,都被认为是 仪表化。

我们可以使用同样的中间件模式,在记录日志的环节我们曾经用过。

然后将其引入到服务中:

stringsvc2

目前位置,完整的服务是 stringsvc2.

此条目发表在GoLang, MircroService分类目录。将固定链接加入收藏夹。

go-kit 入门(三)日志和仪表化》有 518 条评论

  1. You have to waste less time to seek out your required matter on internet, as today the searching techniques of search engines are good. That’s why I fount this paragraph at this place.

  2. Nowadays YouTube video clips quality is more improved and superior, thus that’s the motive that I am watching this video at here.

  3. Sketches are actually good source of instruction instead of content, its my experience, what would you say?

  4. I got this web site from my buddy who informed me about this web site and now this time I am browsing this web page and reading very informative posts at this place.

  5. order viagra说:

    I am truly keen of reading posts regarding creating new blog, or even concerning Web optimization.

  6. Because the admin of this website is working, no doubt very shortly it will be famous, due to its quality contents.

  7. Hello kids, you all have to watch hilarious video clips, but keep in mind that first study then enjoyment okay.

  8. I am in fact eager of reading content on the topic of creating new blog, or even about Web optimization.

  9. viagra uk说:

    Please upload new videos related to cooking if you have, since I wish for to learn more and more concerning all recipes of cooking.

  10. Very shortly this web site will be famous among all blog visitors, due to it’s good articles or reviews

  11. You have to waste less time to seek out your necessary matter on world-wide-web, as nowadays the searching techniques of search engines are fastidious. That’s why I fount this post here.

  12. I have read so many articles or reviews regarding the blogger lovers except this post is genuinely a pleasant post, keep it up.

  13. Hello, it’s understandable paragraph along with this YouTube video; I can’t imagine that one can not understand this effortless paragraph having with video sample.

  14. It’s fantastic that you are getting thoughts from this post as well as from our argument made at this time.

  15. This article will help the internet people for setting up new weblog or even a blog from start to end.

  16. What’s up, can any body assist me how to download this video tutorial from this website, I have watched and listen it at this place but wish for to down load it.

  17. For the reason that the admin of this web page is working, no question very soon it will be well-known, due to its feature contents.

  18. I all the time emailed this weblog post page to all my friends, since if like to read it next my contacts will too.

评论功能已关闭。