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
299
300
301
302
303
304
305
306
307
308
309
310
311
use std::{borrow::Cow, fmt, ops::Deref};
use serde::{ser::SerializeMap, Serializer};
use tracing::{Event, Metadata, Subscriber};
#[cfg(feature = "tracing-log")]
use tracing_log::NormalizeEvent;
use tracing_subscriber::{
layer::Context,
registry::{LookupSpan, SpanRef},
};
use crate::{
cached::Cached,
cursor::Cursor,
layer::{JsonLayer, JsonValue, SchemaKey},
serde::JsonSubscriberFormatter,
};
/// The same thing as [`SpanRef`] but for events.
pub struct EventRef<'a, 'b, 'c, R: for<'lookup> LookupSpan<'lookup>> {
context: &'a Context<'b, R>,
event: &'a Event<'b>,
span: Option<SpanRef<'c, R>>,
}
impl<'a, R: for<'lookup> LookupSpan<'lookup>> Deref for EventRef<'a, '_, '_, R> {
type Target = Event<'a>;
fn deref(&self) -> &Self::Target {
self.event
}
}
impl<'c, R: Subscriber + for<'lookup> LookupSpan<'lookup>> EventRef<'_, '_, 'c, R> {
/// Returns the span's name,
#[allow(dead_code)]
pub fn name(&self) -> &'static str {
self.event.metadata().name()
}
#[cfg(feature = "tracing-log")]
#[allow(dead_code)]
pub fn normalized_metadata(&self) -> Option<Metadata<'_>> {
self.event.normalized_metadata()
}
pub fn metadata(&self) -> &'static Metadata<'static> {
self.event.metadata()
}
/// Returns a `SpanRef` describing this span's parent, or `None` if this
/// span is the root of its trace tree.
pub fn parent_span(&self) -> Option<&SpanRef<'c, R>> {
self.span.as_ref()
}
pub(super) fn event(&self) -> &Event<'_> {
self.event
}
pub(super) fn context(&self) -> &Context<'_, R> {
self.context
}
}
impl<S, W> JsonLayer<S, W>
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
pub(crate) fn format_event(
&self,
context: &Context<'_, S>,
writer: &mut String,
event: &Event<'_>,
) -> fmt::Result {
let mut visit = || {
let writer = Cursor::new(writer);
let mut serializer =
serde_json::Serializer::with_formatter(&writer, JsonSubscriberFormatter);
let mut serializer = serializer.serialize_map(None)?;
let span = context.event_span(event);
let event_ref = EventRef {
context,
event,
span,
};
let mut serialized_anything = false;
let mut serialized_anything_serde = false;
for (SchemaKey::Static(key), value) in &self.keyed_values {
let Some(value) = resolve_json_value(value, &event_ref) else {
continue;
};
match value {
MaybeCached::Serde(value) => {
if serialized_anything && !serialized_anything_serde {
writer.inner_mut().push(',');
}
serialized_anything = true;
serialized_anything_serde = true;
serializer.serialize_entry(key, &value)?;
},
MaybeCached::Cached(Cached::Raw(raw)) => {
debug_assert!(
serde_json::to_value(&*raw).is_ok(),
"[json-subscriber] provided cached value is not valid json: {raw}",
);
let mut writer = writer.inner_mut();
if serialized_anything {
writer.push(',');
}
serialized_anything = true;
writer.push('"');
writer.push_str(key);
writer.push_str("\":");
writer.push_str(&raw);
},
MaybeCached::Cached(Cached::Array(arr)) => {
let mut writer = writer.inner_mut();
if serialized_anything {
writer.push(',');
}
serialized_anything = true;
writer.push('"');
writer.push_str(key);
writer.push_str("\":[");
let mut first = true;
for raw in arr {
debug_assert!(
serde_json::to_value(&*raw).is_ok(),
"[json-subscriber] provided cached value in array is not valid \
json: {raw}",
);
if !first {
writer.push(',');
}
first = false;
writer.push_str(&raw);
}
writer.push(']');
},
MaybeCached::Raw(raw_fun) => {
let mut writer = writer.inner_mut();
let rollback_position = writer.len();
if serialized_anything {
writer.push(',');
}
writer.push('"');
writer.push_str(key);
writer.push_str("\":");
let start_position = writer.len();
match raw_fun(&event_ref, &mut *writer) {
Ok(()) => {
debug_assert!(
serde_json::to_value(&writer[start_position..]).is_ok(),
"[json-subscriber] raw value factory created invalid json: {}",
&writer[start_position..],
);
serialized_anything = true;
},
Err(error) => {
eprintln!(
"[json-subscriber] unable to format raw value to string: \
{error}"
);
writer.truncate(rollback_position);
},
}
},
}
}
for value in self.flattened_values.values() {
let Some(value) = resolve_json_value(value, &event_ref) else {
continue;
};
match value {
MaybeCached::Serde(value) => {
let map = value.as_object().unwrap();
if !map.is_empty() {
if serialized_anything && !serialized_anything_serde {
writer.inner_mut().push(',');
}
serialized_anything = true;
serialized_anything_serde = true;
for (key, value) in map {
serializer.serialize_entry(key, value)?;
}
}
},
MaybeCached::Cached(Cached::Raw(raw)) => {
debug_assert!(
serde_json::to_value(&*raw).is_ok(),
"[json-subscriber] provided cached value is not valid json: {raw}",
);
if !raw.contains('\"') {
// If the raw string contains at least a single quote, there is at least
// one field in the object. Otherwise it is empty and we just skip it.
// Assuming it's a valid JSON of course.
continue;
}
let Some(object_contents) = raw
.as_ref()
.trim()
.strip_prefix('{')
.and_then(|str| str.strip_suffix('}'))
else {
eprintln!(
"[json-subscriber] provided cached value cannot be flattened \
because it is not an object: {raw}"
);
continue;
};
let mut writer = writer.inner_mut();
if serialized_anything {
writer.push(',');
}
serialized_anything = true;
writer.push_str(object_contents);
},
MaybeCached::Cached(Cached::Array(_arr)) => {
todo!();
},
MaybeCached::Raw(raw_fun) => {
let mut output = String::new();
match raw_fun(&event_ref, &mut output) {
Ok(()) => {
debug_assert!(
serde_json::to_value(&output).is_ok(),
"[json-subscriber] raw value factory created invalid json: \
{output}",
);
let Some(object_contents) = output
.trim()
.strip_prefix('{')
.and_then(|str| str.strip_suffix('}'))
else {
eprintln!(
"[json-subscriber] provided cached value cannot be \
flattened because it is not an object: {output}"
);
continue;
};
let mut writer = writer.inner_mut();
if serialized_anything {
writer.push(',');
}
serialized_anything = true;
writer.push_str(object_contents);
},
Err(error) => {
eprintln!(
"[json-subscriber] unable to format raw value to string: \
{error}"
);
},
}
},
}
}
serializer.end()
};
visit().map_err(|_| fmt::Error)?;
writer.push('\n');
debug_assert!(
serde_json::to_value(&*writer).is_ok(),
"[json-subscriber] serialized line is not valid json: {writer}",
);
Ok(())
}
}
fn resolve_json_value<'a, S: Subscriber + for<'lookup> LookupSpan<'lookup>>(
value: &'a JsonValue<S>,
event: &EventRef<'_, '_, '_, S>,
) -> Option<MaybeCached<'a, S>> {
match value {
JsonValue::Serde(value) => Some(MaybeCached::Serde(Cow::Borrowed(value))),
JsonValue::DynamicFromEvent(fun) => fun(event).map(Cow::Owned).map(MaybeCached::Serde),
JsonValue::DynamicFromSpan(fun) => {
event
.parent_span()
.and_then(fun)
.map(Cow::Owned)
.map(MaybeCached::Serde)
},
JsonValue::DynamicCachedFromSpan(fun) => {
event.parent_span().and_then(fun).map(MaybeCached::Cached)
},
JsonValue::DynamicRawFromEvent(fun) => Some(MaybeCached::Raw(fun)),
}
}
#[allow(clippy::type_complexity)]
enum MaybeCached<'a, S: for<'lookup> LookupSpan<'lookup>> {
Serde(Cow<'a, serde_json::Value>),
Cached(Cached),
Raw(
&'a Box<dyn Fn(&EventRef<'_, '_, '_, S>, &mut dyn fmt::Write) -> fmt::Result + Send + Sync>,
),
}