diff options
| author | MatthieuCoder <matthieu@matthieu-dev.xyz> | 2023-01-08 21:56:15 +0400 |
|---|---|---|
| committer | MatthieuCoder <matthieu@matthieu-dev.xyz> | 2023-01-08 21:56:15 +0400 |
| commit | c925e01d4a96d88d9cbbe4f3d55aceaf5bff017f (patch) | |
| tree | c3129cc8f71c342385b085dbcc48a2bf7914d2c1 /libs/shared/src | |
| parent | 19dbd364caf8f3d701a17a06846716dcf6765e58 (diff) | |
| parent | 038add4d5e8465f8bb36f1a1fa5817a02cab833b (diff) | |
merge conflicts
Diffstat (limited to 'libs/shared/src')
| -rw-r--r-- | libs/shared/src/config.rs | 18 | ||||
| -rw-r--r-- | libs/shared/src/error.rs | 2 | ||||
| -rw-r--r-- | libs/shared/src/lib.rs | 9 | ||||
| -rw-r--r-- | libs/shared/src/monitoring.rs | 61 | ||||
| -rw-r--r-- | libs/shared/src/payloads.rs | 30 |
5 files changed, 22 insertions, 98 deletions
diff --git a/libs/shared/src/config.rs b/libs/shared/src/config.rs index ab584a2..73bc449 100644 --- a/libs/shared/src/config.rs +++ b/libs/shared/src/config.rs @@ -1,23 +1,21 @@ -use std::{env, ops::Deref}; use config::{Config, Environment, File}; -use log::info; -use serde::{Deserialize, de::DeserializeOwned}; +use serde::{de::DeserializeOwned, Deserialize}; +use std::{env, ops::Deref}; +use tracing::info; use crate::error::GenericError; #[derive(Debug, Deserialize, Clone)] pub struct Settings<T: Clone + DeserializeOwned + Default> { #[serde(skip_deserializing)] pub config: T, - pub monitoring: crate::monitoring::MonitoringConfiguration, pub nats: crate::nats::NatsConfiguration, pub redis: crate::redis::RedisConfiguration, } -impl<T: Clone + DeserializeOwned + Default> Settings<T> -{ +impl<T: Clone + DeserializeOwned + Default> Settings<T> { pub fn new(service_name: &str) -> Result<Settings<T>, GenericError> { let mut builder = Config::builder(); - + builder = builder.add_source(File::with_name("config/default")); let mode = env::var("ENV").unwrap_or_else(|_| "development".into()); info!("Configuration Environment: {}", mode); @@ -28,7 +26,7 @@ impl<T: Clone + DeserializeOwned + Default> Settings<T> let env = Environment::with_prefix("NOVA").separator("__"); // we can configure each component using environment variables builder = builder.add_source(env); - + let config = builder.build()?; let mut settings: Settings<T> = config.clone().try_deserialize()?; @@ -39,10 +37,10 @@ impl<T: Clone + DeserializeOwned + Default> Settings<T> } } -impl<T: Clone + DeserializeOwned + Default> Deref for Settings<T> { +impl<T: Clone + DeserializeOwned + Default> Deref for Settings<T> { type Target = T; fn deref(&self) -> &Self::Target { &self.config } -}
\ No newline at end of file +} diff --git a/libs/shared/src/error.rs b/libs/shared/src/error.rs index 31b1dcd..990dd1c 100644 --- a/libs/shared/src/error.rs +++ b/libs/shared/src/error.rs @@ -14,5 +14,5 @@ pub enum GenericError { StepFailed(String), #[error("io error")] - Io(#[from] io::Error) + Io(#[from] io::Error), } diff --git a/libs/shared/src/lib.rs b/libs/shared/src/lib.rs index 62d8689..68ff335 100644 --- a/libs/shared/src/lib.rs +++ b/libs/shared/src/lib.rs @@ -1,16 +1,7 @@ -pub use ::config as config_crate; -pub use ::async_nats as nats_crate; -pub use ::redis as redis_crate; -pub use log; -pub use prometheus; -pub use serde; -pub use testcontainers; - /// This crate is all the utilities shared by the nova rust projects /// It includes logging, config and protocols. pub mod config; pub mod error; -pub mod monitoring; pub mod nats; pub mod payloads; pub mod redis; diff --git a/libs/shared/src/monitoring.rs b/libs/shared/src/monitoring.rs deleted file mode 100644 index 4bff043..0000000 --- a/libs/shared/src/monitoring.rs +++ /dev/null @@ -1,61 +0,0 @@ -use hyper::{ - header::CONTENT_TYPE, - service::{make_service_fn, service_fn}, - Body, Request, Response, Server, -}; -use log::{error, info}; -use prometheus::{Encoder, TextEncoder}; -use serde::Deserialize; -use std::net::ToSocketAddrs; - -#[derive(Clone, Debug, Deserialize)] -/// Options for the monitoring service -pub struct MonitoringConfiguration { - pub enabled: bool, - pub address: Option<String>, - pub port: Option<i32>, -} - -/// Handler for the hyper http server -async fn serve_metrics(_request: Request<Body>) -> Result<Response<Body>, hyper::Error> { - let encoder = TextEncoder::new(); - let metrics = prometheus::gather(); - - let mut buffer = vec![]; - encoder.encode(&metrics, &mut buffer).unwrap(); - - let response = Response::builder() - .status(200) - .header(CONTENT_TYPE, encoder.format_type()) - .body(Body::from(buffer)) - .unwrap(); - Ok(response) -} - -/// Starts a monitoring server on the requested port -pub fn start_monitoring(configuration: &MonitoringConfiguration) { - let config = configuration.clone(); - tokio::task::spawn(async move { - if config.enabled { - let address = format!( - "{}:{}", - config - .address - .expect("a listening address must be specified for the metrics server"), - config - .port - .expect("a listening port must be specified for the metrics server") - ); - info!("Starting monitoring server on {}", address); - - let listen_address = address.to_socket_addrs().unwrap().next().unwrap(); - let server = Server::bind(&listen_address).serve(make_service_fn(|_| async { - Ok::<_, hyper::Error>(service_fn(serve_metrics)) - })); - - if let Err(e) = server.await { - error!("failed to start the monitoring server {}", e); - } - } - }); -} diff --git a/libs/shared/src/payloads.rs b/libs/shared/src/payloads.rs index 7b56dfb..6e51b2d 100644 --- a/libs/shared/src/payloads.rs +++ b/libs/shared/src/payloads.rs @@ -1,9 +1,10 @@ use std::fmt::Debug; -use crate::serde::Deserializer; use serde::de::DeserializeSeed; +use serde::Deserializer; use serde::{Deserialize, Serialize}; use serde_json::Value; +use tracing::trace_span; use twilight_model::gateway::event::{DispatchEvent, DispatchEventWithTypeDeserializer}; #[derive(Debug, Clone)] @@ -20,15 +21,19 @@ struct DispatchEventTaggedSerialized { pub kind: String, } +// todo(MatthieuCoder): Remove the use of the Value impl<'de> Deserialize<'de> for DispatchEventTagged { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>, { + let _s = trace_span!("deserializing DispatchEventTagged"); let tagged = DispatchEventTaggedSerialized::deserialize(deserializer)?; let deserializer_seed = DispatchEventWithTypeDeserializer::new(&tagged.kind); let dispatch_event = deserializer_seed.deserialize(tagged.data).unwrap(); - Ok(DispatchEventTagged { data: dispatch_event }) + Ok(DispatchEventTagged { + data: dispatch_event, + }) } } @@ -37,28 +42,19 @@ impl Serialize for DispatchEventTagged { where S: serde::Serializer, { - let kind = self.data.kind().name().unwrap().to_string(); - - let s = DispatchEventTaggedSerialized { - kind, + let _s = trace_span!("serializing DispatchEventTagged"); + let kind = self.data.kind().name().unwrap(); + DispatchEventTaggedSerialized { data: serde_json::to_value(&self.data).unwrap(), - }; - - s.serialize(serializer) + kind: kind.to_string(), + } + .serialize(serializer) } } /// Payload send to the nova cache queues #[derive(Serialize, Deserialize, Debug, Clone)] -// #[serde(bound(deserialize = "T: Deserialize<'de> + std::default::Default + Clone"))] pub struct CachePayload { - pub tracing: Tracing, #[serde(flatten)] pub data: DispatchEventTagged, } - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Tracing { - pub node_id: String, - pub span: Option<String>, -} |
