setup
parent
a5f67cbeb7
commit
5a53c366e6
|
|
@ -3,8 +3,8 @@ use axum::{
|
||||||
response::IntoResponse,
|
response::IntoResponse,
|
||||||
http
|
http
|
||||||
};
|
};
|
||||||
use crate::structs
|
use crate::structs::v3::PrometheusMetrics;
|
||||||
use integr_structs::api::v3::PrometheusMetrics;
|
// use integr_structs::api::v3::PrometheusMetrics;
|
||||||
use prometheus::{Encoder, Registry, TextEncoder, Gauge};
|
use prometheus::{Encoder, Registry, TextEncoder, Gauge};
|
||||||
use std::sync::{Arc, MutexGuard};
|
use std::sync::{Arc, MutexGuard};
|
||||||
use crate::AppState;
|
use crate::AppState;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
// use serde_json::;
|
use crate::structs::v3::MetricOutput;
|
||||||
use integr_structs::api::v3::MetricOutput;
|
|
||||||
use serde_json::{Map, Value};
|
use serde_json::{Map, Value};
|
||||||
use prometheus::Gauge;
|
use prometheus::Gauge;
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
|
||||||
259
src/structs.rs
259
src/structs.rs
|
|
@ -1,237 +1,11 @@
|
||||||
use core::sync;
|
// use std::collections::HashMap;
|
||||||
use std::collections::HashMap;
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use serde_json::{ to_string_pretty, Value };
|
use serde_json::Value;
|
||||||
use anyhow::Result;
|
// use anyhow::Result;
|
||||||
use std::sync::Arc;
|
// use std::sync::Arc;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct ApiConfig {
|
|
||||||
#[serde(default)]
|
|
||||||
pub endpoints : Vec<ApiEndpoint>,
|
|
||||||
pub delay : u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct ApiEndpoint {
|
|
||||||
pub url : String,
|
|
||||||
pub method : String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for ApiConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
ApiConfig {
|
|
||||||
endpoints : vec![],
|
|
||||||
delay : 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// v2
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
|
||||||
pub struct ApiConfigV2 {
|
|
||||||
pub id : u64,
|
|
||||||
#[serde(default)]
|
|
||||||
pub template : Vec<Template>,
|
|
||||||
pub ip_address : String,
|
|
||||||
pub login : Option<String>,
|
|
||||||
pub pass : Option<String>,
|
|
||||||
pub api_key : Option<String>,
|
|
||||||
pub period : u32, // if "0" -> inf
|
|
||||||
pub timeout : u32, // if "0" -> no-delay
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for ApiConfigV2 {
|
|
||||||
fn default() -> Self {
|
|
||||||
ApiConfigV2 {
|
|
||||||
id : 0,
|
|
||||||
template : Vec::new(),
|
|
||||||
ip_address : String::from("no_ip"),
|
|
||||||
login : None,
|
|
||||||
pass : None,
|
|
||||||
api_key : None,
|
|
||||||
period : 0,
|
|
||||||
timeout : 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl ApiConfigV2 {
|
|
||||||
pub fn template() -> Self {
|
|
||||||
ApiConfigV2 {
|
|
||||||
id : 1111,
|
|
||||||
template : Vec::new(),
|
|
||||||
ip_address : String::from("ip"),
|
|
||||||
login : None,
|
|
||||||
pass : None,
|
|
||||||
api_key : None,
|
|
||||||
period : 1111,
|
|
||||||
timeout : 1111,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn pattern() -> Self {
|
|
||||||
ApiConfigV2 {
|
|
||||||
id : 1111,
|
|
||||||
template : vec![
|
|
||||||
Template {
|
|
||||||
id : String::from("no id"),
|
|
||||||
name : String::from("open api"),
|
|
||||||
url : String::from("https://dummy-json.mock.beeceptor.com/countries"),
|
|
||||||
method : String::from("GET"),
|
|
||||||
measure : Vec::new(),
|
|
||||||
}
|
|
||||||
],
|
|
||||||
ip_address : String::from("ip"),
|
|
||||||
login : None,
|
|
||||||
pass : None,
|
|
||||||
api_key : None,
|
|
||||||
period : 1,
|
|
||||||
timeout : 1,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
|
||||||
pub struct Template {
|
|
||||||
pub id : String,
|
|
||||||
pub name : String,
|
|
||||||
pub url : String,
|
|
||||||
pub method : String,
|
|
||||||
#[serde(default)]
|
|
||||||
pub measure : Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Template {
|
|
||||||
fn default() -> Self {
|
|
||||||
Template {
|
|
||||||
id : String::from("no-id"),
|
|
||||||
name : String::from("no-name"),
|
|
||||||
url : String::from("no-url"),
|
|
||||||
method : String::from("post"),
|
|
||||||
measure : Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct ProcessedEndpoint {
|
|
||||||
id : String,
|
|
||||||
name : String,
|
|
||||||
url : String,
|
|
||||||
method : String,
|
|
||||||
#[serde(default)]
|
|
||||||
metrics : HashMap<String, Value>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ProcessedEndpoint {
|
|
||||||
pub fn new(id: &str, name: &str, url: &str, method: &str, metrics: HashMap<String, Value>) -> Self {
|
|
||||||
ProcessedEndpoint {
|
|
||||||
id : id.to_owned(),
|
|
||||||
name : name.to_owned(),
|
|
||||||
url : url.to_owned(),
|
|
||||||
method : method.to_owned(),
|
|
||||||
metrics : metrics,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn from_target_response(response: &str, keys: &Template) -> Result<String> {
|
|
||||||
let mut hm: HashMap<String, Value> = HashMap::new();
|
|
||||||
let mut response: Value = serde_json::from_str(response)?;
|
|
||||||
|
|
||||||
let _ = keys.measure.iter()
|
|
||||||
.map(|key| (key, response[key].take()))
|
|
||||||
.for_each(|(key, value)| {
|
|
||||||
hm.insert(key.clone(), value);
|
|
||||||
});
|
|
||||||
let val = ProcessedEndpoint::new(&keys.id, &keys.name, &keys.url, &keys.method,hm);
|
|
||||||
Ok(to_string_pretty(&val)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod v3 {
|
pub mod v3 {
|
||||||
pub use super::*;
|
pub use super::*;
|
||||||
|
|
||||||
// in config
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
||||||
pub struct Metric {
|
|
||||||
pub id : String,
|
|
||||||
#[serde(rename = "type")]
|
|
||||||
pub json_type : String,
|
|
||||||
pub addr : String,
|
|
||||||
}
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
||||||
pub struct Metrics {
|
|
||||||
pub name : String,
|
|
||||||
pub url : String,
|
|
||||||
#[serde(default)]
|
|
||||||
pub measure : Vec<Metric>
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
||||||
pub struct ConfigEndpoint {
|
|
||||||
pub id : String,
|
|
||||||
pub login : String,
|
|
||||||
#[serde(rename = "pass")]
|
|
||||||
pub password : String,
|
|
||||||
pub api_key : String,
|
|
||||||
period : String,
|
|
||||||
timeout : String,
|
|
||||||
#[serde(default)]
|
|
||||||
pub metrics : Vec<Metrics>,
|
|
||||||
}
|
|
||||||
impl ConfigEndpoint {
|
|
||||||
pub fn from_config(config: Arc<Config>) -> Vec<Arc<Self>> {
|
|
||||||
let mut result: Vec<Arc<ConfigEndpoint>> = Vec::new();
|
|
||||||
config.config
|
|
||||||
.iter()
|
|
||||||
.for_each(|el| {
|
|
||||||
result.push(Arc::new(el.clone()))
|
|
||||||
});
|
|
||||||
result
|
|
||||||
}
|
|
||||||
pub fn get_period(&self) -> Option<u32> {
|
|
||||||
self.period.parse().ok()
|
|
||||||
}
|
|
||||||
pub fn get_timeout(&self) -> Option<u64> {
|
|
||||||
self.timeout.parse().ok()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
||||||
pub struct Config {
|
|
||||||
pub config : Vec<ConfigEndpoint>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Config {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
config : Vec::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Config {
|
|
||||||
pub async fn is_default(&self) -> bool {
|
|
||||||
self.config.is_empty()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Credentials {
|
|
||||||
pub endpoint : Arc<ConfigEndpoint>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Credentials {
|
|
||||||
pub fn from_config_endpoint(endpoint: Arc<ConfigEndpoint>) -> Credentials {
|
|
||||||
Self { endpoint }
|
|
||||||
}
|
|
||||||
// pub fn clone(self) -> Self {
|
|
||||||
// Self {
|
|
||||||
// endpoint : self.endpoint.clone()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
// to prometheus and nmns
|
// to prometheus and nmns
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct MetricOutput {
|
pub struct MetricOutput {
|
||||||
|
|
@ -241,16 +15,6 @@ pub mod v3 {
|
||||||
addr : String,
|
addr : String,
|
||||||
pub value : Value,
|
pub value : Value,
|
||||||
}
|
}
|
||||||
impl MetricOutput {
|
|
||||||
pub fn new_with_slices(id : &str, json_type : &str, addr: &str,value : Value) -> Self {
|
|
||||||
MetricOutput {
|
|
||||||
id : id.to_string(),
|
|
||||||
json_type : json_type.to_string(),
|
|
||||||
addr : addr.to_string(),
|
|
||||||
value : value,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct PrometheusMetrics {
|
pub struct PrometheusMetrics {
|
||||||
|
|
@ -258,19 +22,4 @@ pub mod v3 {
|
||||||
pub endpoint_name: String,
|
pub endpoint_name: String,
|
||||||
pub metrics: Vec<MetricOutput>,
|
pub metrics: Vec<MetricOutput>,
|
||||||
}
|
}
|
||||||
impl PrometheusMetrics {
|
|
||||||
pub fn new(service: &str, endpoint: &str, metrics: Vec<MetricOutput>) -> Self {
|
|
||||||
Self {
|
|
||||||
service_name: service.to_string(),
|
|
||||||
endpoint_name: endpoint.to_string(),
|
|
||||||
metrics: metrics
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn get_bytes_len(&self) -> usize {
|
|
||||||
let str_metrics = serde_json::to_vec(self).unwrap_or_else(
|
|
||||||
|_| Vec::new()
|
|
||||||
);
|
|
||||||
str_metrics.len()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue