From 3c443ddfe21f0eadd8e24a9b9b1b77376bc7af67 Mon Sep 17 00:00:00 2001 From: Oliver Browne Date: Thu, 25 Apr 2024 00:06:29 +0200 Subject: [PATCH 1/2] Add new layer to add env vars to span attributes --- Cargo.toml | 2 +- src/layer/kube_env.rs | 52 +++++++++++++++++++++++++++++++++++++++++++ src/layer/mod.rs | 2 ++ src/subscriber.rs | 4 +++- 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/layer/kube_env.rs diff --git a/Cargo.toml b/Cargo.toml index 3a266ba..702d5ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ repository = "https://github.com/primait/prima_tracing.rs" version = "0.9.1" [features] -default = [] +default = ["live", "dev"] # Legacy feature names kept for compatibility reasons prima-logger-datadog = ["datadog"] prima-logger-json = ["json-logger"] diff --git a/src/layer/kube_env.rs b/src/layer/kube_env.rs new file mode 100644 index 0000000..97ef3cf --- /dev/null +++ b/src/layer/kube_env.rs @@ -0,0 +1,52 @@ +use opentelemetry::KeyValue; +use tracing::{span, Subscriber}; +use tracing_opentelemetry::OtelData; +use tracing_subscriber::{layer::Context, Layer}; + +// Extracts KUBE_APP_PART_OF, KUBE_APP_MANAGED_BY, KUBE_APP_VERSION and KUBE_APP_INSTANCE from the environment +// and adds them to span attributes +pub struct KubeEnvLayer { + pub kube_app_part_of: Option, + pub kube_app_managed_by: Option, + pub kube_app_version: Option, + pub kube_app_instance: Option, +} + +impl Default for KubeEnvLayer { + fn default() -> Self { + Self { + kube_app_part_of: std::env::var("KUBE_APP_PART_OF").ok(), + kube_app_managed_by: std::env::var("KUBE_APP_MANAGED_BY").ok(), + kube_app_version: std::env::var("KUBE_APP_VERSION").ok(), + kube_app_instance: std::env::var("KUBE_APP_INSTANCE").ok(), + } + } +} + +impl Layer for KubeEnvLayer +where + S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>, +{ + fn on_new_span(&self, _attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) { + let span = ctx.span(id).expect("Span not found, this is a bug"); + let mut extensions = span.extensions_mut(); + + if let Some(otel_data) = extensions.get_mut::() { + let builder = &mut otel_data.builder; + if let Some(ref mut attributes) = builder.attributes { + if let Some(ref part_of) = self.kube_app_part_of { + attributes.push(KeyValue::new("kube_app_part_of", part_of.clone())); + } + if let Some(ref managed_by) = self.kube_app_managed_by { + attributes.push(KeyValue::new("kube_app_managed_by", managed_by.clone())); + } + if let Some(ref version) = self.kube_app_version { + attributes.push(KeyValue::new("kube_app_version", version.clone())); + } + if let Some(ref instance) = self.kube_app_instance { + attributes.push(KeyValue::new("kube_app_instance", instance.clone())); + } + } + } + } +} diff --git a/src/layer/mod.rs b/src/layer/mod.rs index 8b42654..f43395f 100644 --- a/src/layer/mod.rs +++ b/src/layer/mod.rs @@ -1,5 +1,7 @@ pub use error::ErrorLayer; +pub use kube_env::KubeEnvLayer; pub use version::VersionLayer; mod error; +mod kube_env; mod version; diff --git a/src/subscriber.rs b/src/subscriber.rs index 95a1fcb..27bd414 100644 --- a/src/subscriber.rs +++ b/src/subscriber.rs @@ -31,6 +31,7 @@ pub fn configure_subscriber( version: _config.version.clone(), }) .with(crate::layer::ErrorLayer) + .with(crate::layer::KubeEnvLayer::default()) }; #[cfg(not(feature = "json-logger"))] @@ -40,7 +41,8 @@ pub fn configure_subscriber( use crate::json::formatter::PrimaFormattingLayer; use crate::json::storage::PrimaJsonStorage; subscriber - .with(PrimaJsonStorage) + .with(PrimaJsonStorage::default()) + .with(crate::layer::KubeEnvLayer::default()) .with(PrimaFormattingLayer::new( _config.service.clone(), _config.country.to_string(), From abfcd3c88ad3cc48ec7731e98668bef1d9890ebe Mon Sep 17 00:00:00 2001 From: Oliver Browne Date: Thu, 25 Apr 2024 00:25:25 +0200 Subject: [PATCH 2/2] lint --- Cargo.toml | 2 +- src/subscriber.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 702d5ca..3a266ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ repository = "https://github.com/primait/prima_tracing.rs" version = "0.9.1" [features] -default = ["live", "dev"] +default = [] # Legacy feature names kept for compatibility reasons prima-logger-datadog = ["datadog"] prima-logger-json = ["json-logger"] diff --git a/src/subscriber.rs b/src/subscriber.rs index 27bd414..867d685 100644 --- a/src/subscriber.rs +++ b/src/subscriber.rs @@ -41,7 +41,7 @@ pub fn configure_subscriber( use crate::json::formatter::PrimaFormattingLayer; use crate::json::storage::PrimaJsonStorage; subscriber - .with(PrimaJsonStorage::default()) + .with(PrimaJsonStorage) .with(crate::layer::KubeEnvLayer::default()) .with(PrimaFormattingLayer::new( _config.service.clone(),