Struct Client

Source
pub struct Client { /* private fields */ }
Expand description

Aliyun Log Service client

§Examples

A simple example of creating a new client:

use aliyun_log_rust_sdk::{Client, Config, FromConfig};
let config = Config::builder()
    .endpoint("cn-hangzhou.log.aliyuncs.com")
    .access_key("access_key_id", "access_key_secret")
    .build()?;
let client = Client::from_config(config)?;

Example of creating a new client with security token and request_timeout:

use aliyun_log_rust_sdk::{Client, Config, FromConfig};
let config = Config::builder()
    .endpoint("cn-hangzhou.log.aliyuncs.com")
    .sts("access_key_id", "access_key_secret", "security_token")
    .request_timeout(std::time::Duration::from_secs(60))
    .build()?;
let client = Client::from_config(config)?;

For more configuration options, see ConfigBuilder.

Implementations§

Source§

impl Client

Source

pub fn pull_logs( &self, project: impl AsRef<str>, logstore: impl AsRef<str>, shard_id: i32, ) -> PullLogsRequestBuilder

Pull logs from a shard of a logstore from the given cursor.

This method allows retrieving logs from a specific shard within a logstore, starting from a specified cursor position. It supports filtering logs with a query, limiting the number of logs retrieved, and controlling the log range with start and end cursors.

§Arguments
  • project - The name of the project containing the logstore
  • logstore - The name of the logstore to pull logs from
  • shard_id - The ID of the shard to pull logs from
§Examples
§Basic usage:
use aliyun_log_rust_sdk::get_cursor_models::CursorPos;
let shard_id = 0;

// First, get a cursor for a shard
let resp = client.get_cursor("my-project", "my-logstore", shard_id)
    .cursor_pos(CursorPos::Begin)
    .send().await?;
let cursor = resp.get_body().cursor();

// Then, pull logs using that cursor
let resp = client.pull_logs("my-project", "my-logstore", shard_id)
    .cursor(cursor)
    .count(100)  // Returns up to 100 log groups
    .query("* | where level = 'ERROR'")  // Optional: filter logs with a SPL query
    .send().await?;

// Print the pulled logs
for log_group in resp.get_body().log_group_list() {
    for log in log_group.logs() {
        println!("Log time: {}", log.time());
        for content in log.contents() {
            println!("  {}: {}", content.key(), content.value());
        }
    }
}

// To continue pulling logs, use the next cursor
let next_cursor = resp.get_body().next_cursor();
println!("{}", next_cursor);
§Advanced usage with cursor range:
use aliyun_log_rust_sdk::get_cursor_models::CursorPos;
let shard_id = 0; // Shard ID to pull logs from
// Get start and end cursors (for a time range)
let resp = client.get_cursor("my-project", "my-logstore", shard_id)
    .cursor_pos(CursorPos::UnixTimeStamp(1700000000))
    .send().await?;
let start_cursor = resp.get_body().cursor();

let resp = client.get_cursor("my-project", "my-logstore", shard_id)
    .cursor_pos(CursorPos::UnixTimeStamp(1700001234))
    .send().await?;
let end_cursor = resp.get_body().cursor();

// Pull logs between [begin_cursor, end_cursor)
let resp = client.pull_logs("my-project", "my-logstore", shard_id)
    .cursor(start_cursor)
    .end_cursor(end_cursor)
    .count(1000)
    .send().await?;

println!("Retrieved {} log groups", resp.get_body().log_group_count());
Source§

impl Client

Source

pub fn put_logs( &self, project: impl AsRef<str>, logstore: impl AsRef<str>, ) -> PutLogsRequestBuilder

Write logs to a logstore.

This method allows sending logs to the specified logstore in an Aliyun Log Service project. Logs are sent as a LogGroup which can contain multiple individual log entries. The data is automatically compressed using LZ4 before transmission to optimize bandwidth usage.

§Arguments
  • project - The name of the project containing the logstore
  • logstore - The name of the logstore to write logs to
§Examples

Basic usage with a single log entry:

use aliyun_log_sdk_protobuf::{Log, LogGroup};

let mut log = Log::from_unixtime(chrono::Utc::now().timestamp() as u32);
log.add_content_kv("level", "info")
    .add_content_kv("message", "Hello from Rust SDK")
    .add_content_kv("service", "user-service");

let mut log_group = LogGroup::new();
log_group.add_log(log);

client.put_logs("my-project", "my-logstore")
    .log_group(log_group)
    .send().await?;

println!("Logs sent successfully");
Source§

impl Client

Source

pub fn get_cursor( &self, project: impl AsRef<str>, logstore: impl AsRef<str>, shard_id: i32, ) -> GetCursorRequestBuilder

Get a cursor for a shard of an existing logstore.

This method retrieves a cursor that represents a specific position in a shard. The cursor can be used to read logs from that position using the pull_logs method. You can get a cursor pointing to the beginning of the shard, the end of the shard, or at a specific time.

§Arguments
  • project - The name of the project containing the logstore
  • logstore - The name of the logstore containing the shard
  • shard_id - The ID of the shard to get a cursor for
§Examples

Getting a cursor at different positions in a shard:

use aliyun_log_rust_sdk::get_cursor_models::CursorPos;
let shard_id = 0;
let resp = client
    .get_cursor("my-project", "my-logstore", shard_id)
    .cursor_pos(CursorPos::Begin) // Get cursor at the beginning of the shard
    .send()
    .await?;

println!("Begin cursor: {}", resp.get_body().cursor());
Source§

impl Client

Source

pub fn list_shards( &self, project: impl AsRef<str>, logstore: impl AsRef<str>, ) -> ListShardsRequestBuilder

List all shards of a logstore.

This method retrieves information about all shards in the specified logstore, including their IDs, status, key ranges, and creation times. Shards are the basic read and write units in Aliyun Log Service and are used to partition data for parallel processing.

§Arguments
  • project - The name of the project containing the logstore
  • logstore - The name of the logstore to list shards for
§Examples

Basic usage:

// List all shards in the specified logstore
let resp = client.list_shards("my-project", "my-logstore").send().await?;
println!("Found {} shards", resp.get_body().shards().len());

for shard in resp.get_body().shards() {
    println!("Shard ID: {}, Status: {}", shard.shard_id(), shard.status());
}
Source§

impl Client

Source

pub fn get_logs( &self, project: impl AsRef<str>, logstore: impl AsRef<str>, ) -> GetLogsRequestBuilder

Get logs from a logstore using the given query.

This method allows you to query logs from a specific logstore within a project. It supports various query parameters including time range, filtering, and pagination. The query syntax follows the Aliyun Log Service query language.

§Arguments
  • project - The name of the project containing the logstore
  • logstore - The name of the logstore to query logs from
§Examples

Basic query with time range, offset, limit, and filter:

use aliyun_log_rust_sdk::GetLogsRequest;
use chrono::Utc;

let now = Utc::now().timestamp();
let one_hour_ago = now - 3600;
let resp = client.get_logs("my-project", "my-logstore")
    .from(one_hour_ago)         // Start time (required)
    .to(now)                    // End time (required)
    .query("level:ERROR")       // Filter for error logs only
    .offset(0)                  // Start from the first log
    .lines(100)                 // Return up to 100 logs
    .send()
    .await?;

// Check if the query completed successfully
if resp.get_body().is_complete() {
    println!("Query completed successfully");
} else {
    println!("Query is incomplete, you may need to retry later");
}

// Process the returned logs
println!("Retrieved {} logs", resp.get_body().logs_count());

for log in resp.get_body().logs() {
    // Each log is a HashMap<String, String>, print all fields in the log
    for (key, value) in log {
        println!("  {}: {}", key, value);
    }
}

Trait Implementations§

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T