Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Practical process of SCF Log Retrieval of Cloud function

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you the practical process of cloud function SCF log retrieval. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

In the process of cloud function development, debugging and online operation and maintenance, developers will inevitably encounter problems that need to be located when function calls fail. Usually, we use logs as the main means of troubleshooting.

In the SCF console, we can see a list of logs containing the status of function calls. You can filter directly to view all the logs that failed to call.

If we can get the RequestId of a failed request from the gateway return information, we can also retrieve the log of the specified request based on the RequestId.

This is the most basic use of log retrieval.

In the process of actually locating the problem, the following scenarios may occur:

Some of the exceptions in the function are caught, but the call state of the function is still successful. How to find the caught exception at this time?

There are a lot of function error calls. What if I just want to see the log information of some specified modules?

When I receive an alarm that the running time of my function exceeds x seconds, how can I quickly find the call log with a specified running time range?

The business log I want to view contains many different keywords. What if I want to find the log where multiple keywords are located at one time?

For the above scenarios, we can use the "advanced log" feature to solve all the above problems.

How to use advanced logs

The following is to share with you the exceptions that have been caught, find requests whose running time of the function is greater than x, and how to use advanced logs in keyword combination retrieval.

1. Caught exception

Cloud functions are often used in combination with API gateways to implement REST API. Let's explain how to use advanced logs in combination with an actual business scenario.

The following simulates a HTTP PUT request to realize the teacher's function of inputting student information.

Def teacher_put (): print ('insert info') try: fh = open ("/ tmp/testfile", "w") fh.write ("students info xxxx") except IOError: print ("Error: cannot find the file or open file failed") else: print ("write info success") fh.close () return (' teacher_put success') def main_handler (event) Context): print (str (event)) if event ["pathParameters"] ["user_type"] = "teacher": if event ["pathParameters"] ["action"] = "get": return teacher_get () if event ["pathParameters"] ["action"] = "put": return teacher_put ()

Because the IO exception when writing the file above has been caught, when the file cannot be found, the result of the function call is still successful, and the API request returns null. If you use the normal call log feature, you need to view the logs one by one, which will be very troublesome.

We print Error information when the code catches an exception, and the keyword can be retrieved directly in the advanced log:

If you want to view the full log containing the request, click the RequestId of the log:

The above describes how to find the exceptions that have been caught, and directly retrieve the keywords printed during the capture. There are no special requirements for log output, and you can use the runtime native log.

two。 Find requests with a function running time greater than x

If we receive an alarm or look through the monitoring chart to see that the running time of a function is abnormal, how can we find these logs quickly?

Advanced logs provide a method of runtime retrieval. For example, if we want to find logs whose running time is greater than 150ms, we can enter SCF_Duration > 150in the search box to find out the logs within this time range.

Time range and keyword retrieval can be combined, and all logs are filtered out in the above example.

If we only want to see how many requests run for more than 150 ms, we can search by combining keyword filtering SCF_Duration > 150 and "Report RequestId":

The above expression means to filter logs that take more than 150 ms to run and contain Report RequestId keywords. The line Report RequestId is the statement printed by the system after each request of the function, so you can use it to filter the unique request log.

If you want to filter out 150

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report