Alibaba Cloud RUM Rust SDK
alibabacloud-rum is the Alibaba Cloud RUM SDK for native Rust applications.
It is currently focused on Android native Rust processes and reqwest based
HTTP clients.
The SDK can initialize a RUM session, emit an initial view event, instrument outbound HTTP requests, inject trace headers, collect resource timing data, and flush RUM payloads to an Alibaba Cloud RUM collector.
Crate
Add the SDK to your application:
[]
= "0.1.0"
= { = "0.12.28", = false, = ["rustls-tls"] }
= { = "1", = ["macros", "rt-multi-thread"] }
The default feature set enables reqwest instrumentation. Disable default
features when you only need manual custom event/resource reporting:
[]
= { = "0.1.0", = false }
Requirements
- Rust
1.86or newer. - An async runtime compatible with
tokio. - A RUM collector address and app id from Alibaba Cloud RUM.
- For Android builds, install the matching Rust target, for example
aarch64-linux-androidforarm64-v8adevices or emulators.
Android native Rust integrations should prefer rustls TLS, as shown in the
dependency example above.
Quick Start
Initialize the SDK once and wrap your reqwest::Client:
use Duration;
use ;
async
For non-global initialization, use Rum::init(config).await? and call
rum.wrap_reqwest(reqwest::Client::new())?.
reqwest Instrumentation
With the reqwest feature enabled, the wrapper supports common request builder
methods such as get, post, put, patch, delete, header, headers,
query, json, form, body, timeout, basic_auth, and bearer_auth.
The wrapper records a resource event when the response body is consumed through
bytes, text, json, or when the wrapped response is dropped. It also
injects outbound trace headers unless tracing is disabled or the target host is
excluded.
let client = wrap_reqwest?;
let response = client
.post
.json
.send
.await?;
let body = response.text.await?;
Custom Events
Use the global helpers or the methods on a Rum instance to report custom
events, logs, exceptions, and manually measured resources.
use ;
report_custom_event
.await?;
report_custom_log
.await?;
report_custom_exception
.await?;
let trace = generate;
report_custom_resource
.await?;
Configuration
RumConfig::builder() requires config_address and app_id. Other fields are
optional:
- Application:
app_name,app_version,app_type,env. - Device and user:
utdid,user_id,user_name,user_tags,device_name,device_model,device_brand,device_type. - Platform:
os_type,os_version,net_device_model. - Extra payload properties:
property(key, value). - Exporter:
flush_interval,max_batch_size,max_queue_size,request_timeout,cache_path. - Network:
enabled,record_enabled,tracing_enabled,trace_parent,sw8,exclude_host,should_record_request,should_trace_request.
The collector host is excluded from network tracing automatically to avoid self-reporting loops.
Trace Propagation
The default propagation mode is W3C Trace Context. It injects:
traceparenttracestate
Switch to Alibaba Cloud/SkyWalking SW8 propagation when your backend expects
sw8:
let config = builder
.config_address
.app_id
.network
.build?;
For manual resources, use TraceContext and TraceHeaderWriter to generate
headers for your own business request.
Examples
Run the reqwest example against local mock services:
Run it against a real collector and a real target API:
RUM_CONFIG_ADDRESS="https://your-project.cn-hangzhou.log.aliyuncs.com/rum/web/v2" \
RUM_APP_ID="your-app-id" \
RUM_TARGET_URL="https://api.example.com/users/123" \
Run the custom event/resource example:
Use SW8 propagation in either example:
RUM_TRACE_MODE=sw8
RUM_TRACE_MODE=sw8
Android Verification
For an arm64-v8a device or emulator:
CC_aarch64_linux_android="/aarch64-linux-android34-clang" \
AR_aarch64_linux_android="/llvm-ar" \
CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="/aarch64-linux-android34-clang" \
Push and run an example:
For a standalone Android rlib consumer demo, see
demos/android-rlib-consumer/README.md.
Features
| Feature | Default | Description |
|---|---|---|
reqwest |
Yes | Enables reqwest client wrapping, trace header injection, and resource event capture. |
Public API
The main exported types and functions are:
Rum,RumConfig,RumConfigBuilder,RumEnvinit_as_global,global,flush,shutdownwrap_reqwest,RumReqwestClient,RumRequestBuilder,RumReqwestResponseCustomEvent,CustomLog,CustomLogLevel,CustomExceptionCustomResource,CustomResourceMeasuring,CustomResourceTypereport_custom_event,report_custom_log,report_custom_exception,report_custom_resourceTraceContext,TraceProtocol,TraceGenerator,TraceHeaderWriter
Development
Common checks:
The previous long-form project notes and Android troubleshooting guide were
moved to CONTRIBUTING.md.