forked from getsentry/sentry-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tracing.rs
182 lines (155 loc) · 5.89 KB
/
test_tracing.rs
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
#![cfg(feature = "test")]
use sentry::protocol::{Context, Request, Value};
use tracing_subscriber::prelude::*;
#[test]
fn test_tracing() {
// Don't configure the fmt layer to avoid logging to test output
let _dispatcher = tracing_subscriber::registry()
.with(sentry_tracing::layer())
.set_default();
#[tracing::instrument(err)]
fn fn_errors() -> Result<(), Box<dyn std::error::Error>> {
Err("I'm broken!".into())
}
let events = sentry::test::with_captured_events(|| {
sentry::configure_scope(|scope| {
scope.set_tag("worker", "worker1");
});
tracing::info!("Hello Tracing World!");
tracing::error!(tagname = "tagvalue", "Shit's on fire yo");
log::info!("Hello Logging World!");
log::error!("Shit's on fire yo");
let err = "NaN".parse::<usize>().unwrap_err();
let err: &dyn std::error::Error = &err;
tracing::error!(err, tagname = "tagvalue");
let _ = fn_errors();
});
assert_eq!(events.len(), 4);
let mut events = events.into_iter();
let event = events.next().unwrap();
assert_eq!(event.tags["worker"], "worker1");
assert_eq!(event.level, sentry::Level::Error);
assert_eq!(event.message, Some("Shit's on fire yo".to_owned()));
assert_eq!(event.breadcrumbs.len(), 1);
assert_eq!(event.breadcrumbs[0].level, sentry::Level::Info);
assert_eq!(
event.breadcrumbs[0].message,
Some("Hello Tracing World!".into())
);
match event.contexts.get("Rust Tracing Tags").unwrap() {
Context::Other(tags) => {
let value = Value::String("tagvalue".to_string());
assert_eq!(*tags.get("tagname").unwrap(), value);
}
_ => panic!("Wrong context type"),
}
match event.contexts.get("Rust Tracing Location").unwrap() {
Context::Other(tags) => {
assert!(matches!(tags.get("module_path").unwrap(), Value::String(_)));
assert!(matches!(tags.get("file").unwrap(), Value::String(_)));
assert!(matches!(tags.get("line").unwrap(), Value::Number(_)));
}
_ => panic!("Wrong context type"),
}
let event = events.next().unwrap();
assert_eq!(event.tags["worker"], "worker1");
assert_eq!(event.level, sentry::Level::Error);
assert_eq!(event.message, Some("Shit's on fire yo".to_owned()));
assert_eq!(event.breadcrumbs.len(), 2);
assert_eq!(event.breadcrumbs[0].level, sentry::Level::Info);
assert_eq!(
event.breadcrumbs[0].message,
Some("Hello Tracing World!".into())
);
assert_eq!(event.breadcrumbs[1].level, sentry::Level::Info);
assert_eq!(
event.breadcrumbs[1].message,
Some("Hello Logging World!".into())
);
let event = events.next().unwrap();
assert!(!event.exception.is_empty());
assert_eq!(event.exception[0].ty, "ParseIntError");
assert_eq!(
event.exception[0].value,
Some("invalid digit found in string".into())
);
match event.contexts.get("Rust Tracing Tags").unwrap() {
Context::Other(tags) => {
let value = Value::String("tagvalue".to_string());
assert_eq!(*tags.get("tagname").unwrap(), value);
}
_ => panic!("Wrong context type"),
}
match event.contexts.get("Rust Tracing Location").unwrap() {
Context::Other(tags) => {
assert!(matches!(tags.get("module_path").unwrap(), Value::String(_)));
assert!(matches!(tags.get("file").unwrap(), Value::String(_)));
assert!(matches!(tags.get("line").unwrap(), Value::Number(_)));
}
_ => panic!("Wrong context type"),
}
let event = events.next().unwrap();
assert_eq!(event.message, Some("I'm broken!".to_string()));
}
#[tracing::instrument(fields(span_field))]
fn function() {
tracing::Span::current().record("span_field", &"some data");
}
#[test]
fn test_span_record() {
let _dispatcher = tracing_subscriber::registry()
.with(sentry_tracing::layer())
.set_default();
let options = sentry::ClientOptions {
traces_sample_rate: 1.0,
..Default::default()
};
let envelopes = sentry::test::with_captured_envelopes_options(
|| {
let _span = tracing::span!(tracing::Level::INFO, "span").entered();
function();
},
options,
);
assert_eq!(envelopes.len(), 1);
let envelope_item = envelopes[0].items().next().unwrap();
let transaction = match envelope_item {
sentry::protocol::EnvelopeItem::Transaction(t) => t,
_ => panic!("expected only a transaction item"),
};
assert_eq!(
transaction.spans[0].data["span_field"].as_str().unwrap(),
"some data"
);
}
#[test]
fn test_set_transaction() {
let options = sentry::ClientOptions {
traces_sample_rate: 1.0,
..Default::default()
};
let envelopes = sentry::test::with_captured_envelopes_options(
|| {
let ctx = sentry::TransactionContext::new("old name", "ye, whatever");
let trx = sentry::start_transaction(ctx);
let request = Request {
url: Some("https://honk.beep".parse().unwrap()),
method: Some("GET".to_string()),
..Request::default()
};
trx.set_request(request);
sentry::configure_scope(|scope| scope.set_span(Some(trx.clone().into())));
sentry::configure_scope(|scope| scope.set_transaction(Some("new name")));
trx.finish();
},
options,
);
assert_eq!(envelopes.len(), 1);
let envelope_item = envelopes[0].items().next().unwrap();
let transaction = match envelope_item {
sentry::protocol::EnvelopeItem::Transaction(t) => t,
_ => panic!("expected only a transaction item"),
};
assert_eq!(transaction.name.as_deref().unwrap(), "new name");
assert!(transaction.request.is_some());
}