You can use the Apptrail Application Events SDK for Rust to send audit logs from your Rust applications to your customers.
Add the dependency to your Cargo.toml
.
bash
apptrail-application-events-sdk="*"
```rust use apptrailapplicationevents_sdk::ApptrailEventsClient;
let myapikey: String = loadMySecretApiKey(); let myregion = "us-west-2".tostring();
let apptrailclient = ApptrailEventsClient::new(myregion, myapikey); ```
```rust use apptrailapplicationevents_sdk::event::{Actor, ApptrailEvent, Context, Resource};
let event = ApptrailEvent { tenantid: "custMGY4MmYzNDMtZjEwOC00OWI".tostring(), eventname: "CreateFoo".tostring(), eventtime: "2022-01-26T06:01:00Z".tostring(), actor: Some(Actor { id: "acctMmRlODllZDctM2I0Yi0".tostring(), details: Some(HashMap::from([ ("type".tostring(), Some(json!("account"))), ("name".tostring(), Some(json!("API Access"))), ])), }), resources: Some(vec![Resource { id: "repoYWI5NjkzY2UtNzI1Ny00N".tostring(), details: Some(HashMap::from([( "repositoryType".tostring(), Some(json!("V2")), )])), }]), context: Some(Context { sourceipaddress: Some("103.6.179.245".tostring()), useragent: Some("Apache-HttpClient/4.5.3 (Java/11.0.11)".tostring()), }), eventdetails: Some(HashMap::from([( "request".tostring(), Some(json!({ "repositoryName": "my-repository", })), )])), tags: Some(HashMap::from([("severity".tostring(), "LOW".to_string())])), };
apptrailclient.putevent(&event).await; ```
As a best practice, you should handle errors while sending events, especially if you are sending critical logs. The Events client includes automatic retries with backoff, but errors can happen due to rare server issues or client side issues.
You can choose what to do with failing events. For example, you may sideline them to disk, or a dead letter queue for retry or remediation.
rust
let result = events_client.put_event(event).await;
match result {
Ok(_) => (),
Err(e) => {
// handle error
println!("{}", e);
}
}