Skip to content

Commit

Permalink
fix: missing service label for otlp logs (#15639)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored May 25, 2024
1 parent c8e3c20 commit 28c543e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/common/tracing/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,19 @@ pub fn inject_span_to_tonic_request<T>(msg: impl tonic::IntoRequest<T>) -> tonic

#[allow(dyn_drop)]
pub fn init_logging(
name: &str,
log_name: &str,
cfg: &Config,
labels: BTreeMap<String, String>,
mut labels: BTreeMap<String, String>,
) -> Vec<Box<dyn Drop + Send + Sync + 'static>> {
let mut guards: Vec<Box<dyn Drop + Send + Sync + 'static>> = 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 {
Expand All @@ -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()
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -233,7 +235,7 @@ pub fn init_logging(
query_logger = query_logger.chain(Box::new(query_log_file) as Box<dyn Write + Send>);
}
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<dyn Log>);
}
}
Expand All @@ -248,7 +250,7 @@ pub fn init_logging(
profile_logger.chain(Box::new(profile_log_file) as Box<dyn Write + Send>);
}
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<dyn Log>);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/common/tracing/src/loggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,6 +99,7 @@ impl OpenTelemetryLogger {
name: impl ToString,
category: impl ToString,
config: &OTLPEndpointConfig,
labels: &BTreeMap<String, String>,
) -> Self {
let exporter = match config.protocol {
OTLPProtocol::Grpc => {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 28c543e

Please sign in to comment.