Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic test for tracing layer #703

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions sentry-tracing/tests/tracing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use sentry::{ClientOptions, Hub};
use sentry_core::test::TestTransport;

use std::sync::Arc;

fn init_sentry() -> Arc<TestTransport> {
use tracing_subscriber::prelude::*;

let transport = TestTransport::new();
let options = ClientOptions {
dsn: Some("https://[email protected]/test".parse().unwrap()),
transport: Some(Arc::new(transport.clone())),
sample_rate: 1.0,
traces_sample_rate: 1.0,
..ClientOptions::default()
};
Hub::current().bind_client(Some(Arc::new(options.into())));

let _ = tracing_subscriber::registry()
.with(sentry_tracing::layer().enable_span_attributes())
.try_init();

transport
}

#[tracing::instrument(fields(tags.tag = "key", not_tag = "value"))]
fn function_with_tags(value: i32) {
tracing::error!(value, "event");
}

#[test]
fn should_instrument_function_with_event() {
let transport = init_sentry();

function_with_tags(1);

let data = transport.fetch_and_clear_envelopes();
assert_eq!(data.len(), 2);
let event = data.first().expect("should have 1 event");
let event = match event.items().next().unwrap() {
sentry::protocol::EnvelopeItem::Event(event) => event,
unexpected => panic!("Expected event, but got {:#?}", unexpected),
};

//Validate transaction is created
let trace = match event.contexts.get("trace").expect("to get 'trace' context") {
sentry::protocol::Context::Trace(trace) => trace,
unexpected => panic!("Expected trace context but got {:?}", unexpected),
};
assert_eq!(trace.op.as_deref().unwrap(), "function_with_tags");

//Confirm transaction values
let transaction = data.get(1).expect("should have 1 transaction");
let transaction = match transaction.items().next().unwrap() {
sentry::protocol::EnvelopeItem::Transaction(transaction) => transaction,
unexpected => panic!("Expected transaction, but got {:#?}", unexpected),
};
assert_eq!(transaction.tags.len(), 1);
assert_eq!(transaction.extra.len(), 2);

let tag = transaction
.tags
.get("tag")
.expect("to have tag with name 'tag'");
assert_eq!(tag, "key");
let not_tag = transaction
.extra
.get("not_tag")
.expect("to have extra with name 'not_tag'");
assert_eq!(not_tag, "value");
let value = transaction
.extra
.get("value")
.expect("to have extra with name 'value'");
assert_eq!(value, 1);
}