|
| 1 | +use sentry::{ClientOptions, Hub}; |
| 2 | +use sentry_core::test::TestTransport; |
| 3 | + |
| 4 | +use std::sync::Arc; |
| 5 | + |
| 6 | +fn init_sentry() -> Arc<TestTransport> { |
| 7 | + use tracing_subscriber::prelude::*; |
| 8 | + |
| 9 | + let transport = TestTransport::new(); |
| 10 | + let options = ClientOptions { |
| 11 | + dsn: Some("https://test@sentry-tracing.com/test".parse().unwrap()), |
| 12 | + transport: Some(Arc::new(transport.clone())), |
| 13 | + sample_rate: 1.0, |
| 14 | + traces_sample_rate: 1.0, |
| 15 | + ..ClientOptions::default() |
| 16 | + }; |
| 17 | + Hub::current().bind_client(Some(Arc::new(options.into()))); |
| 18 | + |
| 19 | + let _ = tracing_subscriber::registry() |
| 20 | + .with(sentry_tracing::layer().enable_span_attributes()) |
| 21 | + .try_init(); |
| 22 | + |
| 23 | + transport |
| 24 | +} |
| 25 | + |
| 26 | +#[tracing::instrument(fields(tags.tag = "key", not_tag = "value"))] |
| 27 | +fn function_with_tags(value: i32) { |
| 28 | + tracing::error!(value, "event"); |
| 29 | +} |
| 30 | + |
| 31 | +#[test] |
| 32 | +fn should_instrument_function_with_event() { |
| 33 | + let transport = init_sentry(); |
| 34 | + |
| 35 | + function_with_tags(1); |
| 36 | + |
| 37 | + let data = transport.fetch_and_clear_envelopes(); |
| 38 | + assert_eq!(data.len(), 2); |
| 39 | + let event = data.first().expect("should have 1 event"); |
| 40 | + let event = match event.items().next().unwrap() { |
| 41 | + sentry::protocol::EnvelopeItem::Event(event) => event, |
| 42 | + unexpected => panic!("Expected event, but got {:#?}", unexpected), |
| 43 | + }; |
| 44 | + |
| 45 | + //Validate transaction is created |
| 46 | + let trace = match event.contexts.get("trace").expect("to get 'trace' context") { |
| 47 | + sentry::protocol::Context::Trace(trace) => trace, |
| 48 | + unexpected => panic!("Expected trace context but got {:?}", unexpected), |
| 49 | + }; |
| 50 | + assert_eq!(trace.op.as_deref().unwrap(), "function_with_tags"); |
| 51 | + |
| 52 | + //Confirm transaction values |
| 53 | + let transaction = data.get(1).expect("should have 1 transaction"); |
| 54 | + let transaction = match transaction.items().next().unwrap() { |
| 55 | + sentry::protocol::EnvelopeItem::Transaction(transaction) => transaction, |
| 56 | + unexpected => panic!("Expected transaction, but got {:#?}", unexpected), |
| 57 | + }; |
| 58 | + assert_eq!(transaction.tags.len(), 1); |
| 59 | + assert_eq!(transaction.extra.len(), 2); |
| 60 | + |
| 61 | + let tag = transaction |
| 62 | + .tags |
| 63 | + .get("tag") |
| 64 | + .expect("to have tag with name 'tag'"); |
| 65 | + assert_eq!(tag, "key"); |
| 66 | + let not_tag = transaction |
| 67 | + .extra |
| 68 | + .get("not_tag") |
| 69 | + .expect("to have extra with name 'not_tag'"); |
| 70 | + assert_eq!(not_tag, "value"); |
| 71 | + let value = transaction |
| 72 | + .extra |
| 73 | + .get("value") |
| 74 | + .expect("to have extra with name 'value'"); |
| 75 | + assert_eq!(value, 1); |
| 76 | +} |
0 commit comments