loki_rs/
lib.rs

1mod push;
2
3type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
4
5/// The Loki client
6pub struct Loki {
7    /// The URI at which the Loki server resides
8    address: String
9}
10
11impl Loki {
12
13    pub fn new(address: String) -> Self {
14        Self { address }
15    }
16
17    pub async fn push_log(&self, stream_name: String, stream_value: String, log_messages: Vec<[String; 2]>) -> Result<()> {
18        // Wrapper for the push_log function
19        let result = push::push_log(self.address.clone(), stream_name, stream_value, log_messages)
20            .await?;
21        
22        Ok(result)
23    }
24}
25 
26// Simple test to ensure that the client can push a log to Loki
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use std::time::SystemTime;
31
32    #[async_std::test]
33    async fn simple_push() {
34        let client = Loki::new("http://localhost:3100".to_string());
35
36        let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
37            .unwrap()
38            .as_nanos();
39
40        let result = client.push_log("foo".to_string(), "bar".to_string(),  vec![[timestamp.to_string(), "Test log!".to_string()]])
41            .await;
42
43        if !result.is_ok() {
44            println!("Error: {:?}", result);
45        }
46        
47        assert_eq!(result.is_ok(), true);
48    }
49}