Skip to content

Commit e27ec9e

Browse files
authored
fix location of panic messages (#67)
1 parent ff370f4 commit e27ec9e

File tree

4 files changed

+68
-68
lines changed

4 files changed

+68
-68
lines changed

src/internal/exporters/console.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ mod tests {
248248
1970-01-01T00:00:00.000002Z DEBUG logfire::internal::exporters::console::tests debug span
249249
1970-01-01T00:00:00.000003Z DEBUG logfire::internal::exporters::console::tests debug span with explicit parent
250250
1970-01-01T00:00:00.000004Z INFO logfire::internal::exporters::console::tests hello world log
251-
[2m1970-01-01T00:00:00.000005Z[0m[31m ERROR[0m [2;3mlogfire[0m [1mpanic: oh no![0m [3mlocation[0m=src/internal/exporters/console.rs:234:17, [3mbacktrace[0m=disabled backtrace
251+
[2m1970-01-01T00:00:00.000005Z[0m[31m ERROR[0m [1mpanic: oh no![0m [3mbacktrace[0m=disabled backtrace
252252
");
253253
}
254254

@@ -296,7 +296,7 @@ mod tests {
296296
 DEBUG logfire::internal::exporters::console::tests debug span
297297
 DEBUG logfire::internal::exporters::console::tests debug span with explicit parent
298298
 INFO logfire::internal::exporters::console::tests hello world log
299-
[31m ERROR[0m [2;3mlogfire[0m [1mpanic: oh no![0m [3mlocation[0m=src/internal/exporters/console.rs:282:17, [3mbacktrace[0m=disabled backtrace
299+
[31m ERROR[0m [1mpanic: oh no![0m [3mbacktrace[0m=disabled backtrace
300300
");
301301
}
302302

@@ -341,7 +341,7 @@ mod tests {
341341
1970-01-01T00:00:00.000000Z INFO logfire::internal::exporters::console::tests root span
342342
1970-01-01T00:00:00.000001Z INFO logfire::internal::exporters::console::tests hello world span
343343
1970-01-01T00:00:00.000002Z INFO logfire::internal::exporters::console::tests hello world log
344-
[2m1970-01-01T00:00:00.000003Z[0m[31m ERROR[0m [2;3mlogfire[0m [1mpanic: oh no![0m [3mlocation[0m=src/internal/exporters/console.rs:329:17, [3mbacktrace[0m=disabled backtrace
344+
[2m1970-01-01T00:00:00.000003Z[0m[31m ERROR[0m [1mpanic: oh no![0m [3mbacktrace[0m=disabled backtrace
345345
");
346346
}
347347

src/lib.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
//!
102102
//! All code instrumented with `log` will therefore automatically be captured by Logfire.
103103
104+
use std::borrow::Cow;
104105
use std::cell::RefCell;
105106
use std::collections::HashMap;
106107
use std::panic::PanicHookInfo;
@@ -119,6 +120,7 @@ use tracing::subscriber::DefaultGuard;
119120
use tracing_subscriber::layer::SubscriberExt;
120121
use tracing_subscriber::registry::LookupSpan;
121122

123+
use crate::__macros_impl::LogfireValue;
122124
use crate::bridges::tracing::LogfireTracingLayer;
123125
use crate::config::{
124126
AdvancedOptions, BoxedSpanProcessor, ConsoleOptions, MetricsOptions, SendToLogfire,
@@ -638,11 +640,20 @@ fn install_panic_handler() {
638640
""
639641
};
640642

641-
// FIXME: code.lineno and code.filepath should probably be set here to the panic location
642-
crate::error!(
643-
"panic: {message}",
644-
location = info.location().as_ref().map(ToString::to_string),
645-
backtrace = Backtrace::capture().to_string(),
643+
let location = info.location();
644+
crate::macros::__macros_impl::export_log_span(
645+
"panic",
646+
&tracing::Span::current(),
647+
format!("panic: {message}"),
648+
tracing::Level::ERROR,
649+
crate::__json_schema!(backtrace),
650+
location.map(|l| Cow::Owned(l.file().to_string())),
651+
location.map(std::panic::Location::line),
652+
None,
653+
[LogfireValue::new(
654+
"backtrace",
655+
Some(Backtrace::capture().to_string().into()),
656+
)],
646657
);
647658
}
648659

src/macros/impl_.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Note that macros exported here will end up at the crate root, they should probably all be prefixed with
44
//! __ just to help avoid collisions with real APIs.
55
6-
use std::{marker::PhantomData, ops::Deref, time::SystemTime};
6+
use std::{borrow::Cow, marker::PhantomData, ops::Deref, time::SystemTime};
77

88
use crate::{bridges::tracing::level_to_level_number, try_with_logfire_tracer};
99
use opentelemetry::{
@@ -140,9 +140,9 @@ pub fn export_log_span(
140140
message: String,
141141
level: tracing::Level,
142142
schema: &'static str,
143-
file: &'static str,
144-
line: u32,
145-
module_path: &'static str,
143+
file: Option<Cow<'static, str>>,
144+
line: Option<u32>,
145+
module_path: Option<&'static str>,
146146
args: impl IntoIterator<Item = LogfireValue>,
147147
) {
148148
thread_local! {
@@ -176,9 +176,6 @@ pub fn export_log_span(
176176
KeyValue::new("logfire.level_num", level_to_level_number(level)),
177177
KeyValue::new("logfire.span_type", "log"),
178178
KeyValue::new("logfire.json_schema", schema),
179-
KeyValue::new("code.filepath", file),
180-
KeyValue::new("code.lineno", i64::from(line)),
181-
KeyValue::new("code.namespace", module_path),
182179
KeyValue::new("thread.id", THREAD_ID.with(|id| *id)),
183180
])
184181
.chain(
@@ -189,6 +186,18 @@ pub fn export_log_span(
189186
)
190187
.collect();
191188

189+
if let Some(file) = file {
190+
attributes.push(KeyValue::new("code.filepath", file));
191+
}
192+
193+
if let Some(line) = line {
194+
attributes.push(KeyValue::new("code.lineno", i64::from(line)));
195+
}
196+
197+
if let Some(module_path) = module_path {
198+
attributes.push(KeyValue::new("code.namespace", module_path));
199+
}
200+
192201
if !null_args.is_empty() {
193202
attributes.push(KeyValue::new(
194203
"logfire.null_args",
@@ -299,9 +308,9 @@ macro_rules! __log {
299308
format!($format),
300309
$level,
301310
$crate::__json_schema!($($($path).+),*),
302-
file!(),
303-
line!(),
304-
module_path!(),
311+
Some(::std::borrow::Cow::Borrowed(file!())),
312+
Some(line!()),
313+
Some(module_path!()),
305314
[
306315
$({
307316
let arg_value = $crate::__evaluate_arg!($($path).+ $(= $value)?);

tests/test_basic_exports.rs

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,47 +1147,47 @@ fn test_basic_span() {
11471147
},
11481148
KeyValue {
11491149
key: Static(
1150-
"code.filepath",
1150+
"thread.id",
11511151
),
1152-
value: String(
1153-
Static(
1154-
"tests/test_basic_exports.rs",
1155-
),
1152+
value: I64(
1153+
0,
11561154
),
11571155
},
11581156
KeyValue {
11591157
key: Static(
1160-
"code.lineno",
1158+
"thread.name",
11611159
),
1162-
value: I64(
1163-
18,
1160+
value: String(
1161+
Owned(
1162+
"test_basic_span",
1163+
),
11641164
),
11651165
},
11661166
KeyValue {
11671167
key: Static(
1168-
"code.namespace",
1168+
"code.filepath",
11691169
),
11701170
value: String(
11711171
Static(
1172-
"test_basic_exports",
1172+
"tests/test_basic_exports.rs",
11731173
),
11741174
),
11751175
},
11761176
KeyValue {
11771177
key: Static(
1178-
"thread.id",
1178+
"code.lineno",
11791179
),
11801180
value: I64(
1181-
0,
1181+
18,
11821182
),
11831183
},
11841184
KeyValue {
11851185
key: Static(
1186-
"thread.name",
1186+
"code.namespace",
11871187
),
11881188
value: String(
1189-
Owned(
1190-
"test_basic_span",
1189+
Static(
1190+
"test_basic_exports",
11911191
),
11921192
),
11931193
},
@@ -1223,7 +1223,7 @@ fn test_basic_span() {
12231223
},
12241224
parent_span_id: 00000000000000f0,
12251225
span_kind: Internal,
1226-
name: "panic: {message}",
1226+
name: "panic",
12271227
start_time: SystemTime {
12281228
tv_sec: 8,
12291229
tv_nsec: 0,
@@ -1233,16 +1233,6 @@ fn test_basic_span() {
12331233
tv_nsec: 0,
12341234
},
12351235
attributes: [
1236-
KeyValue {
1237-
key: Static(
1238-
"location",
1239-
),
1240-
value: String(
1241-
Owned(
1242-
"tests/test_basic_exports.rs:58:13",
1243-
),
1244-
),
1245-
},
12461236
KeyValue {
12471237
key: Static(
12481238
"backtrace",
@@ -1287,54 +1277,44 @@ fn test_basic_span() {
12871277
),
12881278
value: String(
12891279
Static(
1290-
"{\"type\":\"object\",\"properties\":{\"location\":{},\"backtrace\":{}}}",
1291-
),
1292-
),
1293-
},
1294-
KeyValue {
1295-
key: Static(
1296-
"code.filepath",
1297-
),
1298-
value: String(
1299-
Static(
1300-
"src/lib.rs",
1280+
"{\"type\":\"object\",\"properties\":{\"backtrace\":{}}}",
13011281
),
13021282
),
13031283
},
13041284
KeyValue {
13051285
key: Static(
1306-
"code.lineno",
1286+
"thread.id",
13071287
),
13081288
value: I64(
1309-
642,
1289+
0,
13101290
),
13111291
},
13121292
KeyValue {
13131293
key: Static(
1314-
"code.namespace",
1294+
"thread.name",
13151295
),
13161296
value: String(
1317-
Static(
1318-
"logfire",
1297+
Owned(
1298+
"test_basic_span",
13191299
),
13201300
),
13211301
},
13221302
KeyValue {
13231303
key: Static(
1324-
"thread.id",
1304+
"code.filepath",
13251305
),
1326-
value: I64(
1327-
0,
1306+
value: String(
1307+
Owned(
1308+
"tests/test_basic_exports.rs",
1309+
),
13281310
),
13291311
},
13301312
KeyValue {
13311313
key: Static(
1332-
"thread.name",
1314+
"code.lineno",
13331315
),
1334-
value: String(
1335-
Owned(
1336-
"test_basic_span",
1337-
),
1316+
value: I64(
1317+
19,
13381318
),
13391319
},
13401320
],

0 commit comments

Comments
 (0)