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 request to transaction #439

Merged
merged 6 commits into from Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions sentry-core/src/performance.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -454,6 +470,41 @@ 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();
// 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::<serde_json::Value>(&data) {
span.data.insert("data".into(), data);
} 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);
}
}
if !request.env.is_empty() {
if let Ok(env) = serde_json::to_value(request.env) {
span.data.insert("env".into(), env);
}
}
}

/// Returns the headers needed for distributed tracing.
pub fn iter_headers(&self) -> TraceHeadersIter {
let span = self.span.lock().unwrap();
Expand Down
11 changes: 10 additions & 1 deletion sentry-types/src/protocol/v7.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Value>,
/// Optionally HTTP request data to be sent along.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub request: Option<Request>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're inserting the request's contents into data, this should be removed. (This'll likely be pruned or rejected by relay, if it slips in.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah thanks!

}

impl Default for Span {
Expand All @@ -1753,6 +1756,7 @@ impl Default for Span {
same_process_as_parent: Default::default(),
op: Default::default(),
data: Default::default(),
request: Default::default(),
}
}
}
Expand Down Expand Up @@ -1942,6 +1946,9 @@ pub struct Transaction<'a> {
/// Optional contexts.
#[serde(default, skip_serializing_if = "Map::is_empty")]
pub contexts: Map<String, Context>,
/// Optionally HTTP request data to be sent along.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub request: Option<Request>,
}

impl<'a> Default for Transaction<'a> {
Expand All @@ -1959,6 +1966,7 @@ impl<'a> Default for Transaction<'a> {
start_timestamp: SystemTime::now(),
spans: Default::default(),
contexts: Default::default(),
request: Default::default(),
}
}
}
Expand All @@ -1984,6 +1992,7 @@ impl<'a> Transaction<'a> {
start_timestamp: self.start_timestamp,
spans: self.spans,
contexts: self.contexts,
request: self.request,
}
}

Expand Down