From 42f78e3a67c53668c71e2d5aa34068ce1bafb5c5 Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Tue, 1 Mar 2022 08:48:39 -0800 Subject: [PATCH 1/6] add request to transaction Signed-off-by: Jess Frazelle --- sentry-core/src/performance.rs | 22 ++++++++++++++++++++++ sentry-types/src/protocol/v7.rs | 11 ++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/sentry-core/src/performance.rs b/sentry-core/src/performance.rs index 91b6f99f..bc86386f 100644 --- a/sentry-core/src/performance.rs +++ b/sentry-core/src/performance.rs @@ -202,6 +202,14 @@ impl TransactionOrSpan { } } + /// Set the HTTP request information for this Transaction/Span. + pub fn set_request(&self, request: protocol::Request) { + match self { + TransactionOrSpan::Transaction(transaction) => transaction.set_request(request), + TransactionOrSpan::Span(span) => span.set_request(request), + } + } + /// Returns the headers needed for distributed tracing. pub fn iter_headers(&self) -> TraceHeadersIter { match self { @@ -355,6 +363,14 @@ impl Transaction { inner.context.status = Some(status); } + /// Set the HTTP request information for this Transaction. + pub fn set_request(&self, request: protocol::Request) { + let mut inner = self.inner.lock().unwrap(); + if let Some(transaction) = inner.transaction.as_mut() { + transaction.request = Some(request); + } + } + /// Returns the headers needed for distributed tracing. pub fn iter_headers(&self) -> TraceHeadersIter { let inner = self.inner.lock().unwrap(); @@ -454,6 +470,12 @@ impl Span { span.status = Some(status); } + /// Set the HTTP request information for this Span. + pub fn set_request(&self, request: protocol::Request) { + let mut span = self.span.lock().unwrap(); + span.request = Some(request); + } + /// Returns the headers needed for distributed tracing. pub fn iter_headers(&self) -> TraceHeadersIter { let span = self.span.lock().unwrap(); diff --git a/sentry-types/src/protocol/v7.rs b/sentry-types/src/protocol/v7.rs index 268572b1..764a39ab 100644 --- a/sentry-types/src/protocol/v7.rs +++ b/sentry-types/src/protocol/v7.rs @@ -15,7 +15,7 @@ use std::ops; use std::str; use std::time::SystemTime; -use ::debugid::{CodeId, DebugId}; +use self::debugid::{CodeId, DebugId}; use serde::Serializer; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -1737,6 +1737,9 @@ pub struct Span { /// Optional extra information to be sent with the span. #[serde(default, skip_serializing_if = "Map::is_empty")] pub data: Map, + /// Optionally HTTP request data to be sent along. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub request: Option, } impl Default for Span { @@ -1753,6 +1756,7 @@ impl Default for Span { same_process_as_parent: Default::default(), op: Default::default(), data: Default::default(), + request: Default::default(), } } } @@ -1942,6 +1946,9 @@ pub struct Transaction<'a> { /// Optional contexts. #[serde(default, skip_serializing_if = "Map::is_empty")] pub contexts: Map, + /// Optionally HTTP request data to be sent along. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub request: Option, } impl<'a> Default for Transaction<'a> { @@ -1959,6 +1966,7 @@ impl<'a> Default for Transaction<'a> { start_timestamp: SystemTime::now(), spans: Default::default(), contexts: Default::default(), + request: Default::default(), } } } @@ -1984,6 +1992,7 @@ impl<'a> Transaction<'a> { start_timestamp: self.start_timestamp, spans: self.spans, contexts: self.contexts, + request: self.request, } } From 2e699f02957f7b1dc705c0a225a1efd21172b654 Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Wed, 2 Mar 2022 14:03:55 -0800 Subject: [PATCH 2/6] instead insert into data; Signed-off-by: Jess Frazelle --- sentry-core/src/performance.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/sentry-core/src/performance.rs b/sentry-core/src/performance.rs index bc86386f..1c364ea1 100644 --- a/sentry-core/src/performance.rs +++ b/sentry-core/src/performance.rs @@ -473,7 +473,36 @@ impl Span { /// Set the HTTP request information for this Span. pub fn set_request(&self, request: protocol::Request) { let mut span = self.span.lock().unwrap(); - span.request = Some(request); + // Extract values from the request to be used as data in the span. + if let Some(method) = request.method { + span.data.insert("method".into(), method.into()); + } + if let Some(url) = request.url { + span.data.insert("url".into(), url.to_string().into()); + } + if let Some(data) = request.data { + if let Ok(data) = serde_json::from_str::(&data) { + span.data.insert("data".into(), data.into()); + } else { + span.data.insert("data".into(), data.into()); + } + } + if let Some(query_string) = request.query_string { + span.data.insert("query_string".into(), query_string.into()); + } + if let Some(cookies) = request.cookies { + span.data.insert("cookies".into(), cookies.into()); + } + if !request.headers.is_empty() { + if let Ok(headers) = serde_json::to_value(request.headers) { + span.data.insert("headers".into(), headers.into()); + } + } + if !request.env.is_empty() { + if let Ok(env) = serde_json::to_value(request.env) { + span.data.insert("env".into(), env.into()); + } + } } /// Returns the headers needed for distributed tracing. From 512562d6176268a6b1191e6e14110bd712e415e6 Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Wed, 2 Mar 2022 14:11:38 -0800 Subject: [PATCH 3/6] fix clippy Signed-off-by: Jess Frazelle --- sentry-core/src/performance.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sentry-core/src/performance.rs b/sentry-core/src/performance.rs index 1c364ea1..02deb332 100644 --- a/sentry-core/src/performance.rs +++ b/sentry-core/src/performance.rs @@ -482,7 +482,7 @@ impl Span { } if let Some(data) = request.data { if let Ok(data) = serde_json::from_str::(&data) { - span.data.insert("data".into(), data.into()); + span.data.insert("data".into(), data); } else { span.data.insert("data".into(), data.into()); } @@ -495,12 +495,12 @@ impl Span { } if !request.headers.is_empty() { if let Ok(headers) = serde_json::to_value(request.headers) { - span.data.insert("headers".into(), headers.into()); + span.data.insert("headers".into(), headers); } } if !request.env.is_empty() { if let Ok(env) = serde_json::to_value(request.env) { - span.data.insert("env".into(), env.into()); + span.data.insert("env".into(), env); } } } From 88053d815eee0ec98f5f3292fe96f9a84a01096b Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Tue, 8 Mar 2022 08:14:53 -0800 Subject: [PATCH 4/6] cleanup Signed-off-by: Jess Frazelle --- sentry-types/src/protocol/v7.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sentry-types/src/protocol/v7.rs b/sentry-types/src/protocol/v7.rs index 764a39ab..430a35d0 100644 --- a/sentry-types/src/protocol/v7.rs +++ b/sentry-types/src/protocol/v7.rs @@ -1737,9 +1737,6 @@ pub struct Span { /// Optional extra information to be sent with the span. #[serde(default, skip_serializing_if = "Map::is_empty")] pub data: Map, - /// Optionally HTTP request data to be sent along. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub request: Option, } impl Default for Span { @@ -1756,7 +1753,6 @@ impl Default for Span { same_process_as_parent: Default::default(), op: Default::default(), data: Default::default(), - request: Default::default(), } } } From 8fd3ad6d95bfdfe6e8405449d0bdc6d005fd71c5 Mon Sep 17 00:00:00 2001 From: Betty Da Date: Tue, 8 Mar 2022 21:32:16 -0500 Subject: [PATCH 5/6] add test and extend demo --- sentry/examples/performance-demo.rs | 14 ++++++++++++++ sentry/tests/test_tracing.rs | 10 +++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/sentry/examples/performance-demo.rs b/sentry/examples/performance-demo.rs index 12436c84..6fb7fc66 100644 --- a/sentry/examples/performance-demo.rs +++ b/sentry/examples/performance-demo.rs @@ -1,6 +1,8 @@ use std::thread; use std::time::Duration; +use sentry::protocol::Request; + // cargo run --example performance-demo fn main() { let _sentry = sentry::init(sentry::ClientOptions { @@ -12,6 +14,12 @@ fn main() { let transaction = sentry::start_transaction(sentry::TransactionContext::new("transaction", "root span")); + let tx_request = Request { + url: Some("https://honk.beep".parse().unwrap()), + method: Some("GET".to_string()), + ..Request::default() + }; + transaction.set_request(tx_request); sentry::configure_scope(|scope| scope.set_span(Some(transaction.clone().into()))); main_span1(); @@ -76,6 +84,12 @@ where sentry::start_transaction(ctx).into() } }; + let span_request = Request { + url: Some("https://beep.beep".parse().unwrap()), + method: Some("GET".to_string()), + ..Request::default() + }; + span1.set_request(span_request); sentry::configure_scope(|scope| scope.set_span(Some(span1.clone()))); let rv = f(); diff --git a/sentry/tests/test_tracing.rs b/sentry/tests/test_tracing.rs index 4c8038a0..d50e234a 100644 --- a/sentry/tests/test_tracing.rs +++ b/sentry/tests/test_tracing.rs @@ -1,7 +1,7 @@ #![cfg(feature = "test")] use log_ as log; -use sentry::protocol::{Context, Value}; +use sentry::protocol::{Context, Request, Value}; use tracing_ as tracing; use tracing_subscriber::prelude::*; @@ -146,6 +146,13 @@ fn test_set_transaction() { || { 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"))); @@ -164,4 +171,5 @@ fn test_set_transaction() { }; assert_eq!(transaction.name.as_deref().unwrap(), "new name"); + assert!(transaction.request.is_some()); } From fb70909f4dd8046f9cc1442489946b82d0a423da Mon Sep 17 00:00:00 2001 From: Betty Da Date: Tue, 8 Mar 2022 21:50:01 -0500 Subject: [PATCH 6/6] changelog --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3477a4ab..ab7cae19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## Unreleased + +**Features**: + +- Request data can now be attached to Transactions and Spans via `set_transaction`. ([#439](https://github.com/getsentry/sentry-rust/pull/439)) + +**Thank you**: + +Features, fixes and improvements in this release have been contributed by: + +- [@jessfraz](https://github.com/jessfraz) + ## 0.25.0 **Breaking Changes**: