fluentci_logging/
baselime.rs

1use std::env;
2
3use anyhow::Error;
4use reqwest::blocking::Client;
5use serde::Serialize;
6use uuid::Uuid;
7
8const URL: &str = "https://events.baselime.io/v1/logs";
9
10pub enum Level {
11    Info,
12    Error,
13}
14
15#[derive(Serialize, Default)]
16pub struct Payload {
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub duration: Option<u128>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub message: Option<String>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub error: Option<String>,
23    #[serde(rename = "requestId")]
24    pub request_id: String,
25    pub namespace: String,
26    #[serde(skip_serializing_if = "Option::is_none", rename = "gitCommitMessage")]
27    pub git_commit_message: Option<String>,
28    #[serde(skip_serializing_if = "Option::is_none", rename = "gitBranch")]
29    pub git_branch: Option<String>,
30    #[serde(skip_serializing_if = "Option::is_none", rename = "gitCommitHash")]
31    pub git_commit_hash: Option<String>,
32    #[serde(skip_serializing_if = "Option::is_none", rename = "gitRemoteUrl")]
33    pub git_remote_url: Option<String>,
34    #[serde(skip_serializing_if = "Option::is_none", rename = "gitAuthor")]
35    pub git_author: Option<String>,
36}
37
38pub struct Baselime {
39    client: Client,
40}
41
42pub fn new() -> Baselime {
43    let client = reqwest::blocking::Client::new();
44    Baselime { client }
45}
46
47impl Baselime {
48    pub fn send(&self, data: String, level: Level, namespace: String) -> Result<(), Error> {
49        match env::var("BASELIME_API_KEY") {
50            Ok(api_key) => {
51                let json = match level {
52                    Level::Info => serde_json::to_string(&vec![Payload {
53                        message: Some(data),
54                        request_id: Uuid::new_v4().to_string(),
55                        namespace,
56                        git_commit_message: Some(
57                            env::var("GIT_COMMIT_MESSAGE").unwrap_or_default(),
58                        ),
59                        git_branch: Some(env::var("GIT_BRANCH").unwrap_or_default()),
60                        git_commit_hash: Some(env::var("GIT_COMMIT_HASH").unwrap_or_default()),
61                        git_remote_url: Some(env::var("GIT_REMOTE_URL").unwrap_or_default()),
62                        git_author: Some(env::var("GIT_AUTHOR").unwrap_or_default()),
63                        ..Default::default()
64                    }])?,
65                    Level::Error => serde_json::to_string(&vec![Payload {
66                        error: Some(data.to_string()),
67                        request_id: Uuid::new_v4().to_string(),
68                        namespace,
69                        git_commit_message: Some(
70                            env::var("GIT_COMMIT_MESSAGE").unwrap_or_default(),
71                        ),
72                        git_branch: Some(env::var("GIT_BRANCH").unwrap_or_default()),
73                        git_commit_hash: Some(env::var("GIT_COMMIT_HASH").unwrap_or_default()),
74                        git_remote_url: Some(env::var("GIT_REMOTE_URL").unwrap_or_default()),
75                        git_author: Some(env::var("GIT_AUTHOR").unwrap_or_default()),
76                        ..Default::default()
77                    }])?,
78                };
79                self.client
80                    .post(URL)
81                    .header("x-api-key", api_key)
82                    .header("x-service", "fluentci-core")
83                    .header("Content-Type", "application/json")
84                    .body(json)
85                    .send()?;
86                Ok(())
87            }
88            Err(e) => Err(Error::msg(format!("BASELIME_API_KEY not set: {}", e)))?,
89        }
90    }
91
92    pub fn send_with_duration(
93        &self,
94        data: String,
95        level: Level,
96        namespace: String,
97        duration: u128,
98    ) -> Result<(), Error> {
99        match env::var("BASELIME_API_KEY") {
100            Ok(api_key) => {
101                let json = match level {
102                    Level::Info => serde_json::to_string(&vec![Payload {
103                        message: Some(data),
104                        request_id: Uuid::new_v4().to_string(),
105                        namespace,
106                        git_commit_message: Some(
107                            env::var("GIT_COMMIT_MESSAGE").unwrap_or_default(),
108                        ),
109                        git_branch: Some(env::var("GIT_BRANCH").unwrap_or_default()),
110                        git_commit_hash: Some(env::var("GIT_COMMIT_HASH").unwrap_or_default()),
111                        git_remote_url: Some(env::var("GIT_REMOTE_URL").unwrap_or_default()),
112                        git_author: Some(env::var("GIT_AUTHOR").unwrap_or_default()),
113                        duration: Some(duration),
114                        ..Default::default()
115                    }])?,
116                    Level::Error => serde_json::to_string(&vec![Payload {
117                        error: Some(data.to_string()),
118                        request_id: Uuid::new_v4().to_string(),
119                        namespace,
120                        git_commit_message: Some(
121                            env::var("GIT_COMMIT_MESSAGE").unwrap_or_default(),
122                        ),
123                        git_branch: Some(env::var("GIT_BRANCH").unwrap_or_default()),
124                        git_commit_hash: Some(env::var("GIT_COMMIT_HASH").unwrap_or_default()),
125                        git_remote_url: Some(env::var("GIT_REMOTE_URL").unwrap_or_default()),
126                        git_author: Some(env::var("GIT_AUTHOR").unwrap_or_default()),
127                        duration: Some(duration),
128                        ..Default::default()
129                    }])?,
130                };
131                self.client
132                    .post(URL)
133                    .header("x-api-key", api_key)
134                    .header("x-service", "fluentci-core")
135                    .header("Content-Type", "application/json")
136                    .body(json)
137                    .send()?;
138                Ok(())
139            }
140            Err(e) => Err(Error::msg(format!("BASELIME_API_KEY not set: {}", e)))?,
141        }
142    }
143}