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

ref: Support distributed tracing in Actix integration (NATIVE-313) #411

Merged
merged 4 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion sentry-actix/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ async fn captures_message(_req: HttpRequest) -> Result<String, Error> {
Ok("Hello World".into())
}

// cargo run -p sentry-actix --example basic
#[actix_web::main]
async fn main() -> io::Result<()> {
let _guard = sentry::init(sentry::ClientOptions {
auto_session_tracking: true,
traces_sample_rate: 1.0,
session_mode: sentry::SessionMode::Request,
..Default::default()
});
Expand All @@ -35,7 +37,7 @@ async fn main() -> io::Result<()> {

HttpServer::new(|| {
App::new()
.wrap(sentry_actix::Sentry::new())
.wrap(sentry_actix::Sentry::with_transaction())
.service(healthy)
.service(errors)
.service(captures_message)
Expand Down
87 changes: 82 additions & 5 deletions sentry-actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ use std::pin::Pin;
use std::sync::Arc;

use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
use actix_web::http::StatusCode;
use actix_web::Error;
use futures_util::future::{ok, Future, Ready};
use futures_util::FutureExt;

use sentry_core::protocol::{ClientSdkPackage, Event, Request};
use sentry_core::protocol::{self, ClientSdkPackage, Event, Request};
use sentry_core::{Hub, SentryFutureExt};

/// A helper construct that can be used to reconfigure and build the middleware.
Expand All @@ -88,6 +89,13 @@ impl SentryBuilder {
self.middleware
}

/// Tells the middleware to start a new performance monitoring transaction for each request.
#[must_use]
pub fn start_transaction(mut self, start_transaction: bool) -> Self {
self.middleware.start_transaction = start_transaction;
self
}

/// Reconfigures the middleware so that it uses a specific hub instead of the default one.
#[must_use]
pub fn with_hub(mut self, hub: Arc<Hub>) -> Self {
Expand Down Expand Up @@ -125,6 +133,7 @@ pub struct Sentry {
hub: Option<Arc<Hub>>,
emit_header: bool,
capture_server_errors: bool,
start_transaction: bool,
}

impl Sentry {
Expand All @@ -134,6 +143,15 @@ impl Sentry {
hub: None,
emit_header: false,
capture_server_errors: true,
start_transaction: false,
}
}

/// Creates a new sentry middleware which starts a new performance monitoring transaction for each request.
pub fn with_transaction() -> Sentry {
Sentry {
start_transaction: true,
..Sentry::default()
}
}

Expand Down Expand Up @@ -215,10 +233,35 @@ where
.as_ref()
.map_or(false, |client| client.options().send_default_pii);

let (tx, sentry_req) = sentry_request_from_http(&req, with_pii);
hub.configure_scope(|scope| {
scope.set_transaction(tx.as_deref());
scope.add_event_processor(move |event| Some(process_event(event, &sentry_req)))
let (mut tx, sentry_req) = sentry_request_from_http(&req, with_pii);

let transaction = if inner.start_transaction {
let name = std::mem::take(&mut tx)
.unwrap_or_else(|| format!("{} {}", req.method(), req.uri()));

let headers = req.headers().iter().flat_map(|(header, value)| {
value.to_str().ok().map(|value| (header.as_str(), value))
});

let ctx = sentry_core::TransactionContext::continue_from_headers(
&name,
"http.server",
headers,
);
Some(hub.start_transaction(ctx))
} else {
None
};

let parent_span = hub.configure_scope(|scope| {
let parent_span = scope.get_span();
if let Some(transaction) = transaction.as_ref() {
scope.set_span(Some(transaction.clone().into()));
} else {
scope.set_transaction(tx.as_deref());
}
scope.add_event_processor(move |event| Some(process_event(event, &sentry_req)));
parent_span
});

let fut = self.service.call(req).bind_hub(hub.clone());
Expand All @@ -231,6 +274,15 @@ where
if inner.capture_server_errors {
hub.capture_error(&e);
}

if let Some(transaction) = transaction {
if transaction.get_status().is_none() {
let status = protocol::SpanStatus::UnknownError;
transaction.set_status(status);
}
transaction.finish();
hub.configure_scope(|scope| scope.set_span(parent_span));
}
return Err(e);
}
};
Expand All @@ -249,12 +301,37 @@ where
}
}

if let Some(transaction) = transaction {
if transaction.get_status().is_none() {
let status = map_status(res.status());
transaction.set_status(status);
}
transaction.finish();
hub.configure_scope(|scope| scope.set_span(parent_span));
}

Ok(res)
}
.boxed_local()
}
}

fn map_status(status: StatusCode) -> protocol::SpanStatus {
match status {
StatusCode::UNAUTHORIZED => protocol::SpanStatus::Unauthenticated,
StatusCode::FORBIDDEN => protocol::SpanStatus::PermissionDenied,
StatusCode::NOT_FOUND => protocol::SpanStatus::NotFound,
StatusCode::TOO_MANY_REQUESTS => protocol::SpanStatus::ResourceExhausted,
status if status.is_client_error() => protocol::SpanStatus::InvalidArgument,
StatusCode::NOT_IMPLEMENTED => protocol::SpanStatus::Unimplemented,
StatusCode::SERVICE_UNAVAILABLE => protocol::SpanStatus::Unavailable,
status if status.is_server_error() => protocol::SpanStatus::InternalError,
StatusCode::CONFLICT => protocol::SpanStatus::AlreadyExists,
status if status.is_success() => protocol::SpanStatus::Ok,
_ => protocol::SpanStatus::UnknownError,
}
}

/// Build a Sentry request struct from the HTTP request
fn sentry_request_from_http(request: &ServiceRequest, with_pii: bool) -> (Option<String>, Request) {
let transaction = if let Some(name) = request.match_name() {
Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl TransactionContext {
) -> Self {
let mut trace = None;
for (k, v) in headers.into_iter() {
if k == "sentry-trace" {
if k.eq_ignore_ascii_case("sentry-trace") {
trace = parse_sentry_trace(v);
}
}
Expand Down