Golang http log traces based on the coprocessor ID

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Recently took over a code, is the use of Golang Revel Web framework, constantly have operations, testing to harass, novice must be honest, ask must answer, to a I will go to the log text to see, "a long, constant brush screen, how to see, about what time, give an order number Bai", this is the most things I do. Really is to see the heart tired, log content is too little, the keyword is too few, too many concurrent requests, can not be confused is a request. Programmers have to use programs to liberate the workforce, and how to waste time reading the logs. With the demand point, the log records have to be standardized. Summarize several key points for logging.

1. Import print request parameter, export print return value. -Easy and front-end wrangling, what parameters are wrong, no return, etc.

2. Log keyword Explicit, time, level, line number function name. --Easy grep for various searches, location code at any time

3. A request has a unique request ID, one track, no matter how many times RPC, or function call


1.2 These two problems are very good solution, the 3rd is very troublesome, in the online search a lot of information, see a Google distributed tracking, but this is mainly based on RPC, but I think is based on the function call, a request, no matter how many layer functions are called, Then the log inside the function carries a unique ID that represents the request, and the other request calls the other ID and cannot be heavy. In view of the demand, then I think of a scheme, each time the function call to pass an ID, incoming log this way, but it is too cumbersome, but also to change all the log call place, not advisable. In fact, this problem, careful analysis becomes to find multiple requests to call multiple function calls in common, what is common 1. The request is common, but this incoming, log intrusion code is too expensive, there is a common, follow the function stack up, can catch up to the initial function of request processing, but we also have to distinguish between different requests, Can not just trace the call route, think of a way, is not able to assign an ID according to each call stack, in each character function can get the ID of the stack, but this is my idea does not know, how to implement.

Finally found there is one thing in common, every request is go a co-process, and the inside of the call each function is in this thread, so only need to get the ID of the association is not the only corresponding to this request. The problem has finally been solved.


Here are some ways to get the Golang ID.

1. https://www.zhihu.com/question/39863941/answer/83575802

2.http://blog.sgmansfield.com/2015/12/goroutine-ids/

3.https://github.com/huandu/goroutine

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.