Rust SDK overview

Updated at:

The Simple Log Service (SLS) Rust SDK writes and queries logs in Rust applications and manages resources such as Projects, LogStores, and indexes. The SDK consists of two components, Client and Producer, which differ in runtime environment and write semantics. Before you choose a component, review the access prerequisites and the data reliability constraints.

Prerequisites

Before you integrate the SDK, ensure that the following requirements are met:

  • Access credentials: Use an authorized Resource Access Management (RAM) user or RAM role to access SLS, and grant only the permissions that your business requires. Do not hard-code an AccessKey or commit it to a code repository.

  • Endpoint: The Endpoint must correspond to the region of the Project. Explicitly include https:// in the Endpoint. If you do not specify a protocol, the SDK uses HTTP by default.

  • Runtime environment: Client requests must run in a Tokio asynchronous runtime. Use .send().await to wait for results. Producer does not require a separate Tokio runtime and can be used in standard synchronous or asynchronous programs.

SDK components

The Rust SDK consists of two components that you install separately:

ComponentPackageResponsibilities
Clientaliyun-log-rust-sdkCalls SLS APIs to query logs and manage resources.
Produceraliyun-log-producerContinuously collects application logs and sends them in batches in the background.

The two components are not mutually exclusive and can be used in the same application: Producer handles continuous log writes, and Client handles LogSearch and resource management.

Choose between the Client component and the Producer component

The following table compares the two components:

ItemClientProducer
ScenariosQuery logs and manage resources, or assemble a batch of logs and wait for the write result.Continuously collect application logs.
Write methodThe caller batches logs and uses .send().await to wait for the write result.Automatically merges and compresses logs, sends them in the background, and retries failed requests based on the retry policy. After an application submits logs to Producer, the application can continue processing business logic and receive the final write result through a callback.
Runtime environmentMust run in a Tokio asynchronous runtime.Can be used in standard synchronous or asynchronous programs without a separate Tokio runtime.
LogSearch and resource managementSupported.Not supported. Use Client.
Warning

Producer stores logs that are pending delivery in memory and does not write them to the local disk. If the process exits unexpectedly, logs that have not been sent may be lost. Producer does not guarantee log order, and network errors and retries may cause duplicate writes. If you need to deduplicate logs, include a unique business identifier in each log and handle deduplication in your business logic.

For continuous log writes, use Producer to reduce the batch sending and retry handling in your application. Producer does not provide LogSearch or resource management features. Use Client for these operations.

Workflow

The following table describes the workflow for integrating the SLS Rust SDK:

StepTopicDescription
1Install the Rust SDKPrepare the Rust environment and add dependencies.
2Initialize the Rust SDKConfigure the Endpoint and access credentials, and then create a Client.
3Rust SDK Getting StartedUse Client to write a log, and then query logs to confirm the write result.
4Use Rust Producer to write logsUse Producer to send logs in batches, get the final write result through a callback, adjust configurations, and exit safely.

Instance reuse and billing

  • Reuse Client and Producer instances instead of creating a new instance for each log that you write.

  • Fees for log writes, storage, and queries in SLS are charged based on the billing rules of the resources that you use.

References