preface
A few days ago, I was talking to a reader and he told me that he was insulted by the product manager. The reason was that there was a Bug on the line, which was finally known through customer feedback.
I asked him, “Are you not doing surveillance?”
Reader: We are a newly established entrepreneurial team. At present, the most important thing is the stack function, and we have no time to do a lot of infrastructure.
Is the so-called how big the bowl to eat how much rice, do not blindly pursue the large-scale, very cattle that kind of scheme, the right can. Monitoring is the same, as long as the small scheme is enough, can solve the problem, is also a very good choice.
The following describes some common exception monitoring methods:
Cost minimization
If it is a start-up team, it can use the minimum implementation cost to monitor system anomalies in real time. The so-called minimal implementation cost is that it can be implemented without relying on any three-party framework.
You can use manual burying to alarm exceptions. In this way, alarms are generated where global exceptions are handled so that unified management can be achieved.
As shown in the code:
@ExceptionHandler(value = Exception.class) @ResponseBody public ResponseData<Object> Terrorhandler (HttpServletRequest req, Exception E) {terrorHandler (HttpServletRequest req, Exception E) {Copy the code
ExceptionHandler (HttpServletRequest, HttpServletRequest, HttpServletRequest, HttpServletRequest, HttpServletRequest) Then the alarm is generated.
Abnormal Alarm Information
The exception alarm information must be detailed. When an exception occurs online, fix the problem immediately. If there is no detailed information can not reproduce the problem, it is difficult to locate and solve.
The alarm information must contain the following information:
Alarm service: Mobile-gateway Person in charge: yinJihuan Request address: http://xxx.com/xxx/xxx?id=xxx Request body: {"name": "XXX"} request header: key = value anomaly code: 500 exception types: RuntimeException exception stack: Java. Lang. RuntimeException: Com. XXX. Exception. ApplicationException: XXX information failed!Copy the code
The most important thing is the request parameters, which are needed to reproduce the error. Note that an error is reported when retrieving the request body via HttpServletRequest because the stream can only be read once.
By the time we get to the global exception class it’s already been read, so we need to do something special, write a filter to cache the value of the request body, Can org. Springframework. Web. Util. ContentCachingRequestWrapper decoration on it, Then through ContentCachingRequestWrapper access request body.
Minimum cost + consideration of performance
This process is synchronous, which more or less increases the response time. However, if the request is sent to the exception processing, it proves that the request has failed and the impact is not significant.
It doesn’t make a lot of difference, but you can optimize it a little bit. One of the most common optimizations is to switch synchronization to asynchronous operations, such as sending alarms to a separate thread pool, sending alarms to a memory queue, and sending alarms to a separate thread.
Local asynchronism may be lost. It is not a problem if several monitored messages are lost. If you do not want to lose the monitored messages, you can use an external message queue to store alarm messages.
Unified Log Monitoring
The way to minimize costs is to write a few dozen lines of code. The e downside is that every project has to have such a code, and the alarm logic is coupled to the code.
What EFK, ELK I believe you have heard of, unified log collection, centralized management. When an error occurs in each system, you only need to write the exception information to the local log. You do not need to separately alarm the fault. The alarm system determines whether an alarm is required and the alarm frequency based on the collected logs.
Unified log monitoring requires the establishment of a log platform, and the cost is relatively high. There are also open source solutions and commercial solutions.
Commercial users can use cloud services, which are simple to use and fast to access and support alarm rules of various dimensions. However, it costs a little money.
If you just want to monitor exceptions, I recommend an open source bug-tracking system. Sentry is an open source real-time bug-tracking system that helps developers monitor and fix exceptions in real time. Sentry also has a commercial version.
APM monitoring
Application Performance Management (APM) monitors service invocation chains, Performance details, and exception information.
Common APM have Skywalking, pinpoint, Cat, etc., with CAT as an example, the problem report shows the error information of the application, and the market of cat home page will show the error situation of each application by minutes, if there are a lot of mistakes, the color of the market is red. When you see a wave of red, there are too many anomalies.
Of course, CAT also has the alarm function. It is not realistic to look at the market artificially at a regular time. When there is an error, a timely alarm is meaningful. Want to learn more about cat can I look at this article: mp.weixin.qq.com/s/3mqmySr2n…
conclusion
Do a minimum cost exception monitoring, and it is estimated to be done in a day. If you don’t, you’ll be damned. It’s almost impossible not to control a bug, and programs are bound to have bugs. What we need to do is to find the bug in time at the first time, and then eliminate it.
Code word is not easy, if you can do a triple punch, thank you!
About the author: Yin Jihuan, a simple technology lover, the author of Spring Cloud Micro-service – Full stack Technology and Case Analysis, Spring Cloud Micro-service Introduction combat and Progress, the founder of the public account Monkey World.
I have organized a very complete learning material. If you are interested, you can search “Monkey World” on wechat and reply to the keyword “learning material” to obtain the Spring Cloud, Spring Cloud Alibaba, Sharing-JDBC sub-library sub-table and task scheduling framework XXL-job that I have organized. MongoDB, crawler and other related information.