Compare commits

...

3 Commits

Author SHA1 Message Date
Ryan 8e1b46fee0 0.1.5 1 year ago
Ryan ceeb7406d4 0.1.4 1 year ago
Ryan 9ab5afd99e 0.1.3 1 year ago
  1. 7
      Cargo.toml
  2. 14
      RELEASE_NOTE.md
  3. 3
      src/lib.rs
  4. 76
      src/log.rs
  5. 4
      src/metrics.rs
  6. 30
      src/s3.rs

7
Cargo.toml

@ -1,6 +1,6 @@
[package]
name = "device-common"
version = "0.1.2"
version = "0.1.5"
edition = "2021"
authors = ["Ryan Bae <jh.bae@anypointmedia.com>"]
@ -13,8 +13,6 @@ aws-sdk-kinesis = { version = "1.36", features = ["behavior-version-latest"] }
aws-sdk-ssm = "1.40"
aws-types="1.3"
aws-credential-types = { version = "1.2", features = ["hardcoded-credentials"] }
rusoto_core = "0.48"
rusoto_s3 = "0.48"
deadpool-postgres = "0.14"
tokio-postgres = { version = "0.7", features = ["with-chrono-0_4"] }
tracing = "0.1"
@ -24,10 +22,9 @@ tracing-appender = "0.2"
thiserror = "1.0"
tokio = { version = "1.39", features = ["full"] }
lapin = "2.5"
sysinfo = "0.30"
sysinfo = "0.31"
prometheus = "0.13"
dashmap = "6.0"
toml = "0.8"
serial_test = "3.1"
async-trait = "0.1"

14
RELEASE_NOTE.md

@ -9,4 +9,16 @@ first deploy
0.1.2
======
- create log directory
- create log directory
0.1.3
======
- remove s3 dependency(rusoto_core, rusoto_s3)
0.1.4
======
- fix bug for using toml library
0.1.5
======
- fix bug for log

3
src/lib.rs

@ -8,5 +8,4 @@ pub mod env;
pub mod orchestration;
pub mod metrics;
pub mod parameter_store;
pub mod constants;
mod s3;
pub mod constants;

76
src/log.rs

@ -1,55 +1,27 @@
use std::env::current_exe;
use std::fs;
use std::{env, fs};
use std::path::Path;
use tracing::{error, Level, subscriber};
use tracing_appender::non_blocking::{WorkerGuard};
use tracing_appender::rolling::{RollingFileAppender, Rotation};
use tracing_unwrap::{OptionExt, ResultExt};
use crate::constants::{FAILOVER_LOG_LEVEL_KEY, GATEWAY_LOG_LEVEL_KEY, NODE_LOG_LEVEL_KEY, TICKET_LOG_LEVEL_KEY};
use crate::parameter_store::get_parameter;
fn read_cargo_toml() -> toml::Value{
let cargo_toml_content = fs::read_to_string("Cargo.toml")
.expect_or_log("Failed to read Cargo.toml file");
let cargo_toml: toml::Value = toml::from_str(&cargo_toml_content)
.expect_or_log("Failed to parse Cargo.toml");
cargo_toml
}
fn get_app_name() -> Option<String> {
let cargo_toml = read_cargo_toml();
let package = cargo_toml
.get("package")
.expect("`[package]` section is missing in Cargo.toml");
let name = package
.get("name")
.expect_or_log("`name` field is missing in Cargo.toml");
let name_str = name
.as_str()
.expect_or_log("Package name is not a string");
Option::from(name_str.to_string())
}
fn get_log_level_parameter_key() -> Option<String> {
let mut ret: Option<String> = None;
match get_app_name() {
None => { ret = None }
Some(app_name) => {
match app_name.as_str() {
"device-gateway" => { ret = Option::from(GATEWAY_LOG_LEVEL_KEY.to_string()) }
"device-node" => { ret = Option::from(NODE_LOG_LEVEL_KEY.to_string()) }
"device-ticket" => { ret = Option::from(TICKET_LOG_LEVEL_KEY.to_string()) }
"device-failover" => { ret = Option::from(FAILOVER_LOG_LEVEL_KEY.to_string()) }
_ => { return ret }
}
}
let exe_path = current_exe().expect("Failed to get current executable");
let program_name = exe_path.file_name()
.unwrap_or_else(|| std::ffi::OsStr::new("unknown"))
.to_string_lossy()
.to_string();
match program_name.as_str() {
"device-gateway" => { ret = Option::from(GATEWAY_LOG_LEVEL_KEY.to_string()) }
"device-node" => { ret = Option::from(NODE_LOG_LEVEL_KEY.to_string()) }
"device-ticket" => { ret = Option::from(TICKET_LOG_LEVEL_KEY.to_string()) }
"device-failover" => { ret = Option::from(FAILOVER_LOG_LEVEL_KEY.to_string()) }
_ => { return ret }
}
ret
@ -112,4 +84,26 @@ fn get_log_dir() -> String {
current_dir.pop();
current_dir.push("log");
current_dir.display().to_string()
}
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
fn setup() {
}
fn teardown() {
}
#[test]
#[serial]
fn test_get_log_level_parameter_key() {
setup();
assert!(get_log_level_parameter_key().is_none());
teardown();
}
}

4
src/metrics.rs

@ -1,6 +1,6 @@
use sysinfo::System;
use prometheus::{register_gauge, Gauge};
use std::sync::{Arc, OnceLock};
use std::sync::OnceLock;
pub fn get_cpu_usage() -> &'static Gauge {
static GAUGE: OnceLock<Gauge> = OnceLock::new();
@ -33,7 +33,7 @@ pub fn get_memory_usage() -> &'static Gauge {
pub fn collect(system: &mut System) -> (f64, f64, f64, f64) {
system.refresh_all();
let cpu_usage = system.global_cpu_info().cpu_usage() as f64;
let cpu_usage = system.global_cpu_usage() as f64;
let memory_used = system.used_memory() as f64;
let memory_total = system.total_memory() as f64;
let memory_usage = memory_used / memory_total * 100.0;

30
src/s3.rs

@ -1,30 +0,0 @@
use std::sync::Arc;
use aws_credential_types::Credentials;
use aws_sdk_kinesis::{Client, Config};
use rusoto_core::{Region, HttpClient};
use rusoto_core::credential::{StaticProvider, ProvideAwsCredentials};
use rusoto_s3::{S3Client, S3, GetObjectRequest};
use tokio::io::AsyncReadExt;
use tokio::sync::OnceCell;
static AWS_S3_CLIENT: OnceCell<Arc<S3Client>> = OnceCell::const_new();
async fn initialize_data() -> Arc<S3Client> {
let config = crate::config::get_config()
.await;
let access_key_id = &config.kinesis_stream.node.access_key.clone();
let secret_access_key = &config.kinesis_stream.node.secret_key.clone();
let region = &config.kinesis_stream.node.region.clone();
let credentials_provider = StaticProvider::new_minimal(access_key_id.to_string(), secret_access_key.to_string());
let s3_client = S3Client::new_with(HttpClient::new().expect("failed to create request dispatcher"), credentials_provider, region.parse().unwrap());
Arc::new(s3_client)
}
async fn get_client() -> Arc<S3Client> {
AWS_S3_CLIENT.get_or_init(initialize_data)
.await
.clone()
}
Loading…
Cancel
Save