Updated on 2024-11-15 GMT+08:00

Building a Program

Prerequisites

(1) The IP address in the two functions is an access point of LTS. To obtain this IP address, perform the following steps:
  1. Log in to the LTS console. In the navigation pane on the left, choose Host Management > Hosts.
  2. In the upper right corner of the page, click Install ICAgent.
  3. Obtain the access point IP address in the Install ICAgent window.
    Figure 1 Access point IP address

2. Obtain the values of log_group_id and log_stream_id in the functions. For details, see Obtaining the Account ID, Project ID, Log Group ID, and Log Stream ID.

3. Create the LTS dependency required by function B. For details, see How Do I Create a Dependency on the FunctionGraph Console? and How Do I Add a Dependency to a Function? You can run the pip install huaweicloudsdklts command to create the dependency. The sample code contains the huaweicloudsdklts dependency for Python 3.9.

Creating a Function

Create a log extraction function by uploading the sample code package. Select the Python 3.9 runtime and the agency LtsOperation created in Creating an Agency. For details about how to create a function, see Creating an Event Function.

Create function A. For the sample code of this function, see the write_log.py file. In the code of function A, replace host, log_group_id, and log_stream_id with the access point and the IDs of log group test-1206 and log stream test-206, as shown in Figure 2.

Figure 2 write_log.py

Create function B. For the sample code of this function, see the lts_cleanse.py file. In the code of function B, replace host, log_group_id, and log_stream_id with the access point and the IDs of log group test-1121 and log stream test-1121, and add the huaweicloudsdklts dependency to this function, as shown in Figure 3 and Figure 4.

Figure 3 lts_cleanse.py
Figure 4 Adding a dependency for function B

This function performs Base64 decoding on received log event data, extracts alarm logs containing keyword WRN, WARN, ERR, or ERROR, and then stores the extracted logs to a specified LTS log stream. Set log extraction conditions based on the content of your service logs.