1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use crate::error::Result;
use opentelemetry::trace::{SpanId, TraceContextExt};
use serde::{ser::SerializeMap, Serialize, Serializer};
use serde_json::Value;
use std::{collections::BTreeMap, fmt, num::NonZeroU8};
use time::format_description::well_known::iso8601::{Config, Iso8601, TimePrecision};
use tracing::{field::Field, Event, Subscriber};
use tracing_opentelemetry::OtelData;
use tracing_subscriber::registry::{Extensions, SpanRef};
use tracing_subscriber::{
fmt::{format, FmtContext, FormatEvent, FormatFields},
registry::LookupSpan,
Layer,
};
#[cfg(tracing_unstable)]
use crate::tracing_fields::TRACING_FIELDS_STRUCTURE_NAME;
#[cfg(tracing_unstable)]
use valuable_serde::Serializable;
pub(crate) const VERSION: &str = "version";
pub(crate) const SERVICE: &str = "service";
pub(crate) const COMPONENT: &str = "component";
pub(crate) const TARGET: &str = "target";
pub(crate) const MSG: &str = "msg";
pub(crate) const MESSAGE: &str = "message";
pub(crate) const LOG_LEVEL: &str = "LogLevel";
pub(crate) const TIME: &str = "time";
pub(crate) const TIMESTAMP: &str = "timestamp";
pub(crate) const TRACE_ID: &str = "traceId";
pub(crate) const SPAN_ID: &str = "spanId";
const DEFAULT_FIELDS: [&str; 11] = [
VERSION, SERVICE, COMPONENT, TARGET, MSG, LOG_LEVEL, TIME, TIMESTAMP, MESSAGE, TRACE_ID,
SPAN_ID,
];
pub fn fregate_layer<S>(formatter: EventFormatter) -> impl Layer<S>
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
tracing_subscriber::fmt::layer().event_format(formatter)
}
#[derive(Debug, Default)]
pub struct EventFormatter {
default_fields: BTreeMap<String, Value>,
}
impl EventFormatter {
pub fn new() -> Self {
Self::default()
}
pub fn add_field_to_events<V: Serialize>(&mut self, key: &str, value: V) -> Result<()> {
if DEFAULT_FIELDS.contains(&key) {
Err(crate::error::Error::CustomError(format!(
"Prohibited to add key: {key} to EventFormatter"
)))
} else {
self.add_default_field_to_events(key, value)
}
}
pub(crate) fn add_default_field_to_events<V: Serialize>(
&mut self,
key: &str,
value: V,
) -> Result<()> {
let val = serde_json::to_value(value)?;
self.default_fields.insert(key.to_owned(), val);
Ok(())
}
}
impl<S, N> FormatEvent<S, N> for EventFormatter
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &FmtContext<'_, S, N>,
mut writer: format::Writer<'_>,
event: &Event<'_>,
) -> fmt::Result {
let serialize = || {
let mut buf = Vec::with_capacity(event.fields().count());
let mut serializer = serde_json::Serializer::new(&mut buf);
let mut map_serializer = serializer.serialize_map(None).unwrap();
let mut visitor = JsonVisitor::default();
event.record(&mut visitor);
self.default_fields
.iter()
.try_for_each(|(key, value)| map_serializer.serialize_entry(key, value))?;
let mut event_storage = visitor.storage;
let message = event_storage.remove(MESSAGE).unwrap_or_default();
map_serializer.serialize_entry(MSG, &message)?;
event_storage
.iter()
.filter(|(key, _)| {
!DEFAULT_FIELDS.contains(&key.as_str())
&& !self.default_fields.contains_key(key.as_str())
})
.try_for_each(|(key, value)| map_serializer.serialize_entry(key, value))?;
if let Some((span_id, trace_id)) = ctx
.lookup_current()
.as_ref()
.map(SpanRef::extensions)
.as_ref()
.and_then(Extensions::get::<OtelData>)
.and_then(|otel_data| {
if otel_data.parent_cx.has_active_span() {
Some(otel_data.parent_cx.span().span_context().trace_id())
} else {
otel_data.builder.trace_id
}
.map(|trace_id| {
let span_id = otel_data.builder.span_id.unwrap_or(SpanId::INVALID);
(span_id, trace_id)
})
})
{
map_serializer.serialize_entry(TRACE_ID, &trace_id.to_string())?;
map_serializer.serialize_entry(SPAN_ID, &span_id.to_string())?;
}
let metadata = event.metadata();
map_serializer.serialize_entry(TARGET, metadata.target())?;
map_serializer.serialize_entry(LOG_LEVEL, metadata.level().as_str())?;
let time = time::OffsetDateTime::now_utc();
let time_ns = time.unix_timestamp_nanos();
map_serializer.serialize_entry(TIME, &time_ns)?;
if let Ok(time) = time.format(
&Iso8601::<
{
Config::DEFAULT
.set_time_precision(TimePrecision::Second {
decimal_digits: NonZeroU8::new(3),
})
.encode()
},
>,
) {
map_serializer.serialize_entry(TIMESTAMP, time.as_str())?;
};
map_serializer.end()?;
Ok(buf)
};
let result: std::io::Result<Vec<u8>> = serialize();
match result {
Ok(formatted) => {
write!(writer, "{}", String::from_utf8_lossy(&formatted))?;
}
Err(err) => {
write!(writer, "{}", err)?;
}
}
writeln!(writer)
}
}
#[derive(Clone, Debug, Default)]
struct JsonVisitor {
storage: BTreeMap<String, Value>,
}
impl JsonVisitor {
fn insert<T: Serialize>(&mut self, key: &str, value: T) {
self.storage
.insert(key.to_owned(), serde_json::json!(value));
}
}
impl tracing::field::Visit for JsonVisitor {
#[cfg(tracing_unstable)]
fn record_value(&mut self, field: &Field, value: valuable::Value<'_>) {
let serde_value = serde_json::json!(Serializable::new(value));
let structurable = value.as_structable();
if let Some(structurable) = structurable {
let definition = structurable.definition();
if definition.is_dynamic() && definition.name() == TRACING_FIELDS_STRUCTURE_NAME {
match serde_value.as_object() {
Some(value) => {
value.into_iter().for_each(|(k, v)| {
self.insert(k.as_str(), v);
});
return;
}
None => {
unreachable!("Named structure should always be mapped to serde_json::Value::Object()")
}
}
}
}
self.insert(field.name(), serde_value);
}
fn record_f64(&mut self, field: &Field, value: f64) {
self.insert(field.name(), value);
}
fn record_i64(&mut self, field: &Field, value: i64) {
self.insert(field.name(), value);
}
fn record_u64(&mut self, field: &Field, value: u64) {
self.insert(field.name(), value);
}
fn record_bool(&mut self, field: &Field, value: bool) {
self.insert(field.name(), value);
}
fn record_str(&mut self, field: &Field, value: &str) {
self.insert(field.name(), value);
}
fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) {
self.insert(field.name(), value.to_string());
}
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
match field.name() {
name if name.starts_with("r#") => {
self.insert(&name[2..], format!("{:?}", value));
}
name => {
self.insert(name, format!("{:?}", value));
}
};
}
}