For “operation and maintenance” programming, coding regardless of operation cost is playing hooligans!

Time:2022-1-2

Operation and maintenance oriented programming? Minghui in our group did it. He is facing Shiwei, who is the operation and maintenance brother of our technical team. Minghui is facing Shiwei every day.

The O & m here does not refer to the O & M students, but to the maintenance of our system during production and operation, which is usually understood as code tuning, log embedding, problem troubleshooting, etc.

 

 

 

This afternoon, problems were reported in the system operation group. When we checked the production log for problems, we found that there was a large text log constantly brushing the screen, which seriously interfered with our positioning of the problem.

 

The big log swiping (w è n) image (t í) has a long history. The production environment prints a large number of logs every day, and the log files are often 10g or 20g. These large texts are usually API interface signature strings, data encryption strings, image file Base64 Strings / hex strings, and list serialization strings obtained from dB or RPC interfaces.

When we do anything, we actually have to consider the cost. Production troubleshooting is especially a measure of time cost. If the cause cannot be located for a long time, it cannot be fed back and repaired in time, and small problems may also rise (y) ǎ n) Level (BI à n) is a production accident.

Therefore, we should not ignore these details in our daily coding. For logging, it is necessary to pay attention to whether the target object will output a long piece of text. If so, then we have to deal with it.

 

For example, the following two pieces of code, one is the spring MVC interface, and the program prints the obtained request parameters. One is that the program prints data messages before and after calling the external API. The communication messages of these two blocks involve large fields, so they need to be processed.

@RequestMapping(value = "/common.do", method = RequestMethod.POST)
public void doBusiness(@RequestBody String reqJson, HttpServletRequest request, HttpServletResponse response) throws IOException {
    log. Info ("merchant API pre request, request parameter = {}", reqjson);
    ....
}

 

logger. Info ("OCR identification request address: {}, parameter: {}", listauthurl, JSON);

Response response = FastHttpClient.
        post().
        addHeader("Content-Type", "application/json").
        body(json).
        url(listAuthUrl.get(0).getCode()).
        build().
        execute();

String result = response.body().string();
logger. Info ("OCR recognition return:" + result);

Small changes, big benefits!

Both reqjson and JSON above are JSON strings containing long text. So, how to deal with this situation?

The simple and crude way is to directly use substring to intercept substrings of a specified length. The disadvantage is that the necessary information may be discarded, which is not conducive to production troubleshooting.

Another way is to intercept the specific large text. The following stringutil methods are available for reference.

/**
 *Intercept large text in JSON string
 *@ param jsonstr JSON string with special length value
 * @return
 */
public static String cutLongJSONString(String jsonStr) {
    String[] split = jsonStr.split("\"");
    System.out.println(split.length);
    for (int i = 0; i < split.length; i++) {
        if (split[i].length() > 100) {
            split[i] = cutLongString(split[i], 128);
        }
    }
    return String.join("\"", split);
}

/**
 *Intercepts a large text string.
 *This method will include the length of the original string. If you don't pay attention, you can directly use {@ link org. Apache. Commons. Lang3. Stringutils #abbreviate (string, int)} or its overloaded methods
 * @param longString
 * @param length
 * @return
 */
public static String cutLongString(String longString, int length) {
    if(StringUtils.isBlank(longString)){
        return "";
    }
    StringBuilder sb=new StringBuilder();

    if (longString.length()>length){
        sb.append(longString, 0, length).append("...(length=").append(longString.length()).append(")");
        return sb.toString();
    }
    return longString;

}

Recommended Today

Explanation of websocket heartbeat reconnection

Recently, websocket has been used in the development of applet. The applet provides corresponding native API, which is different from H5 API. Therefore, some mature class libraries of popular H5 are difficult to use, and the native API has some defects, so it implements a set of heartbeat reconnection mechanism by itself. First, let’s briefly […]