Skip to content

Commit ed19bf7

Browse files
committed
fix(ffi): New fields formatter to remove duplicated span fields.
This patch fixes a bug in the tracing system. It introduces one fields formatter _per layer_ to force the fields to be recorded in different span extensions, and thus to remove the duplicated fields in `FormattedFields`. The patch contains links to the bug report in `tokio-rs/tracing`. This patch is a workaround.
1 parent eeb325a commit ed19bf7

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

bindings/matrix-sdk-ffi/src/platform.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
use tracing_appender::rolling::{RollingFileAppender, Rotation};
22
use tracing_core::Subscriber;
33
use tracing_subscriber::{
4-
fmt::{self, time::FormatTime, FormatEvent, FormatFields, FormattedFields},
4+
field::RecordFields,
5+
fmt::{
6+
self,
7+
format::{DefaultFields, Writer},
8+
time::FormatTime,
9+
FormatEvent, FormatFields, FormattedFields,
10+
},
511
layer::SubscriberExt,
612
registry::LookupSpan,
713
util::SubscriberInitExt,
@@ -135,7 +141,25 @@ where
135141

136142
let writer = builder.build(&c.path).expect("Failed to create a rolling file appender.");
137143

144+
// Another fields formatter is necessary because of this bug
145+
// https://github.com/tokio-rs/tracing/issues/1372. Using a new
146+
// formatter for the fields forces to record them in different span
147+
// extensions, and thus remove the duplicated fields in the span.
148+
#[derive(Default)]
149+
struct FieldsFormatterForFiles(DefaultFields);
150+
151+
impl<'writer> FormatFields<'writer> for FieldsFormatterForFiles {
152+
fn format_fields<R: RecordFields>(
153+
&self,
154+
writer: Writer<'writer>,
155+
fields: R,
156+
) -> std::fmt::Result {
157+
self.0.format_fields(writer, fields)
158+
}
159+
}
160+
138161
fmt::layer()
162+
.fmt_fields(FieldsFormatterForFiles::default())
139163
.event_format(EventFormatter::new())
140164
// EventFormatter doesn't support ANSI colors anyways, but the
141165
// default field formatter does, which is unhelpful for iOS +
@@ -147,15 +171,34 @@ where
147171
Layer::and_then(
148172
file_layer,
149173
config.write_to_stdout_or_system.then(|| {
174+
// Another fields formatter is necessary because of this bug
175+
// https://github.com/tokio-rs/tracing/issues/1372. Using a new
176+
// formatter for the fields forces to record them in different span
177+
// extensions, and thus remove the duplicated fields in the span.
178+
#[derive(Default)]
179+
struct FieldsFormatterFormStdoutOrSystem(DefaultFields);
180+
181+
impl<'writer> FormatFields<'writer> for FieldsFormatterFormStdoutOrSystem {
182+
fn format_fields<R: RecordFields>(
183+
&self,
184+
writer: Writer<'writer>,
185+
fields: R,
186+
) -> std::fmt::Result {
187+
self.0.format_fields(writer, fields)
188+
}
189+
}
190+
150191
#[cfg(not(target_os = "android"))]
151192
return fmt::layer()
193+
.fmt_fields(FieldsFormatterFormStdoutOrSystem::default())
152194
.event_format(EventFormatter::new())
153195
// See comment above.
154196
.with_ansi(false)
155197
.with_writer(std::io::stderr);
156198

157199
#[cfg(target_os = "android")]
158200
return fmt::layer()
201+
.fmt_fields(FieldsFormatterFormStdoutOrSystem::default())
159202
.event_format(EventFormatter::for_logcat())
160203
// See comment above.
161204
.with_ansi(false)

0 commit comments

Comments
 (0)