diff --git a/src/common/tracing/src/init.rs b/src/common/tracing/src/init.rs index 0ba555abe414..6299799e2318 100644 --- a/src/common/tracing/src/init.rs +++ b/src/common/tracing/src/init.rs @@ -72,17 +72,19 @@ pub fn inject_span_to_tonic_request(msg: impl tonic::IntoRequest) -> tonic #[allow(dyn_drop)] pub fn init_logging( - name: &str, + log_name: &str, cfg: &Config, - labels: BTreeMap, + mut labels: BTreeMap, ) -> Vec> { let mut guards: Vec> = Vec::new(); - let log_name = name; + if !labels.contains_key("service") { + labels.insert("service".to_string(), log_name.to_string()); + } let trace_name = match labels.get("node_id") { - None => name.to_string(), + None => log_name.to_string(), Some(node_id) => format!( "{}@{}", - name, + log_name, if node_id.len() >= 7 { &node_id[0..7] } else { @@ -105,8 +107,8 @@ pub fn init_logging( "service.name", trace_name.clone(), )); - for (k, v) in labels { - kvs.push(opentelemetry::KeyValue::new(k, v)); + for (k, v) in &labels { + kvs.push(opentelemetry::KeyValue::new(k.to_string(), v.to_string())); } let exporter = match cfg.tracing.otlp.protocol { OTLPProtocol::Grpc => opentelemetry_otlp::new_exporter() @@ -201,7 +203,7 @@ pub fn init_logging( // OpenTelemetry logger if cfg.otlp.on { - let logger = OpenTelemetryLogger::new(log_name, "system", &cfg.otlp.endpoint); + let logger = OpenTelemetryLogger::new(log_name, "system", &cfg.otlp.endpoint, &labels); let dispatch = fern::Dispatch::new() .level(cfg.otlp.level.parse().unwrap_or(LevelFilter::Info)) .format(formatter("json")) @@ -233,7 +235,7 @@ pub fn init_logging( query_logger = query_logger.chain(Box::new(query_log_file) as Box); } if let Some(endpoint) = &cfg.query.otlp { - let logger = OpenTelemetryLogger::new(log_name, "query", endpoint); + let logger = OpenTelemetryLogger::new(log_name, "query", endpoint, &labels); query_logger = query_logger.chain(Box::new(logger) as Box); } } @@ -248,7 +250,7 @@ pub fn init_logging( profile_logger.chain(Box::new(profile_log_file) as Box); } if let Some(endpoint) = &cfg.profile.otlp { - let logger = OpenTelemetryLogger::new(log_name, "profile", endpoint); + let logger = OpenTelemetryLogger::new(log_name, "profile", endpoint, &labels); profile_logger = profile_logger.chain(Box::new(logger) as Box); } } diff --git a/src/common/tracing/src/loggers.rs b/src/common/tracing/src/loggers.rs index a09b59d9437c..4f4bbd2a4c18 100644 --- a/src/common/tracing/src/loggers.rs +++ b/src/common/tracing/src/loggers.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::collections::BTreeMap; use std::fmt; use std::io::BufWriter; use std::path::Path; @@ -98,6 +99,7 @@ impl OpenTelemetryLogger { name: impl ToString, category: impl ToString, config: &OTLPEndpointConfig, + labels: &BTreeMap, ) -> Self { let exporter = match config.protocol { OTLPProtocol::Grpc => { @@ -144,6 +146,9 @@ impl OpenTelemetryLogger { "category", category.to_string(), )); + for (k, v) in labels { + kvs.push(opentelemetry::KeyValue::new(k.to_string(), v.to_string())); + } let provider = opentelemetry_sdk::logs::LoggerProvider::builder() .with_batch_exporter(exporter, opentelemetry_sdk::runtime::Tokio) .with_config(