TLS supports uploading log data to the server via Kafka protocol. You can collect log data using the Kafka Producer SDK and upload it to TLS via Kafka protocol. This topic describes how to upload logs to TLS using Kafka protocol.
Background information
Kafka is a high-throughput message middleware that is widely used as a message pipeline in various self-hosted log collection scenarios. For example, you can use open-source collection tools on the log source server to collect logs or write log data directly with a producer, and ship them to downstream applications through a consumption pipeline. TLS supports uploading and consuming log data through the Kafka protocol; offers complete upstream and downstream data services based on the Kafka data pipeline.
If you use Kafka protocol for log uploading, you do not need to enable the feature manually or install data collection tools on the data source side. With simple configuration, a Kafka Producer will collect and upload log information to TLS. TLS provides sample projects in Java and Go language for your reference. For more details, see Example. When collecting logs with Kafka protocol, TLS parses valid JSON format logs into key-value pairs. For invalid JSON formats, some fields may be parsed incorrectly. For other log formats, the full original log will be stored as a string in the __content__ field.
Notice
When parsing JSON format logs via the Kafka protocol, only one extension layer is supported at most. Log fields with multiple nested layers will be collected and stored as a single string.
Limits
- Supported Kafka protocol versions: 0.11.x to 2.0.x.
- Supported compression types: gzip, snappy, and lz4.
- To ensure secure log transmission, you must use the SASL_SSL connection protocol. The corresponding username is your TLS project ID, and the password is the Volcanoengine account access key. For more details, see Example.
- If the log topic contains multiple shards, data sequence can not be guaranteed. We recommend using load balancing mode to upload logs.
- When using Kafka Producer Batch to send data, the size of each batch must not exceed 5 MiB. The maximum size for a single message is 5 MiB and each batch request can contain up to 10,000 messages. The server checks the length of log data written by each Producer request. If the request exceeds these limits, the entire request fails and no log data is written successfully.
Prerequisites
- Log service has been enabled; a project and a topic are created; log data has been successfully collected. For more details, see Overview.
- Ensure that your account has permission to enable log upload via Kafka protocol. That is to say, you need permission for Action PutLogs. For more information, see Authorizable operation.
When uploading logs using Kafka protocol, you need to configure the following parameters.
For better performance, we recommend adding the following Kafka producer parameters.
Configuration steps
TLS can automatically generate parameter configurations for uploading log data using Kafka protocol. After you fill in the basic configuration and data source information for uploading logs as described below, the console page automatically generates a list of corresponding parameter configurations for your reference.
- Select the region where log service is located from the top navigation bar.
- On the left navigation bar, select Common functions > Log collection.
- Then, on the Data import tab, click Create Kafka protocol write configuration.
- Select the project and topic to store Kafka data.
- Enter the data source configuration information.
-
-
- Click Confirm. The page will display Copy parameter configuration.
- The system does not directly create a writing task. Besides, writing tasks are not displayed in task list.
- To create a writing task, paste the relevant configuration in the producer and check the index configuration of the target log topic.
Notice
To ensure that the log data collected on the server side is retrieved and analyzed, you need to set up index. For details of setting indexes, see Configuration Index. Example
Upload logs using Logstash
Logstash has a built-in Kafka output plugin (logstash_output_kafka) that can collect data through Kafka protocol. In the scenario of building a log collection and analysis system with ELK, you only need to modify the Logstash configuration file to upload log data to TLS using the Kafka protocol.
If a Kafka plugin configuration example for Logstash automatically generates via TLS console, you can use this example to test the plugin connectivity. For details about parameters such as ${hosts}, see Result preview in Configuration steps. Notice
- TLS uses SASL_SSL connection protocol. Therefore, Kafka requires a SASL_SSL endpoint for message delivery and receipt.
- Supported Logstash versions: 7.12–8.8.1. If you need other Logstash versions, you can contact technical support through Ticket system.
- We recommend testing plugin connectivity with the configuration below during the testing phase. Remember to remove all stdout-related output configurations in production environments.
- For logs in JSON format, set the output format to JSON (codec => json) in your configuration; otherwise, uploaded data may be incomplete. TLS automatically parses and structures log data in JSON format.
Upload logs using Kafka Java SDK
With simple parameter configuration, you can use various Kafka Producer SDKs to collect log data and upload it to TLS via the Kafka protocol. The dependencies and sample code for uploading logs using the Kafka Java SDK are as follows:
- Add the relevant kafka-clients dependency in the pom file.
- The following code is an example of uploading logs via Kafka Java SDK.
Notice
Before running the example code, review Result Preview in Configuration steps and correctly fill in parameters such as userName. Upload logs using Kafka Go SDK
With simple parameter configuration, you can use various Kafka Producer SDKs to collect log data and upload it to TLS via the Kafka protocol. The dependencies and sample code for uploading logs with the Kafka Go SDK are as follows:
- Run the following command to install Sarama.
- Import the required dependencies such as Sarama, and upload logs.
- Refer to the following sample code to upload logs using the Kafka Go SDK.
Notice
Before running the sample code below, refer to the result preview in Configuration stepsand correctly fill in parameters such as User. Error message
If uploading logs using Kafka protocol fails, error messages will be returned based on Kafka error code. For more information, see Kafka error list. In addition, TLS also encapsulates authentication and configuration parameter error messages in the Java Kafka error code SASLAuthenticationException. Details are described as below: