If you want to compress logs and upload compressed logs to Simple Log Service to reduce the usage of network transmission resources when you use big data computing engines such as Flink, Spark, and Storm, Simple Log Service API and Simple Log Service SDK are not recommended because the API and SDK cannot meet the requirements for data writing in big data scenarios. In this case, you can use Aliyun Log Go Producer to upload logs to Simple Log Service in an efficient manner.
Prerequisites
A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.
The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables in Linux, macOS, and Windows.
ImportantThe AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.
We recommend that you do not save the AccessKey ID or AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of all resources within your account may be compromised.
Simple Log Service SDK for Go is installed. For more information, see Install Simple Log Service SDK for Go.
The proto dependency is installed. You can run the
go get -u github.com/gogo/protobuf/protocommand to install the proto dependency.
Usage notes
In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used, which is cn-hangzhou.log.aliyuncs.com.
If you want to access Simple Log Service by using other Alibaba Cloud services that reside in the same region as your project, you can use the internal Simple Log Service endpoint, which is cn-hangzhou-intranet.log.aliyuncs.com.
For more information about the mappings between the endpoints and regions that are supported by Simple Log Service, see Endpoints.
Introduction to Aliyun Log Go Producer
Aliyun Log Go Producer is a high-performance class library provided for Go applications that run in big data and high concurrency scenarios. Compared with Simple Log Service API and Simple Log Service SDK, Aliyun Log Go Producer provides multiple benefits that are related to log writing, such as high performance, logic isolation between computing and I/O, and resource management. Aliyun Log Go Producer uses the sequential writing feature provided by Simple Log Service to ensure the upload sequence of logs.
The following figure shows the workflow of Aliyun Log Go Producer:
Limits
The underlying mechanism of Aliyun Log Go Producer calls the PutLogs operation to upload logs. The size of raw logs that can be uploaded each time is limited. For more information, see Data read and write.
The basic resources of Simple Log Service, such as projects, logstores, shards, and machine groups, also have limitations. For more information, see Basic resources.
The first time you run code, you must enable the indexing feature for your logstore in the Simple Log Service console. Then, wait for about one minute before querying logs.
If you query logs in the Simple Log Service console and the value length of a field in the returned logs exceeds the upper limit, the field value will be truncated, and the excess part will not be used for analysis. For more information, see Create indexes.
1. Configure the ProducerConfig data structure
2. Start a producer instance
3. Call a send method to deliver logs to Simple Log Service
4. Shut down a producer instance
References
If the response that is returned by Simple Log Service contains error information after you call an API operation, the call fails. You can fix the error based on the error code description of the related API operation. For more information, see Error codes.
For more information about sample code, see Aliyun Log Go Producer on Github.