diff options
| author | MatthieuCoder <matthieu@matthieu-dev.xyz> | 2023-01-15 17:03:29 +0400 |
|---|---|---|
| committer | MatthieuCoder <matthieu@matthieu-dev.xyz> | 2023-01-15 17:03:29 +0400 |
| commit | cbcaa3c01ec4d9ed95dc5af8232de1d10191bc44 (patch) | |
| tree | 02cd56bb6eba762024c33f2df3cbec4f7a1a27e1 /libs/shared | |
| parent | c06cf52e887cc560f1fd4fa72713b239fe0102a6 (diff) | |
add metrics and jemalloc
Diffstat (limited to 'libs/shared')
| -rw-r--r-- | libs/shared/Cargo.toml | 6 | ||||
| -rw-r--r-- | libs/shared/src/config.rs | 1 | ||||
| -rw-r--r-- | libs/shared/src/lib.rs | 1 | ||||
| -rw-r--r-- | libs/shared/src/opentelemetry.rs | 91 |
4 files changed, 97 insertions, 2 deletions
diff --git a/libs/shared/Cargo.toml b/libs/shared/Cargo.toml index 419ad0c..9b29bf2 100644 --- a/libs/shared/Cargo.toml +++ b/libs/shared/Cargo.toml @@ -10,7 +10,7 @@ serde_repr = "0.1" config = { version = "0.13", default-features = false, features = ["json", "yaml-rust", "ini"] } -async-nats = "0.25.1" +async-nats = "0.26.0" redis = { version = "0.22.1", features = ["cluster", "connection-manager", "tokio-comp"] } tokio = { version = "1", features = ["signal", "rt"] } @@ -21,4 +21,6 @@ anyhow = "1.0.68" serde_test = "1.0.152" -tracing = "0.1.37"
\ No newline at end of file +tracing = "0.1.37" +opentelemetry-otlp = "0.11.0" +opentelemetry = "0.18.0"
\ No newline at end of file diff --git a/libs/shared/src/config.rs b/libs/shared/src/config.rs index cdf0bd3..4967e3c 100644 --- a/libs/shared/src/config.rs +++ b/libs/shared/src/config.rs @@ -10,6 +10,7 @@ pub struct Settings<T: Clone + DeserializeOwned> { pub config: T, pub nats: crate::nats::Configuration, pub redis: crate::redis::Configuration, + pub opentelemetry: Option<crate::opentelemetry::Configuration>, } impl<T: Clone + DeserializeOwned + Default> Settings<T> { diff --git a/libs/shared/src/lib.rs b/libs/shared/src/lib.rs index 92e7a05..33a6a13 100644 --- a/libs/shared/src/lib.rs +++ b/libs/shared/src/lib.rs @@ -13,3 +13,4 @@ pub mod config; pub mod nats; pub mod payloads; pub mod redis; +pub mod opentelemetry;
\ No newline at end of file diff --git a/libs/shared/src/opentelemetry.rs b/libs/shared/src/opentelemetry.rs new file mode 100644 index 0000000..ca6542d --- /dev/null +++ b/libs/shared/src/opentelemetry.rs @@ -0,0 +1,91 @@ +use std::ops::{Deref, DerefMut}; + +use opentelemetry_otlp::{ExportConfig, Protocol}; +use serde::{de::Visitor, Deserialize}; + +#[derive(Debug, Default)] +#[repr(transparent)] +pub struct ExportConfigDeserialize(ExportConfig); +impl Clone for ExportConfigDeserialize { + fn clone(&self) -> Self { + Self(ExportConfig { + endpoint: self.0.endpoint.clone(), + protocol: self.0.protocol, + timeout: self.0.timeout, + }) + } +} + +impl From<ExportConfigDeserialize> for ExportConfig { + fn from(val: ExportConfigDeserialize) -> Self { + val.0 + } +} + +impl Deref for ExportConfigDeserialize { + type Target = ExportConfig; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for ExportConfigDeserialize { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl<'de> Deserialize<'de> for ExportConfigDeserialize { + fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> + where + D: serde::Deserializer<'de>, + { + #[derive(Deserialize, Debug)] + #[serde(field_identifier, rename_all = "lowercase")] + enum Fields { + Endpoint, + Timeout, + } + + struct OpenTelemetryExportConfigVisitor; + impl<'de> Visitor<'de> for OpenTelemetryExportConfigVisitor { + type Value = ExportConfigDeserialize; + fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + formatter.write_str("struct OpenTelemetryExportConfig") + } + + fn visit_map<A>(self, mut map: A) -> std::result::Result<Self::Value, A::Error> + where + A: serde::de::MapAccess<'de>, + { + let mut export_config = ExportConfigDeserialize::default(); + export_config.0.protocol = Protocol::Grpc; + while let Some(name) = map.next_key::<Fields>()? { + match name { + Fields::Endpoint => { + export_config.0.endpoint = map.next_value()?; + } + Fields::Timeout => { + export_config.0.timeout = map.next_value()?; + } + } + } + + Ok(export_config) + } + } + + deserializer.deserialize_struct( + "OpenTelemetryExportConfig", + &["endpoint", "protocol", "timeout"], + OpenTelemetryExportConfigVisitor, + ) + } +} + +#[derive(Debug, Clone, Deserialize)] +pub struct Configuration { + pub traces: Option<ExportConfigDeserialize>, + pub metrics: Option<ExportConfigDeserialize>, +} |
