1use std::collections::BTreeMap;
2use std::error::Error;
3
4use sentry_core::protocol::{Event, Exception, Mechanism, Value};
5use sentry_core::{event_from_error, Breadcrumb, Level, TransactionOrSpan};
6use tracing_core::field::{Field, Visit};
7use tracing_core::Subscriber;
8use tracing_subscriber::layer::Context;
9use tracing_subscriber::registry::LookupSpan;
10
11use super::layer::SentrySpanData;
12use crate::TAGS_PREFIX;
13
14fn convert_tracing_level(level: &tracing_core::Level) -> Level {
16 match level {
17 &tracing_core::Level::TRACE | &tracing_core::Level::DEBUG => Level::Debug,
18 &tracing_core::Level::INFO => Level::Info,
19 &tracing_core::Level::WARN => Level::Warning,
20 &tracing_core::Level::ERROR => Level::Error,
21 }
22}
23
24#[allow(unused)]
25fn level_to_exception_type(level: &tracing_core::Level) -> &'static str {
26 match *level {
27 tracing_core::Level::TRACE => "tracing::trace!",
28 tracing_core::Level::DEBUG => "tracing::debug!",
29 tracing_core::Level::INFO => "tracing::info!",
30 tracing_core::Level::WARN => "tracing::warn!",
31 tracing_core::Level::ERROR => "tracing::error!",
32 }
33}
34
35fn extract_event_data(event: &tracing_core::Event) -> (Option<String>, FieldVisitor) {
38 let mut visitor = FieldVisitor::default();
40 event.record(&mut visitor);
41 let message = visitor
42 .json_values
43 .remove("message")
44 .or_else(|| visitor.json_values.remove("error"))
47 .and_then(|v| match v {
48 Value::String(s) => Some(s),
49 _ => None,
50 });
51
52 (message, visitor)
53}
54
55fn extract_event_data_with_context<S>(
56 event: &tracing_core::Event,
57 ctx: Option<Context<S>>,
58) -> (Option<String>, FieldVisitor)
59where
60 S: Subscriber + for<'a> LookupSpan<'a>,
61{
62 let (message, mut visitor) = extract_event_data(event);
63
64 let current_span = ctx.as_ref().and_then(|ctx| {
66 event
67 .parent()
68 .and_then(|id| ctx.span(id))
69 .or_else(|| ctx.lookup_current())
70 });
71 if let Some(span) = current_span {
72 for span in span.scope() {
73 let name = span.name();
74 let ext = span.extensions();
75 if let Some(span_data) = ext.get::<SentrySpanData>() {
76 match &span_data.sentry_span {
77 TransactionOrSpan::Span(span) => {
78 for (key, value) in span.data().iter() {
79 if key != "message" {
80 let key = format!("{}:{}", name, key);
81 visitor.json_values.insert(key, value.clone());
82 }
83 }
84 }
85 TransactionOrSpan::Transaction(transaction) => {
86 for (key, value) in transaction.data().iter() {
87 if key != "message" {
88 let key = format!("{}:{}", name, key);
89 visitor.json_values.insert(key, value.clone());
90 }
91 }
92 }
93 }
94 }
95 }
96 }
97
98 (message, visitor)
99}
100
101#[derive(Default)]
103pub(crate) struct FieldVisitor {
104 pub json_values: BTreeMap<String, Value>,
105 pub exceptions: Vec<Exception>,
106}
107
108impl FieldVisitor {
109 fn record<T: Into<Value>>(&mut self, field: &Field, value: T) {
110 self.json_values
111 .insert(field.name().to_owned(), value.into());
112 }
113}
114
115impl Visit for FieldVisitor {
116 fn record_i64(&mut self, field: &Field, value: i64) {
117 self.record(field, value);
118 }
119
120 fn record_u64(&mut self, field: &Field, value: u64) {
121 self.record(field, value);
122 }
123
124 fn record_bool(&mut self, field: &Field, value: bool) {
125 self.record(field, value);
126 }
127
128 fn record_str(&mut self, field: &Field, value: &str) {
129 self.record(field, value);
130 }
131
132 fn record_error(&mut self, _field: &Field, value: &(dyn Error + 'static)) {
133 let event = event_from_error(value);
134 for exception in event.exception {
135 self.exceptions.push(exception);
136 }
137 }
138
139 fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
140 self.record(field, format!("{value:?}"));
141 }
142}
143
144pub fn breadcrumb_from_event<'context, S>(
146 event: &tracing_core::Event,
147 ctx: impl Into<Option<Context<'context, S>>>,
148) -> Breadcrumb
149where
150 S: Subscriber + for<'a> LookupSpan<'a>,
151{
152 let (message, visitor) = extract_event_data_with_context(event, ctx.into());
153
154 let FieldVisitor {
155 exceptions,
156 mut json_values,
157 } = visitor;
158
159 let errors = exceptions
160 .iter()
161 .rev()
162 .filter_map(|x| x.value.as_ref().map(|v| format!("{}: {}", x.ty, *v)))
163 .collect::<Vec<String>>();
164 if !errors.is_empty() {
165 json_values.insert("errors".to_owned(), errors.into());
166 }
167
168 Breadcrumb {
169 category: Some(event.metadata().target().to_owned()),
170 ty: "log".into(),
171 level: convert_tracing_level(event.metadata().level()),
172 message,
173 data: json_values,
174 ..Default::default()
175 }
176}
177
178fn tags_from_event(fields: &mut BTreeMap<String, Value>) -> BTreeMap<String, String> {
179 let mut tags = BTreeMap::new();
180
181 fields.retain(|key, value| {
182 let Some(key) = key.strip_prefix(TAGS_PREFIX) else {
183 return true;
184 };
185 let string = match value {
186 Value::Bool(b) => b.to_string(),
187 Value::Number(n) => n.to_string(),
188 Value::String(s) => std::mem::take(s),
189 Value::Null => return false,
191 Value::Array(_) | Value::Object(_) => return true,
193 };
194
195 tags.insert(key.to_owned(), string);
196
197 false
198 });
199
200 tags
201}
202
203fn contexts_from_event(
204 event: &tracing_core::Event,
205 fields: BTreeMap<String, Value>,
206) -> BTreeMap<String, sentry_core::protocol::Context> {
207 let event_meta = event.metadata();
208 let mut location_map = BTreeMap::new();
209 if let Some(module_path) = event_meta.module_path() {
210 location_map.insert("module_path".to_string(), module_path.into());
211 }
212 if let Some(file) = event_meta.file() {
213 location_map.insert("file".to_string(), file.into());
214 }
215 if let Some(line) = event_meta.line() {
216 location_map.insert("line".to_string(), line.into());
217 }
218
219 let mut context = BTreeMap::new();
220 if !fields.is_empty() {
221 context.insert(
222 "Rust Tracing Fields".to_string(),
223 sentry_core::protocol::Context::Other(fields),
224 );
225 }
226 if !location_map.is_empty() {
227 context.insert(
228 "Rust Tracing Location".to_string(),
229 sentry_core::protocol::Context::Other(location_map),
230 );
231 }
232 context
233}
234
235pub fn event_from_event<'context, S>(
237 event: &tracing_core::Event,
238 ctx: impl Into<Option<Context<'context, S>>>,
239) -> Event<'static>
240where
241 S: Subscriber + for<'a> LookupSpan<'a>,
242{
243 #[allow(unused_mut)]
248 let (mut message, visitor) = extract_event_data_with_context(event, ctx.into());
249 let FieldVisitor {
250 mut exceptions,
251 mut json_values,
252 } = visitor;
253
254 #[cfg(feature = "backtrace")]
260 if !exceptions.is_empty() && message.is_some() {
261 if let Some(client) = sentry_core::Hub::current().client() {
262 if client.options().attach_stacktrace {
263 let thread = sentry_backtrace::current_thread(true);
264 let exception = Exception {
265 ty: level_to_exception_type(event.metadata().level()).to_owned(),
266 value: message.take(),
267 module: event.metadata().module_path().map(str::to_owned),
268 stacktrace: thread.stacktrace,
269 raw_stacktrace: thread.raw_stacktrace,
270 thread_id: thread.id,
271 mechanism: Some(Mechanism {
272 synthetic: Some(true),
273 ..Mechanism::default()
274 }),
275 };
276 exceptions.push(exception)
277 }
278 }
279 }
280
281 if let Some(exception) = exceptions.last_mut() {
282 "tracing".clone_into(
283 &mut exception
284 .mechanism
285 .get_or_insert_with(Mechanism::default)
286 .ty,
287 );
288 }
289
290 Event {
291 logger: Some(event.metadata().target().to_owned()),
292 level: convert_tracing_level(event.metadata().level()),
293 message,
294 exception: exceptions.into(),
295 tags: tags_from_event(&mut json_values),
296 contexts: contexts_from_event(event, json_values),
297 ..Default::default()
298 }
299}