Skip to content

Commit c21adb2

Browse files
fiadlieltisonkun
andauthored
feat: Support Google structured logging format (#118)
Co-authored-by: tison <[email protected]>
1 parent 3bc2be6 commit c21adb2

File tree

7 files changed

+480
-5
lines changed

7 files changed

+480
-5
lines changed

.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ jobs:
8484
cargo run --no-default-features --example simple_stdout
8585
cargo run --features="json" --example json_stdout
8686
cargo run --features="json,rolling-file" --example rolling_file
87+
cargo run --features="fastrace/enable,google_structured_log" --example google_structured_log
8788
cargo run --features="fastrace/enable" --example fastrace
8889
8990
required:

Cargo.lock

+143
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+12-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ default = []
3636

3737
colored = ["dep:colored"]
3838
fastrace = ["dep:fastrace"]
39+
google_structured_log = ["internal-serde", "dep:serde_json"]
3940
journald = ["dep:libc"]
40-
json = ["dep:serde_json", "dep:serde", "jiff/serde"]
41+
json = ["internal-serde", "dep:serde_json"]
4142
native-tls = ["dep:native-tls", "fasyslog?/native-tls"]
4243
non-blocking = ["dep:crossbeam-channel"]
4344
opentelemetry = [
@@ -48,6 +49,9 @@ opentelemetry = [
4849
rolling-file = ["non-blocking"]
4950
syslog = ["non-blocking", "dep:fasyslog"]
5051

52+
# Internal features - not intended for directly public use
53+
internal-serde = ["dep:serde", "log/kv_serde", "jiff/serde"]
54+
5155
[dependencies]
5256
anyhow = { version = "1.0" }
5357
env_filter = { version = "0.1.1" }
@@ -125,4 +129,10 @@ required-features = ["journald"]
125129
doc-scrape-examples = true
126130
name = "fastrace"
127131
path = "examples/fastrace.rs"
128-
required-features = ["fastrace", "fastrace/enable"]
132+
required-features = ["fastrace/enable"]
133+
134+
[[example]]
135+
doc-scrape-examples = true
136+
name = "google_structured_log"
137+
path = "examples/google_structured_log.rs"
138+
required-features = ["fastrace/enable", "google_structured_log"]

examples/google_structured_log.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2024 FastLabs Developers
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+
use fastrace::collector::Config;
16+
use fastrace::collector::ConsoleReporter;
17+
use fastrace::collector::SpanContext;
18+
use fastrace::prelude::SpanId;
19+
use fastrace::prelude::TraceId;
20+
use fastrace::Span;
21+
use logforth::append;
22+
use logforth::diagnostic;
23+
use logforth::layout::GoogleStructuredLogLayout;
24+
use serde::Serialize;
25+
26+
#[derive(Serialize)]
27+
struct NestedStructuredValue {
28+
c: String,
29+
d: bool,
30+
}
31+
32+
#[derive(Serialize)]
33+
struct MyStructuredValue {
34+
a: i32,
35+
b: NestedStructuredValue,
36+
}
37+
38+
fn main() {
39+
logforth::builder()
40+
.dispatch(|d| {
41+
d.diagnostic(diagnostic::FastraceDiagnostic::default())
42+
.append(
43+
append::Stdout::default().with_layout(
44+
GoogleStructuredLogLayout::default()
45+
.trace_project_id("project-id")
46+
.label_keys(["label1"]),
47+
),
48+
)
49+
})
50+
.apply();
51+
52+
fastrace::set_reporter(ConsoleReporter, Config::default());
53+
54+
{
55+
let root = Span::root("root", SpanContext::new(TraceId::random(), SpanId(0)));
56+
let _g = root.set_local_parent();
57+
58+
let structured_value = MyStructuredValue {
59+
a: 1,
60+
b: NestedStructuredValue {
61+
c: "Hello".into(),
62+
d: true,
63+
},
64+
};
65+
66+
log::info!(
67+
label1 = "this is a label value",
68+
notLabel:serde = structured_value;
69+
"Hello label value!",
70+
);
71+
}
72+
}

0 commit comments

Comments
 (0)