Skip to content

Commit c2c914c

Browse files
committed
feat(tracing): self-register module log tags
1 parent b4eace8 commit c2c914c

File tree

41 files changed

+198
-35
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+198
-35
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ cargo_metadata = "0.19"
549549
fast-float2 = "0.2.3"
550550
gix = "0.71.0"
551551
indent = "0.1.1"
552+
inventory = "0.3.15"
552553
logos = "0.12.1"
553554
nom = "7.1.1"
554555
nom-rule = "0.4"

src/common/tracing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ defer = { workspace = true }
2020
fastrace = { workspace = true }
2121
fastrace-opentelemetry = { workspace = true }
2222
itertools = { workspace = true }
23+
inventory = { workspace = true }
2324
jiff = { workspace = true }
2425
libc = { workspace = true }
2526
log = { workspace = true }

src/common/tracing/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod crash_hook;
2222
mod filter;
2323
mod init;
2424
mod loggers;
25+
pub mod module_tag;
2526
mod panic_hook;
2627
mod remote_log;
2728
mod structlog;

src/common/tracing/src/loggers.rs

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ use logforth::Layout;
3636
use serde_json::Map;
3737
use serde_json::Value as JsonValue;
3838

39+
use crate::module_tag::label_for_module;
40+
3941
const PRINTER: DateTimePrinter = DateTimePrinter::new().precision(Some(6));
4042

4143
pub fn format_timestamp(zdt: &Zoned) -> String {
@@ -124,53 +126,20 @@ impl<const FIXED_TIME: bool> Layout for TextLayout<FIXED_TIME> {
124126
}
125127

126128
let level = record.level();
127-
let module = map_module(record.module_path().unwrap_or(""));
129+
let module = record.module_path().map(label_for_module).unwrap_or("");
128130
let log_file = Path::new(record.file().unwrap_or_default())
129131
.file_name()
130132
.and_then(|name| name.to_str())
131133
.unwrap_or_default();
132134
let line = record.line().unwrap_or(0);
133135
let msg = record.args();
134-
write!(buf, " {level:>5} {module}: {log_file}:{line} {msg}",)?;
136+
write!(buf, " {level:>5} {module}: {log_file}:{line} {msg}")?;
135137
record.key_values().visit(&mut KvWriter(&mut buf))?;
136138

137139
Ok(buf)
138140
}
139141
}
140142

141-
fn map_module(full_name: &str) -> &str {
142-
if full_name.starts_with("databend_query") {
143-
if full_name.starts_with("databend_query::sessions::query_ctx") {
144-
return "[QUERY-CTX]";
145-
}
146-
if full_name.starts_with("databend_query::sessions::queue_mgr") {
147-
return "[QUERY-QUEUE]";
148-
}
149-
if full_name.starts_with("databend_query::interpreters::interpreter") {
150-
return "[INTERPRETER]";
151-
}
152-
if full_name.starts_with("databend_query::servers::http::v1::query") {
153-
return "[HTTP-QUERY]";
154-
}
155-
if full_name.starts_with("databend_query::pipelines::builders") {
156-
return "[BUILD-PIPELINES]";
157-
}
158-
if full_name.starts_with("databend_query::pipelines::executor::query_pipeline_executor") {
159-
return "[PIPELINE-EXECUTOR]";
160-
}
161-
if full_name.starts_with("databend_query::pipelines::processors::transforms") {
162-
return "[TRANSFORMS]";
163-
}
164-
}
165-
if full_name.starts_with("databend_common_pipeline_transforms") {
166-
return "[TRANSFORMS]";
167-
}
168-
if full_name.starts_with("databend_common_sql::planner::planner") {
169-
return "[SQL-PLANNER]";
170-
}
171-
full_name
172-
}
173-
174143
#[derive(Debug)]
175144
pub struct JsonLayout<const FIXED_TIME: bool = false>;
176145

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2021 Datafuse Labs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/// A mapping between a module path prefix and a short log label.
16+
#[derive(Debug)]
17+
pub struct ModuleTag {
18+
pub prefix: &'static str,
19+
pub label: &'static str,
20+
}
21+
22+
inventory::collect!(ModuleTag);
23+
24+
/// Resolve the best matching label for the provided module path.
25+
pub fn label_for_module<'a>(full_name: &'a str) -> &'a str {
26+
inventory::iter::<ModuleTag>
27+
.into_iter()
28+
.filter_map(|entry| {
29+
full_name
30+
.starts_with(entry.prefix)
31+
.then_some((entry.prefix.len(), entry.label))
32+
})
33+
.max_by_key(|(len, _)| *len)
34+
.map(|(_, label)| label)
35+
.unwrap_or(full_name)
36+
}
37+
38+
#[macro_export]
39+
macro_rules! register_module_tag {
40+
($label:expr $(,)?) => {
41+
inventory::submit! {
42+
#![crate = databend_common_tracing]
43+
44+
databend_common_tracing::module_tag::ModuleTag {
45+
prefix: module_path!(),
46+
label: $label,
47+
}
48+
}
49+
};
50+
($label:expr, $prefix:expr $(,)?) => {
51+
inventory::submit! {
52+
#![crate = databend_common_tracing]
53+
54+
databend_common_tracing::module_tag::ModuleTag {
55+
prefix: $prefix,
56+
label: $label,
57+
}
58+
}
59+
};
60+
}
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use super::*;
65+
66+
inventory::submit! {
67+
ModuleTag {
68+
prefix: "module::submodule",
69+
label: "[SUB]",
70+
}
71+
}
72+
73+
inventory::submit! {
74+
ModuleTag {
75+
prefix: "module",
76+
label: "[MODULE]",
77+
}
78+
}
79+
80+
#[test]
81+
fn chooses_longest_prefix() {
82+
assert_eq!(label_for_module("module::submodule::leaf"), "[SUB]");
83+
}
84+
85+
#[test]
86+
fn falls_back_to_full_module_name() {
87+
assert_eq!(label_for_module("unknown::module"), "unknown::module");
88+
}
89+
}

src/query/ee/src/storages/fuse/operations/vacuum_table_v2.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Logs from this module will show up as "[FUSE-VACUUM2] ...".
16+
databend_common_tracing::register_module_tag!("[FUSE-VACUUM2]");
17+
1518
use std::collections::HashSet;
1619
use std::sync::Arc;
1720

src/query/pipeline/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ async-trait = { workspace = true }
1212
databend-common-base = { workspace = true }
1313
databend-common-exception = { workspace = true }
1414
databend-common-expression = { workspace = true }
15+
databend-common-tracing = { workspace = true }
1516
fastrace = { workspace = true }
1617
futures = { workspace = true }
1718
log = { workspace = true }

src/query/pipeline/core/src/processors/processor.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Logs from this module will show up as "[PIPELINE-EXECUTOR] ...".
16+
databend_common_tracing::register_module_tag!("[PIPELINE-EXECUTOR]");
17+
1518
use std::any::Any;
1619
use std::cell::UnsafeCell;
1720
use std::ops::Deref;

src/query/pipeline/transforms/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ databend-common-functions = { workspace = true }
2121
databend-common-license = { workspace = true }
2222
databend-common-pipeline-core = { workspace = true }
2323
databend-common-sql = { workspace = true }
24+
databend-common-tracing = { workspace = true }
2425
databend-storages-common-cache = { workspace = true }
2526
enum-as-inner = { workspace = true }
2627
fastrace = { workspace = true }

0 commit comments

Comments
 (0)